catchorg / catchorg/Catch2

Conditional SECTION

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

Description

**Description**
To have a `SECTION` variant `SECTION_IF` that would be only executed if condition was true:

```
#define SECTION_IF(cond, ...)
```
The motivational example:
```
TEST_CASE("MyVector")
{
auto vector = GENERATE(std::vector{}, std::vector{1,2,3,4,5});

SECTION_IF(!vector.isEmpty(), "operator[]")
{
REQUIRE(vector[0] == 1);
}
}
```
Currently I can skip code inside a section with `if` but the section will still cause the whole test to be needlessly restarted even if it is not relevant for given generated input (e.g. empty vector). Another option would be to have two separate test cases but since the tests share a lots of code it that is less than ideal. Same as putting the generators or initialization code in only the relevant sections. Too much code duplication.

Instead I resorted to the following workaround:
```
if(condition)
{
SECTION("MySection")
{
}
}
```
That seems to work but there is a bug report #714 that seems to have issues with this approach (might be outdated though) so this might be buggy way of doing things.

I hope this can be added seemelssly. Not sure if this would greatly upset how SECTIONs are handled by Catch2.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.