userver: userver/utest/utest.hpp Source File
Loading...
Searching...
No Matches
utest.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utest/utest.hpp
4/// @brief Versions of gtest macros that run tests in a coroutine environment
5
6#include <chrono>
7
8#include <gtest/gtest.h>
9
10#include <userver/compiler/impl/asan.hpp>
11#include <userver/engine/run_in_coro.hpp> // legacy
12#include <userver/utest/assert_macros.hpp>
13#include <userver/utest/test_case_macros.hpp>
14
15// gtest-specific serializers
16namespace testing {
17
18void PrintTo(std::chrono::seconds s, std::ostream* os);
19void PrintTo(std::chrono::milliseconds ms, std::ostream* os);
20void PrintTo(std::chrono::microseconds us, std::ostream* os);
21void PrintTo(std::chrono::nanoseconds ns, std::ostream* os);
22
23} // namespace testing
24
25USERVER_NAMESPACE_BEGIN
26
27/// Mocks and test helpers
28namespace utest {
29
30/// The maximum time a typical test is allowed to execute. If exceeded, a
31/// deadlock is assumed. This time must not be too low to avoid flaky tests.
32///
33/// Use this timeout for awaiting something that is expected to complete.
34/// Use appropriate shorter timeouts for awaiting something that is not expected to ever complete.
35inline constexpr std::chrono::seconds kMaxTestWaitTime(20);
36
37} // namespace utest
38
39USERVER_NAMESPACE_END
40
41#ifndef NDEBUG
42// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
43#define DISABLED_IN_DEBUG_TEST_NAME(name) DISABLED_##name
44#else
45// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
46#define DISABLED_IN_DEBUG_TEST_NAME(name) name
47#endif
48
49#if USERVER_IMPL_HAS_ASAN
50// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
51#define DISABLED_IN_ASAN_TEST_NAME(name) DISABLED_##name
52#else
53// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
54#define DISABLED_IN_ASAN_TEST_NAME(name) name
55#endif
56
57#ifdef __APPLE__
58// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
59#define DISABLED_IN_MAC_OS_TEST_NAME(name) DISABLED_##name
60#else
61// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
62#define DISABLED_IN_MAC_OS_TEST_NAME(name) name
63#endif
64
65#ifdef _LIBCPP_VERSION
66// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
67#define DISABLED_IN_LIBCPP_TEST_NAME(name) DISABLED_##name
68#else
69// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
70#define DISABLED_IN_LIBCPP_TEST_NAME(name) name
71#endif
72
73/// @defgroup userver_utest Unit-testing (utest)
74///
75/// @brief Versions of gtest macros that run tests in a coroutine environment
76/// and other helpers.
77///
78/// There are the following extensions:
79///
80/// 1. `_MT` ("multi-threaded") macro versions take 'thread_count' integer
81/// as the 3rd parameter at the test definition. It specifies the number of
82/// worker threads that should be created for the test. By default,
83/// there is only 1 worker thread, which should be enough for most tests;
84/// 2. The current thread count is available via
85/// `engine::current_task::GetWorkerCount()`.
86///
87/// ## Usage examples:
88/// @snippet core/src/engine/semaphore_test.cpp UTEST macro example 1
89/// @snippet core/src/engine/semaphore_test.cpp UTEST macro example 2
90///
91/// See @ref scripts/docs/en/userver/testing.md for more usage examples and
92/// descriptions
93/// @{
94
95/// @brief An equivalent of the gtest macro TEST that starts the test body as a
96/// coroutine task
97/// @hideinitializer
98// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
99#define UTEST(test_suite_name, test_name) IMPL_UTEST_TEST(test_suite_name, test_name, 1, false)
100
101/// @brief An equivalent of the gtest macro TEST for death tests that starts the
102/// test body as a coroutine task
103/// @hideinitializer
104// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
105#define UTEST_DEATH(test_suite_name, test_name) IMPL_UTEST_TEST(test_suite_name, test_name, 1, true)
106
107/// @brief An equivalent of the gtest macro TEST that starts the test body as a
108/// coroutine task
109/// @param thread_count the number of threads to process tasks
110/// @hideinitializer
111// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
112#define UTEST_MT(test_suite_name, test_name, thread_count)
113 IMPL_UTEST_TEST(test_suite_name, test_name, thread_count, false)
114
115/// @brief An equivalent of the gtest macro TEST_F that starts the test body as
116/// a coroutine task
117/// @hideinitializer
118// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
119#define UTEST_F(test_suite_name, test_name) IMPL_UTEST_TEST_F(test_suite_name, test_name, 1, false)
120
121/// @brief An equivalent of the gtest macro TEST_F for death tests that starts
122/// the test body as a coroutine task
123/// @hideinitializer
124// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
125#define UTEST_F_DEATH(test_suite_name, test_name) IMPL_UTEST_TEST_F(test_suite_name, test_name, 1, true)
126
127/// @brief An equivalent of the gtest macro TEST_F that starts the test body as
128/// a coroutine task
129/// @param thread_count the number of threads to process tasks
130/// @hideinitializer
131// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
132#define UTEST_F_MT(test_suite_name, test_name, thread_count)
133 IMPL_UTEST_TEST_F(test_suite_name, test_name, thread_count, false)
134
135/// @brief An equivalent of the gtest macro TEST_P that starts the test body as
136/// a coroutine task
137/// @hideinitializer
138// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
139#define UTEST_P(test_suite_name, test_name) IMPL_UTEST_TEST_P(test_suite_name, test_name, 1, false)
140
141/// @brief An equivalent of the gtest macro TEST_P for death tests that starts
142/// the test body as a coroutine task
143/// @hideinitializer
144// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
145#define UTEST_P_DEATH(test_suite_name, test_name) IMPL_UTEST_TEST_P(test_suite_name, test_name, 1, true)
146
147/// @brief An equivalent of the gtest macro TEST_P that starts the test body as
148/// a coroutine task
149/// @hideinitializer
150// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
151#define UTEST_P_MT(test_suite_name, test_name, thread_count)
152 IMPL_UTEST_TEST_P(test_suite_name, test_name, thread_count, false)
153
154/// @brief An equivalent of the gtest macro TYPED_TEST that starts the test body
155/// as a coroutine task
156/// @hideinitializer
157// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
158#define TYPED_UTEST(test_suite_name, test_name) IMPL_UTEST_TYPED_TEST(test_suite_name, test_name, 1, false)
159
160/// @brief An equivalent of the gtest macro TYPED_TEST that starts the test body
161/// as a coroutine task
162/// @param thread_count the number of threads to process tasks
163/// @hideinitializer
164// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
165#define TYPED_UTEST_MT(test_suite_name, test_name, thread_count)
166 IMPL_UTEST_TYPED_TEST(test_suite_name, test_name, thread_count, false)
167
168/// @brief An equivalent of the gtest macro TYPED_TEST_P that starts the test
169/// body as a coroutine task
170/// @hideinitializer
171// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
172#define TYPED_UTEST_P(test_suite_name, test_name) IMPL_UTEST_TYPED_TEST_P(test_suite_name, test_name, 1, false)
173
174/// @brief An equivalent of the gtest macro TYPED_TEST_P that starts the test
175/// body as a coroutine task
176/// @param thread_count the number of threads to process tasks
177/// @hideinitializer
178// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
179#define TYPED_UTEST_P_MT(test_suite_name, test_name, thread_count)
180 IMPL_UTEST_TYPED_TEST_P(test_suite_name, test_name, thread_count, false)
181
182/// @brief An equivalent of the gtest macro TYPED_TEST_SUITE that starts the
183/// test body as a coroutine task
184/// @hideinitializer
185// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
186#define TYPED_UTEST_SUITE(test_suite_name, types)
188 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
190 TYPED_TEST_SUITE(test_suite_name, types, USERVER_NAMESPACE::utest::impl::DefaultNameGenerator);
191 }
192 struct UtestImplForceSemicolon
193
194/// @brief An equivalent of the gtest macro INSTANTIATE_TEST_SUITE_P that starts
195/// the test body as a coroutine task
196/// @hideinitializer
197// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
198#define INSTANTIATE_UTEST_SUITE_P(prefix, test_suite_name, ...)
199 IMPL_UTEST_MAKE_USER_FIXTURE_ALIAS(test_suite_name);
200 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
202 test_suite_name,
203 USERVER_NAMESPACE::utest::impl::TestLauncherParametric
204 )
205 INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, __VA_ARGS__);
206 }
207 struct UtestImplForceSemicolon
208
209/// @brief An equivalent of the gtest macro REGISTER_TYPED_TEST_SUITE_P that
210/// starts the test body as a coroutine task
211/// @hideinitializer
212// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
213#define REGISTER_TYPED_UTEST_SUITE_P(test_suite_name, ...)
214 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
215 REGISTER_TYPED_TEST_SUITE_P(test_suite_name, __VA_ARGS__);
216 }
217 struct UtestImplForceSemicolon
218
219/// @brief An equivalent of the gtest macro INSTANTIATE_TYPED_TEST_SUITE_P that
220/// starts the test body as a coroutine task
221/// @hideinitializer
222// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
223#define INSTANTIATE_TYPED_UTEST_SUITE_P(prefix, test_suite_name, types)
224 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
225 INSTANTIATE_TYPED_TEST_SUITE_P(
226 prefix,
227 test_suite_name,
228 types,
229 USERVER_NAMESPACE::utest::impl::DefaultNameGenerator
230 );
231 }
232 struct UtestImplForceSemicolon
233
234/// @brief An equivalent of the gtest macro TYPED_TEST_SUITE_P that starts the
235/// test body as a coroutine task
236/// @hideinitializer
237// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
238#define TYPED_UTEST_SUITE_P(test_suite_name)
240 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
242 TYPED_TEST_SUITE_P(test_suite_name);
243 }
244 struct UtestImplForceSemicolon
245/// @}