All userver test helpers live in userver-utest
CMake target:
To include gtest and userver-specific macros, do:
The header provides alternative gtest-like macros that run tests in a coroutine environment:
As usual, gmock is available in <gmock/gmock.h>
.
See also utest::PrintTestName for info on how to simplify writing parametrized tests and official gtest documentation.
To test code that uses coroutine environment (e.g. creates tasks or uses synchronization primitives), use UTEST
instead of TEST
in the test header:
There are U
-versions for all gtest macros that declare tests and test groups: UTEST_F
, UTEST_P
, INSTANTIATE_UTEST_SUITE_P
etc. Test fixture's constructor, destructor, SetUp
and TearDown
are executed in the same coroutine environment as the test body.
By default, a single-threaded TaskProcessor
is used. It is usually enough, because userver engine enables concurrency even on 1 thread. However, some tests require actual parallelism, e.g. torture tests that search for potential data races. In such cases, use _MT
macro versions:
The specified thread count is available in U
-tests as GetThreadCount()
method.
For DEATH-tests (when testing aborts or assertion fails) use UTEST_DEATH
. It configures gtest-DEATH-checks to work in multithreaded environment. Also it disables using of ev_default_loop
and catching of SIGCHLD
signal to work with gtest's waitpid()
calls.
Standard gtest exception assertions provide poor error messages. Their equivalents with proper diagnostics are available in <userver/utest/utest.hpp>
:
Example usage:
utils::datetime::Now()
and utils::datetime::SteadyNow()
from <userver/utils/datetime.hpp>
instead of std::chrono::system_clock::now()
and std::chrono::steady_clock::now()
, respectively.<userver/utils/mock_now.hpp>
You can fill dynamic config with custom config values using dynamic_config::StorageMock
.
If you don't want to specify all configs used by the tested code, you can use default dynamic config.
To use default dynamic config values in tests, add DEFAULT_DYNAMIC_CONFIG_FILENAME
preprocessor definition to your test CMake target, specifying the path of a YAML file with dynamic_config::DocsMap
contents.
Default dynamic config values can be accessed using <dynamic_config/test_helpers.hpp>
:
dynamic_config::GetDefaultSnapshot()
dynamic_config::GetDefaultSource()
dynamic_config::MakeDefaultStorage(overrides)
All userver benchmark helpers live in userver-ubench
CMake target:
As usual, google-benchmark is available in <benchmark/benchmark.h>
.
See also official google-benchmark documentation.
Use engine::RunStandalone
to run parts of your benchmark in a coroutine environment:
See the equivalent utest section.
Default dynamic configs are available in in <userver/dynamic_config/test_helpers.hpp>
.