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