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