@@ -43,6 +43,21 @@ namespace CppUtils::UnitTest
4343 {}
4444 };
4545
46+ class TestSkipped final: public std::runtime_error
47+ {
48+ public:
49+ explicit inline TestSkipped(std::string message):
50+ std::runtime_error{std::move(message)}
51+ {}
52+ };
53+
54+ enum class TestStatus
55+ {
56+ Success,
57+ Failure,
58+ Skipped
59+ };
60+
4661 class TestRunner final
4762 {
4863 using Logger = Logger<"UnitTests">;
@@ -62,7 +77,7 @@ namespace CppUtils::UnitTest
6277 m_testSuites.addDependency(name, prerequisite);
6378 }
6479
65- inline auto executeTest(const Test& test, const TestSettings& settings) const -> bool
80+ inline auto executeTest(const Test& test, const TestSettings& settings) const -> TestStatus
6681 {
6782 using namespace std::chrono_literals;
6883 if (settings.verbose)
@@ -102,6 +117,12 @@ namespace CppUtils::UnitTest
102117
103118 future.get();
104119 }
120+ catch (const TestSkipped& skipped)
121+ {
122+ if (settings.verbose)
123+ Logger::print<"warning">("Skipped: {}", skipped.what());
124+ return TestStatus::Skipped;
125+ }
105126 catch (const TestException&)
106127 {
107128 std::throw_with_nested(std::runtime_error{std::format("The following test didn't pass:\n{}", test.name)});
@@ -114,12 +135,12 @@ namespace CppUtils::UnitTest
114135 catch (const std::exception& exception)
115136 {
116137 logException<Logger>(exception);
117- return false ;
138+ return TestStatus::Failure ;
118139 }
119140 if (settings.verbose)
120141 Logger::print<"success">("Passed");
121142 Logger::waitUntilFinished(10s);
122- return true ;
143+ return TestStatus::Success ;
123144 }
124145
125146 inline auto executeTests(TestSettings settings) -> int
@@ -167,8 +188,11 @@ namespace CppUtils::UnitTest
167188 continue;
168189 }
169190
170- if (executeTest(test, settings))
191+ const auto status = executeTest(test, settings);
192+ if (status == TestStatus::Success)
171193 ++nbSuccess;
194+ else if (status == TestStatus::Skipped)
195+ ++nbSkip;
172196 else
173197 {
174198 ++nbFail;
@@ -213,7 +237,7 @@ namespace CppUtils::UnitTest
213237
214238 if (nbSkip > 0)
215239 {
216- Logger::emit<"color">(Terminal::TextColor::TextColorEnum::Yellow, std::format("- {} skipped tests (due to failed dependencies) ", nbSkip));
240+ Logger::emit<"color">(Terminal::TextColor::TextColorEnum::Yellow, std::format("- {} skipped tests", nbSkip));
217241 for (const auto& skippedTestSuite : skippedTestSuites)
218242 Logger::emit<"color">(Terminal::TextColor::TextColorEnum::Yellow, std::format(" - {}", skippedTestSuite));
219243 }
@@ -254,6 +278,11 @@ namespace CppUtils::UnitTest
254278 tests.emplace_back(std::move(name), [function = std::move(function)](std::stop_token) { function(); }, timeout);
255279 }
256280
281+ [[noreturn]] inline auto skip(std::string message) -> void
282+ {
283+ throw TestSkipped{std::move(message)};
284+ }
285+
257286 // Todo C++23: std::stacktrace stacktrace = std::current_stacktrace()
258287 inline auto expect(bool condition, std::source_location sourceLocation = std::source_location::current()) -> void
259288 {
0 commit comments