Catch2 icon indicating copy to clipboard operation
Catch2 copied to clipboard

Different preconditions but same set of checks

Open zrno opened this issue 3 years ago • 0 comments

Description I would like to write BDD tests where the same set of checks is executed for different preconditions to reduce code duplication. Another older issue had the same question and was closed but I don't think the issue is solved in an elegant way. https://github.com/catchorg/Catch2/issues/603

TEST_CASE("Test foo1")
{
    Foo foo;
    foo.Method1();

    REQUIRE(foo.hasSomething());
    REQUIRE(....); 
}

TEST_CASE("Test foo2")
{
    Foo foo;
    foo.Method2();

    REQUIRE(foo.hasSomething());
    REQUIRE(....); 
}

Additional context I would like to write unit tests that look like this:

TEST_CASE("Test foo")
{
    Foo foo;
    
    SCENARIO_CONDITION("Precondition1")
    {
        foo.Method1();
    }
    OR_SCENARIO_CONDITION("Precondition2")
    {
        foo.Method2();
    }

    REQUIRE(foo.hasSomething());
    REQUIRE(....); 
}

I have a feeling this could be easily achieved with the current state of Catch2 and the existing generators but I am unfamiliar with the code to propose a solution myself

zrno avatar Jun 17 '21 23:06 zrno