0

Let's say we want to test one file (or class). What should be better approach (always? maybe in special cases?)?

Framework Catch2: https://github.com/catchorg/Catch2

  1. Several test cases
TEST_CASE("First", "[some_details]")
{
  Class obj;

  REQUIRE(obj.firstFunction() == 1);
}

TEST_CASE("Second", "[some_details]")
{
  Class obj;

  REQUIRE(obj.secondFunction() == 1);
}

TEST_CASE("Third", "[some_details]")
{
  Class obj;

  REQUIRE(obj.thirdFunction() == 1);
}
  1. One test case with several sections
TEST_CASE("Check functions", "[some_details]")
{
  Class obj;

  SECTION("First", "[some_details]")
  {
    REQUIRE(obj.firstFunction() == 1);
  }

  SECTION("Second", "[some_details]")
  {
    REQUIRE(obj.secondFunction() == 1);
  }

  SECTION("Third", "[some_details]")
  {
    REQUIRE(obj.thirdFunction() == 1);
  }
}

I think we ought to use TEST_CASE for each functionality separately (if it is possible). If it's not then we can use SECTION in case of some dependence. I also think that SECTION are good when we want to check several outputs from one function, for example:

TEST_CASE("Check function first", "[some_details]")
{
  Class obj;

  SECTION("Positive", "[some_details]")
  {
    REQUIRE(obj.firstFunction() == 1);
  }

  SECTION("Negative", "[some_details]")
  {
    REQUIRE(obj.firstFunction() == 0);
  }
}

What's your opinion? Which approach is better (and when)? You got some suggestions?

bridzysta
  • 3
  • 1

1 Answers1

0

The main difference is:

Using TEST_CASE you can test each case individually. With SECTION this is not possible in some IDEs (like CLION.

arved
  • 4,401
  • 4
  • 30
  • 53
  • What you mean by "feature to execute only a specific SECTION"? You thought about how to run exact section, like it is mentioned here: https://github.com/catchorg/Catch2/blob/devel/docs/command-line.md#specify-the-section-to-run – bridzysta Nov 25 '22 at 12:26
  • In my example above in step 2 TEST_CASE run three times, once for each section. – bridzysta Nov 25 '22 at 12:30
  • You are right on the command line it is possible. In my IDE (CLION it is not yet possible..) – arved Nov 25 '22 at 13:48