2

I have to check the some condition (eg. initial state) in several test cases. I cannot use CHECK in function and I would like to replace the current macro if possible.

#include "catch.hpp"

#define CHECK_INITIAL_STATE() \
    CHECK(first_condition); \
    CHECK(second_condition);

TEST_CASE("first_test") {
    CHECK_INITIAL_STATE();
    // do something
    // restore state
    CHECK_INITIAL_STATE();
}
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Jónás Balázs
  • 781
  • 10
  • 24
  • 1
    Why can't use you `CHECK` in a function? – Rakete1111 Sep 06 '18 at 08:35
  • Thanks the answer. Error message will refer to the line in the function not the line where the error actually happened. It's just more comfortable when I can jump to the erroneous line and I don't need to search for the place where the function was called. – Jónás Balázs Sep 06 '18 at 08:53

1 Answers1

2

Catch2 comes with this feature built-in in a very elegant way:

TEST_CASE("first_test") {
    CHECK(first_condition);
    CHECK(second_condition);

    SECTION("do something 1") {
        // this test is executed after the code outside of the section
    }
    SECTION("do something 2") {
        // this test is executed after the code outside of the section
        // but without executing the previous section
    }
}
Rakete1111
  • 47,013
  • 16
  • 123
  • 162