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)
120 IMPL_UTEST_TEST(test_suite_name, test_name, 1, false)
121
122/// @brief An equivalent of the gtest macro TEST for death tests that starts the
123/// test body as a coroutine task
124/// @hideinitializer
125// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
126#define UTEST_DEATH(test_suite_name, test_name)
127 IMPL_UTEST_TEST(test_suite_name, test_name, 1, true)
128
129/// @brief An equivalent of the gtest macro TEST that starts the test body as a
130/// coroutine task
131/// @param thread_count the number of threads to process tasks
132/// @hideinitializer
133// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
134#define UTEST_MT(test_suite_name, test_name, thread_count)
135 IMPL_UTEST_TEST(test_suite_name, test_name, thread_count, false)
136
137/// @brief An equivalent of the gtest macro TEST_F that starts the test body as
138/// a coroutine task
139/// @hideinitializer
140// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
141#define UTEST_F(test_suite_name, test_name)
142 IMPL_UTEST_TEST_F(test_suite_name, test_name, 1, false)
143
144/// @brief An equivalent of the gtest macro TEST_F for death tests that starts
145/// the test body as a coroutine task
146/// @hideinitializer
147// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
148#define UTEST_F_DEATH(test_suite_name, test_name)
149 IMPL_UTEST_TEST_F(test_suite_name, test_name, 1, true)
150
151/// @brief An equivalent of the gtest macro TEST_F that starts the test body as
152/// a coroutine task
153/// @param thread_count the number of threads to process tasks
154/// @hideinitializer
155// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
156#define UTEST_F_MT(test_suite_name, test_name, thread_count)
157 IMPL_UTEST_TEST_F(test_suite_name, test_name, thread_count, false)
158
159/// @brief An equivalent of the gtest macro TEST_P that starts the test body as
160/// a coroutine task
161/// @hideinitializer
162// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
163#define UTEST_P(test_suite_name, test_name)
164 IMPL_UTEST_TEST_P(test_suite_name, test_name, 1, false)
165
166/// @brief An equivalent of the gtest macro TEST_P that starts the test body as
167/// a coroutine task
168/// @hideinitializer
169// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
170#define UTEST_P_MT(test_suite_name, test_name, thread_count)
171 IMPL_UTEST_TEST_P(test_suite_name, test_name, thread_count, false)
172
173/// @brief An equivalent of the gtest macro TYPED_TEST that starts the test body
174/// as a coroutine task
175/// @hideinitializer
176// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
177#define TYPED_UTEST(test_suite_name, test_name)
178 IMPL_UTEST_TYPED_TEST(test_suite_name, test_name, 1, false)
179
180/// @brief An equivalent of the gtest macro TYPED_TEST that starts the test body
181/// as a coroutine task
182/// @param thread_count the number of threads to process tasks
183/// @hideinitializer
184// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
185#define TYPED_UTEST_MT(test_suite_name, test_name, thread_count)
186 IMPL_UTEST_TYPED_TEST(test_suite_name, test_name, thread_count, false)
187
188/// @brief An equivalent of the gtest macro TYPED_TEST_P that starts the test
189/// body as a coroutine task
190/// @hideinitializer
191// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
192#define TYPED_UTEST_P(test_suite_name, test_name)
193 IMPL_UTEST_TYPED_TEST_P(test_suite_name, test_name, 1, false)
194
195/// @brief An equivalent of the gtest macro TYPED_TEST_P that starts the test
196/// body as a coroutine task
197/// @param thread_count the number of threads to process tasks
198/// @hideinitializer
199// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
200#define TYPED_UTEST_P_MT(test_suite_name, test_name, thread_count)
201 IMPL_UTEST_TYPED_TEST_P(test_suite_name, test_name, thread_count, false)
202
203/// @brief An equivalent of the gtest macro TYPED_TEST_SUITE that starts the
204/// test body as a coroutine task
205/// @hideinitializer
206// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
207#define TYPED_UTEST_SUITE(test_suite_name, types)
209 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
211 TYPED_TEST_SUITE(test_suite_name, types,
212 USERVER_NAMESPACE::utest::impl::DefaultNameGenerator);
213 }
214 struct UtestImplForceSemicolon
215
216/// @brief An equivalent of the gtest macro INSTANTIATE_TEST_SUITE_P that starts
217/// the test body as a coroutine task
218/// @hideinitializer
219// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
220#define INSTANTIATE_UTEST_SUITE_P(prefix, test_suite_name, ...)
221 IMPL_UTEST_MAKE_USER_FIXTURE_ALIAS(test_suite_name);
222 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
224 test_suite_name, USERVER_NAMESPACE::utest::impl::TestLauncherParametric)
225 INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, __VA_ARGS__);
226 }
227 struct UtestImplForceSemicolon
228
229/// @brief An equivalent of the gtest macro REGISTER_TYPED_TEST_SUITE_P that
230/// starts the test body as a coroutine task
231/// @hideinitializer
232// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
233#define REGISTER_TYPED_UTEST_SUITE_P(test_suite_name, ...)
234 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
235 REGISTER_TYPED_TEST_SUITE_P(test_suite_name, __VA_ARGS__);
236 }
237 struct UtestImplForceSemicolon
238
239/// @brief An equivalent of the gtest macro INSTANTIATE_TYPED_TEST_SUITE_P that
240/// starts the test body as a coroutine task
241/// @hideinitializer
242// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
243#define INSTANTIATE_TYPED_UTEST_SUITE_P(prefix, test_suite_name, types)
244 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
245 INSTANTIATE_TYPED_TEST_SUITE_P(
246 prefix, test_suite_name, types,
247 USERVER_NAMESPACE::utest::impl::DefaultNameGenerator);
248 }
249 struct UtestImplForceSemicolon
250
251/// @brief An equivalent of the gtest macro TYPED_TEST_SUITE_P that starts the
252/// test body as a coroutine task
253/// @hideinitializer
254// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
255#define TYPED_UTEST_SUITE_P(test_suite_name)
257 namespace IMPL_UTEST_NAMESPACE_NAME(test_suite_name) {
259 TYPED_TEST_SUITE_P(test_suite_name);
260 }
261 struct UtestImplForceSemicolon
262/// @}