Run the Test!
Running your unit tests is the final step to validate your setup. Follow the instructions below to run and manage your tests effectively.
Run the Unit Test
Navigate to the unittest directory in your build environment and execute the unit test binary:
./unittest/unittest
If you followed the earlier steps and created a test suite with a single test, the output should look like this:
===[ Running 1 test suites with 1 tests ]===
Start Time: Tue Apr 25 10:41:23 2023
Filter: no filter set
Suite: Basic OK!
Test: Magic OK!
Total Test Duration: 8.63e-05 seconds
===[ SUCCESS | Successfully run all tests without errors. ]===
For the complete example, the output includes more details, such as errors and additional tests:
===[ Running 2 test suites with 4 tests ]===
Start Time: Tue Apr 25 10:09:11 2023
Filter: no filter set
Suite: Basic OK!
Test: Magic OK!
Test: NameSetAndGet OK!
Test: IsNamePalindrome FAILED!
[1]: ~/erbsland-unittest-example/unittest/src/BasicTest.hpp:57: REQUIRE(exampleLib.isNamePalindrome())
Suite: Context OK!
Test: NameSetAndGet FAILED!
[2]: ~/erbsland-unittest-example/unittest/src/ContextTest.hpp:52: REQUIRE_FALSE(exampleLib.getNameLength() == expectedSize)
[1]: ~/erbsland-unittest-example/unittest/src/ContextTest.hpp:63: WITH_CONTEXT(setAndVerifyName_error("Lisa"))
ExampleLib:
getName() = "Lisa"
getNameLength() = 4
===[ ERROR SUMMARY ]===
Error 1 - Basic / IsNamePalindrome FAILED!
[1]: ~/erbsland-unittest-example/unittest/src/BasicTest.hpp:57: REQUIRE(exampleLib.isNamePalindrome())
Error 2 - Context / NameSetAndGet FAILED!
[2]: ~/erbsland-unittest-example/unittest/src/ContextTest.hpp:52: REQUIRE_FALSE(exampleLib.getNameLength() == expectedSize)
[1]: ~/erbsland-unittest-example/unittest/src/ContextTest.hpp:63: WITH_CONTEXT(setAndVerifyName_error("Lisa"))
===[ ERROR | 2 errors while running the tests. ]===
Don’t worry about the errors in the complete example. They are intentionally included to demonstrate the framework’s features, such as error handling and contextual output.
Get Command Line Help
To explore all available command-line options for your test suite, use the -h
or --help
option:
./unittest/unittest --help
This outputs a list of options:
Erbsland Unit Test Help:
-h/--help ......... Display this help
-v/--verbose ...... Display verbose messages. Skipped tests.
-e ................ Stop at the first error.
-l/--list ......... List all suites and tests. Do not run any test.
-c/--no-color ..... Do not colorize the output and disable status updates.
-s/--no-summary ... Do not list the first three errors at the end of the run.
name:<name> ....... Exclusively run tests with the specified test or class name (case sensitive).
+name:<name> ...... Run tests with the specified test or class name, even optional ones.
-name:<name> ...... Skip tests with the specified test or class name.
target:<target> ... Exclusively run tests for the specified target.
+target:<target> .. Run tests with for the specified target, even optional ones.
-target:<target> .. Skip tests with for the specified target.
tag:<tag> ......... Exclusively run tests with the specified tags.
+tag:<tag> ........ Run tests with the specified tags, even optional ones.
-tag:<tag> ........ Skip tests with the specified tags.
By default, all tests not marked with SKIP_BY_DEFAULT()
are enabled. Use the +
and -
options to customize the test set.
Filtering Tests
You can run specific tests using tags, names, or targets. For example, to run the IsNamePalindromeBruteForce
test, use the tag long-test
:
./unittest/unittest tag:long-test
The output will look like this:
===[ Running 1 test suites with 1 tests ]===
Start Time: Tue Apr 25 10:48:11 2023
Filter:
Tags: only use 'long-test'
Suite: Long OK!
(...)
Test: IsNamePalindromeBruteForce OK!
Total Test Duration: 85.1 seconds
===[ SUCCESS | Successfully run all tests without errors. ]===
This test takes longer to execute, especially in debugging mode.
With these tools, you can efficiently run and manage your test suite. Let’s proceed to explore what’s next.