[insert:file:style.inc]

Vespa Test Framework Tutorial

The Vespa test framework helps you write small applications to test C++ code. All interaction with the test framework is done with the use of macros.

The minimal test application looks like this:

[insert:example:minimal/minimal_test.cpp]

The runnable application itself is called a test suite and inherits its name from the cpp file containing the TEST_MAIN macro. Each individual verification of some value is called a check. Checks can be put anywhere, but it is highly recommended that you put them inside tests. Tests are created by a family of macros. Another macro (TEST_RUN_ALL) is used to execute them.

Example with two tests, each containing a single check:

[insert:example:simple/simple_test.cpp]

All checks are available in two variants. Those with the EXPECT_ prefix allow execution to continue even if a check fails. Those with the ASSERT_ prefix will terminate execution of the current test if it fails.

[insert:example:checks/checks_test.cpp]

Checks involving comparison of values typically use == and < operators to compare values. Also; in order to be part of a comparison check, the value must support the << operator to print the value to a string stream in case the check fails.

Sometimes multiple tests wish to use the same predefined state as a starting point. This state is called a test fixture. Test fixtures are untangled from the actual tests and construction/destruction is used to handle their lifetime. When thinking of what can be used as a fixture, think of what can be put after new to create an object.

[insert:example:fixtures/fixtures_test.cpp]

One of the most novel features of the test framework is the ability to write multi-threaded tests. Multi-threaded tests are created by using test macros containing _MT and supplying a thread count. All threads will execute the block of code encapsulated by the test. The test fixtures are shared among all threads. In addition, each thread has a variable named num_threads containing the total number of threads running the test and a variable named thread_id identifying the thread.

The TEST_BARRIER() macro can be used inside the test to synchronize the threads. The macro will block execution of each thread invoking it until all threads have invoked it.

[insert:example:threads/threads_test.cpp]
[insert:example:../box/box_test.cpp] [insert:example:../barrier/barrier_test.cpp] [insert:example:../dual_merge_director/dual_merge_director_test.cpp]

Overall Execution Macros

Test Creation Macros

Check Macros

Thread Macros

State Tracking Macros

Macros of Limited Use