X21-PartialTestCaseEvents.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. /**\file
  7. * Registers custom reporter that reports testCase* events
  8. *
  9. * The resulting executable can then be used by an external Python script
  10. * to verify that testCase{Starting,Ended} and testCasePartial{Starting,Ended}
  11. * events are properly nested.
  12. */
  13. #include <catch2/catch_test_macros.hpp>
  14. #include <catch2/reporters/catch_reporter_streaming_base.hpp>
  15. #include <catch2/catch_test_case_info.hpp>
  16. #include <catch2/reporters/catch_reporter_registrars.hpp>
  17. #include <catch2/generators/catch_generators.hpp>
  18. #include <iostream>
  19. using Catch::TestCaseInfo;
  20. using Catch::TestCaseStats;
  21. class PartialReporter : public Catch::StreamingReporterBase {
  22. public:
  23. using StreamingReporterBase::StreamingReporterBase;
  24. ~PartialReporter() override; // = default
  25. static std::string getDescription() {
  26. return "Special reporter for testing TestCasePartialStarting/Ended events";
  27. }
  28. //! Called _once_ for each TEST_CASE, no matter how many times it is entered
  29. void testCaseStarting(TestCaseInfo const& testInfo) override {
  30. std::cout << "TestCaseStarting: " << testInfo.name << '\n';
  31. }
  32. //! Called _every time_ a TEST_CASE is entered, including repeats (due to sections)
  33. void testCasePartialStarting(TestCaseInfo const& testInfo, uint64_t partNumber) override {
  34. std::cout << "TestCaseStartingPartial: " << testInfo.name << '#' << partNumber << '\n';
  35. }
  36. //! Called _every time_ a TEST_CASE is entered, including repeats (due to sections)
  37. void testCasePartialEnded(TestCaseStats const& testCaseStats, uint64_t partNumber) override {
  38. std::cout << "TestCasePartialEnded: " << testCaseStats.testInfo->name << '#' << partNumber << '\n';
  39. }
  40. //! Called _once_ for each TEST_CASE, no matter how many times it is entered
  41. void testCaseEnded(TestCaseStats const& testCaseStats) override {
  42. std::cout << "TestCaseEnded: " << testCaseStats.testInfo->name << '\n';
  43. }
  44. };
  45. PartialReporter::~PartialReporter() = default;
  46. CATCH_REGISTER_REPORTER("partial", PartialReporter)
  47. TEST_CASE("section") {
  48. SECTION("A") {}
  49. SECTION("B") {}
  50. SECTION("C") {}
  51. SECTION("D") {}
  52. }
  53. TEST_CASE("generator") {
  54. auto _ = GENERATE(1, 2, 3, 4);
  55. (void)_;
  56. }