catchorg / catchorg/Catch2

Different preconditions but same set of checks

Open
#2,251 0 comments 1 reaction 0 assignees View on GitHub
Query
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

**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

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the related issue #603 and the existing generator functionality mentioned in the report. Determine whether the proposed scenario-condition syntax can support alternative preconditions with one shared set of checks; done means the capability is defined, implemented, and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.