Executing Unit Tests

Unit tests can be executed individually. You can also execute a selection or all of the unit tests in a unit test module. The unit test runner provides a way to execute unit tests in multiple Unit Test Modules with a single command.

To execute a single unit test:

  1. Highlight the applicable test.
  2. Press the F8 key, the Run selected button from the bottom menu bar, or select Run unit test from the context menu.

The number of passed, failed, and disabled tests, as well as the total number of tests in the document is displayed on the bottom menu bar.

A green check mark indicates that the test passed. A red X indicates that the test failed. The actual result is shown to the right of the test and can be copied and pasted into the test.

To run all unit tests in a given document, select the Run all button from the bottom menu bar. You can also highlight multiple unit tests and run the highlighted tests by selecting Run selected.

When you run a unit test, if the unit test expression doesn't evaluate to a UnitTestResult object, then the test will fail and the error message:

Error: Returned expression is not a valid unit test result

will be displayed.

Using the Execution Details with Unit Tests

The Execution Details window can be used to find the cause of the error. To use the Execution Details window in conjunction with unit tests:

  1. Select View > Execution Details (Shift + F4) to open the execution details window.
  2. If the Details OFF button is displayed on the status bar, press the button to turn the details on.
  3. Run the unit test in question. The details of the execution are displayed in the Execution Details window.

For more details about the Execution Details window, see The Execution Details Viewer.

Unit Tests and the Build Process

Before a unit test is executed, the unit test expression is run through the same build function that is used to build object definitions that contain code. This is required in order to be able to execute unit tests where the left-hand side of the unit test expression has the form of a clause (contains an Assert object for example) rather than just being a simple function call.

The build processing is largely transparent to users, but it is important to be aware of the fact that Sym doesn't support dynamically building the right-hand side of a unit test. The following unit test will fail even though Simplify[x + x] returns 2x:

But you can do something equivalent to this that will pass: