VirtualFluids 0.1.0
Parallel CFD LBM Solver
|
This page describes how to add unit tests to VirtualFluids. VirtualFluids uses the C++ testing and mocking framework GoogleTest.
VirtualFluids is build upon multiple libraries <library>
(e.g. basics). Every library can have a corresponding test executable. The test executable is called <library>Test
and is created automatically by the CMake build system if the following two conditions are met:
test/unit-tests/
with the name of the library exists (e.g. basics)CMakeLists.txt
with the following content: vf_add_tests(...)
(e.g. CMakeLists.txt)The tests can be built by running the following commands:
or by using the presets:
The tests can then be run by executing the specific test executable:
or all test executables can be run with:
Additionally all test executables are automatically executed by our continuous integration pipeline during each push to the repository.
We will show you a simple example on how to add tests to VirtualFluids: To add tests for the class ExampleClass
declared in ExampleClass.h
you need to create a file named ExampleClassTest.cpp
in the test library folder (example)
The following code block shows a simple test case:
The signature of the test contains the test suite's name along with the name of the test: TEST(TestSuiteName, TestName)
.
The first step inside the TEST()
function is to arrange the variables and objects needed for the test.
The next step ist to act on the target behavior. The act step should cover the main thing to be tested. For example this could be calling a member function which should be tested.
The third and final step is to assert the expected outcome. The result or response of the act step is checked. The assertion(s) determine(s) whether the test passes or fails.
For the assert step googleTest provides two options: EXPECT and ASSERT. Upon failure, EXPECT_
macros generate nonfatal failures and allow the current function to continue running, while ASSERT_
macros generate fatal failures and abort the current function. The above example uses the EXPECT_THAT
macro. With EXPECT_THAT()
you can use Google Test's predefined matchers from the testing namespace (for example testing::IsTrue()
or testing::IsEmpty()
or your own custom matchers. The example above uses the custom matcher RealEQ()
for testing the equality of two real numbers (float or double).
When you test a class which depends on CUDA you may get a build error like this:
To fix this problem, you need to specify CUDA as language in CMake by adding the following code to CMakeList.txt
:
For VirtualFluidsGPU you can find the CMakeList here: VirtualFluids/src/gpu/VirtualFluids_GPU/CMakeLists.txt
.
You can find further information on how to write tests in GoogleTest User’s Guide.