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
- 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);
}
- 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?