Skip to content

Running Tests

Anton Hibl edited this page Mar 18, 2023 · 8 revisions

Running Tests with ERT

This package relies on a comprehensive test suite powered by ERT (Emacs Lisp Regression Testing), which is bundled with Emacs by default. To avoid any interference from API rate-limiting, the tests employ a set of mock responses, ensuring accurate evaluation of the package’s functionality.

There are two methods to run the test suite, keep in mind this is intended for developers who might want to extend the package for themselves. Tests have been included in the tests directory when you clone the repo for development purposes, but they are not installed during package-install as they are intended solely for development and not for end users. Including them would unnecessarily increase the package size.

Now here are the two methods to run the test suite:

Using the ERT Interactive Test Runner

  1. In Emacs, execute M-x ert. This command opens the ERT interactive test runner.
  2. To run all tests, press enter(defaulting to t), or to run a specific test by its name, specify the name.
  3. The test results will be displayed in the ERT test runner buffer.

Running Tests in Batch Mode

  1. In the scratch buffer or another buffer, evaluate the following code: (ert-run-tests-batch-and-exit)
  2. The tests will be executed in batch mode, and the test results will appear in the Messages buffer.
  3. If all tests pass, you will see the message “All tests passed.”

By following either of these methods, you can effectively test the package’s functionality and ensure it is working as intended.

Adding Tests with ERT

To expand the test suite and cover more aspects of the package, you can add new tests using ERT. Writing tests with ERT involves defining test functions with assertions to verify the correctness of your package’s functions.

Here’s a step-by-step guide on adding new tests with ERT:

Creating a test function

Create a test function by defining a new function with the ert-deftest macro. The function name should follow the format test-<your-function-name>. For instance, if you want to test a function called gptai-example-function, the test function should be named test-gptai-example-function.

(ert-deftest test-gptai-example-function ()
  ;; Your test code here
  )

Writing assertions

Within the test function, use ERT’s assertion macros to check if your package’s functions return the expected results. Some common assertion macros are:

should: Check if a form’s value is non-nil. should-not: Check if a form’s value is nil. should-error: Ensure a form signals an error. should-equal: Compare two forms for equality.

For example, to test gptai-example-function returning the expected output, you can use the should-equal macro:

(ert-deftest test-gptai-example-function ()
  (should-equal (gptai-example-function "input")
                "expected-output"))

Grouping tests

You can group related tests using the :tags keyword followed by a list of symbols. This makes it easier to run a specific group of tests.

(ert-deftest test-gptai-example-function ()
  :tags '(gptai examples)
  ;; Your test code here
  )

To run only tests with a specific tag, use M-x ert, then press t and type (member tag-name).

Adding the test function to the test file

Add the test function to the appropriate test file, such as gptai-test.el. If the test belongs to a submodule, place it in the corresponding test file, like gptai-turbo-test.el.

Running the tests

Once you’ve added the new test, run the entire test suite using either the ERT Interactive Test Runner, M-x ert, or batch mode, (ert-run-tests-batch-and-exit), as described in the previous section.

By following these steps, you can extend the test suite to cover more functionality and cover different use cases and new functionalities.