userver: userver/utest/death_tests.hpp Source File
Loading...
Searching...
No Matches
death_tests.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utest/death_tests.hpp
4/// @brief Contains facilities for testing service crashes
5/// @ingroup userver_universal
6
7#include <gtest/gtest.h>
8
9#include <userver/utils/assert.hpp>
10#include <userver/utils/impl/disable_core_dumps.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace utest::impl {
15
16class DeathTestScope final {
17public:
18 DeathTestScope() { utils::impl::dump_stacktrace_on_assert_failure = false; }
19 ~DeathTestScope() { utils::impl::dump_stacktrace_on_assert_failure = true; }
20
21 bool ShouldKeepIterating() const noexcept { return keep_iterating_; }
22 void StopIterating() noexcept { keep_iterating_ = false; }
23
24private:
25 utils::impl::DisableCoreDumps disable_core_dumps_;
26 bool keep_iterating_{true};
27};
28
29} // namespace utest::impl
30
31USERVER_NAMESPACE_END
32
33/// @ingroup userver_utest
34///
35/// @brief An optimized equivalent of EXPECT_DEATH.
36///
37/// @hideinitializer
38// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
39#define UEXPECT_DEATH(statement, message)
40 for (USERVER_NAMESPACE::utest::impl::DeathTestScope utest_impl_death_test_scope;
41 utest_impl_death_test_scope.ShouldKeepIterating();
42 utest_impl_death_test_scope.StopIterating())
43 EXPECT_DEATH(statement, message)