VirtualFluids 0.1.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
Unit Tests

This page describes how to add unit tests to VirtualFluids. VirtualFluids uses the C++ testing and mocking framework GoogleTest.

0. Test Structure in VirtualFluids

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:

  1. A folder in test/unit-tests/ with the name of the library exists (e.g. basics)
  2. The folder contains a file named CMakeLists.txt with the following content: vf_add_tests(...) (e.g. CMakeLists.txt)
  3. A least one cpp test file exists in the library folder (see 2. Adding Tests)

1. Building and Running Tests

The tests can be built by running the following commands:

cmake .. -DBUILD_VF_UNIT_TESTS=ON
make

or by using the presets:

cmake .. --preset=all_make
make

The tests can then be run by executing the specific test executable:

./bin/<library>Test

or all test executables can be run with:

ctest

Additionally all test executables are automatically executed by our continuous integration pipeline during each push to the repository.

2. Adding Tests

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:

#include <gmock/gmock.h>
#include "ExampleClass.h"
auto RealEq = [](auto value) {
#ifdef VF_DOUBLE_ACCURACY {#ifdef-vfdoubleaccuracy}
return testing::DoubleEq(value);
#else {#else}
return testing::FloatEq(value);
#endif {#endif}
};
{
//arrange
real number = 1.1;
int multiplicator = 2;
ExampleClass sut();
// act
result = sut.multiplyIntByReal(multiplicator, number);
// assert
}
std::shared_ptr< T > SPtr
float real
Definition DataTypes.h:42
TEST(ConfigurationFileTest, ContainsReturnsTrueForExistingKey)
auto RealEq

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.

Assertions with googletest

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).

3. Common Problems

When you test a class which depends on CUDA you may get a build error like this:

fatal error: cuda_runtime.h: No such file or directory
11 | #include <cuda_runtime.h>

To fix this problem, you need to specify CUDA as language in CMake by adding the following code to CMakeList.txt:

if(BUILD_VF_UNIT_TESTS)
set_source_files_properties(ExampleClassTest.cpp PROPERTIES LANGUAGE CUDA)
endif()

For VirtualFluidsGPU you can find the CMakeList here: VirtualFluids/src/gpu/VirtualFluids_GPU/CMakeLists.txt.

4. Further Information

You can find further information on how to write tests in GoogleTest User’s Guide.