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. `GetThreadCount()` method is available in the test scope.
85///
86/// ## Usage examples:
87/// @snippet core/src/engine/semaphore_test.cpp UTEST macro example 1
88/// @snippet core/src/engine/semaphore_test.cpp UTEST macro example 2
89///
90/// See @ref scripts/docs/en/userver/testing.md for more usage examples and
91/// descriptions
92/// @{
93
94/// @brief An equivalent of the gtest macro TEST that starts the test body as a
95/// coroutine task
96/// @hideinitializer
97// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
98#define UTEST(test_suite_name, test_name) IMPL_UTEST_TEST(test_suite_name, test_name, 1, false)
99
100/// @brief An equivalent of the gtest macro TEST for death tests that starts the
101/// test body as a coroutine task
102/// @hideinitializer
103// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
104#define UTEST_DEATH(test_suite_name, test_name) IMPL_UTEST_TEST(test_suite_name, test_name, 1, true)
105
106/// @brief An equivalent of the gtest macro TEST that starts the test body as a
107/// coroutine task
108/// @param thread_count the number of threads to process tasks
109/// @hideinitializer
110// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
111#define UTEST_MT(test_suite_name, test_name, thread_count)
112 IMPL_UTEST_TEST(test_suite_name, test_name, thread_count, false)
113
114/// @brief An equivalent of the gtest macro TEST_F that starts the test body as
115/// a coroutine task
116/// @hideinitializer
117// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
118#define UTEST_F(test_suite_name, test_name) IMPL_UTEST_TEST_F(test_suite_name, test_name, 1, false)
119
120/// @brief An equivalent of the gtest macro TEST_F for death tests that starts
121/// the test body as a coroutine task
122/// @hideinitializer
123// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
124#define UTEST_F_DEATH(test_suite_name, test_name) IMPL_UTEST_TEST_F(test_suite_name, test_name, 1, true)
125
126/// @brief An equivalent of the gtest macro TEST_F that starts the test body as
127/// a coroutine task
128/// @param thread_count the number of threads to process tasks
129/// @hideinitializer
130// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
131#define UTEST_F_MT(test_suite_name, test_name, thread_count)
132 IMPL_UTEST_TEST_F(test_suite_name, test_name, thread_count, false)
133
134/// @brief An equivalent of the gtest macro TEST_P that starts the test body as
135/// a coroutine task
136/// @hideinitializer
137// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
138#define UTEST_P(test_suite_name, test_name) IMPL_UTEST_TEST_P(test_suite_name, test_name, 1, false)
139
140/// @brief An equivalent of the gtest macro TEST_P for death tests that starts
141/// the test body as a coroutine task
142/// @hideinitializer
143// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
144#define UTEST_P_DEATH(test_suite_name, test_name) IMPL_UTEST_TEST_P(test_suite_name, test_name, 1, true)
145
146/// @brief An equivalent of the gtest macro TEST_P that starts the test body as
147/// a coroutine task
148/// @hideinitializer
149// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
150#define UTEST_P_MT(test_suite_name, test_name, thread_count)
151 IMPL_UTEST_TEST_P(test_suite_name, test_name, thread_count, false)
152
153/// @brief An equivalent of the gtest macro TYPED_TEST that starts the test body
154/// as a coroutine task
155/// @hideinitializer
156// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
157#define TYPED_UTEST(test_suite_name, test_name) IMPL_UTEST_TYPED_TEST(test_suite_name, test_name, 1, false)
158
159/// @brief An equivalent of the gtest macro TYPED_TEST that starts the test body
160/// as a coroutine task
161/// @param thread_count the number of threads to process tasks
162/// @hideinitializer
163// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
164#define TYPED_UTEST_MT(test_suite_name, test_name, thread_count)
165 IMPL_UTEST_TYPED_TEST(test_suite_name, test_name, thread_count, false)
166
167/// @brief An equivalent of the gtest macro TYPED_TEST_P that starts the test
168/// body as a coroutine task
169/// @hideinitializer
170// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
171#define TYPED_UTEST_P(test_suite_name, test_name) IMPL_UTEST_TYPED_TEST_P(test_suite_name, test_name, 1, false)
172
173/// @brief An equivalent of the gtest macro TYPED_TEST_P that starts the test
174/// body as a coroutine task
175/// @param thread_count the number of threads to process tasks
176/// @hideinitializer
177// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
178#define TYPED_UTEST_P_MT(test_suite_name, test_name, thread_count)
179 IMPL_UTEST_TYPED_TEST_P(test_suite_name, test_name, thread_count, false)
180
181/// @brief An equivalent of the gtest macro TYPED_TEST_SUITE that starts the
182/// test body as a coroutine task
183/// @hideinitializer
184// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
185#define TYPED_UTEST_SUITE(test_suite_name, types)
187 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
189 TYPED_TEST_SUITE(test_suite_name, types, USERVER_NAMESPACE::utest::impl::DefaultNameGenerator);
190 }
191 struct UtestImplForceSemicolon
192
193/// @brief An equivalent of the gtest macro INSTANTIATE_TEST_SUITE_P that starts
194/// the test body as a coroutine task
195/// @hideinitializer
196// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
197#define INSTANTIATE_UTEST_SUITE_P(prefix, test_suite_name, ...)
198 IMPL_UTEST_MAKE_USER_FIXTURE_ALIAS(test_suite_name);
199 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
201 test_suite_name,
202 USERVER_NAMESPACE::utest::impl::TestLauncherParametric
203 )
204 INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, __VA_ARGS__);
205 }
206 struct UtestImplForceSemicolon
207
208/// @brief An equivalent of the gtest macro REGISTER_TYPED_TEST_SUITE_P that
209/// starts the test body as a coroutine task
210/// @hideinitializer
211// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
212#define REGISTER_TYPED_UTEST_SUITE_P(test_suite_name, ...)
213 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
214 REGISTER_TYPED_TEST_SUITE_P(test_suite_name, __VA_ARGS__);
215 }
216 struct UtestImplForceSemicolon
217
218/// @brief An equivalent of the gtest macro INSTANTIATE_TYPED_TEST_SUITE_P that
219/// starts the test body as a coroutine task
220/// @hideinitializer
221// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
222#define INSTANTIATE_TYPED_UTEST_SUITE_P(prefix, test_suite_name, types)
223 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
224 INSTANTIATE_TYPED_TEST_SUITE_P(
225 prefix,
226 test_suite_name,
227 types,
228 USERVER_NAMESPACE::utest::impl::DefaultNameGenerator
229 );
230 }
231 struct UtestImplForceSemicolon
232
233/// @brief An equivalent of the gtest macro TYPED_TEST_SUITE_P that starts the
234/// test body as a coroutine task
235/// @hideinitializer
236// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
237#define TYPED_UTEST_SUITE_P(test_suite_name)
239 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
241 TYPED_TEST_SUITE_P(test_suite_name);
242 }
243 struct UtestImplForceSemicolon
244/// @}