catchorg / catchorg/Catch2

GIVEN section in a function not executed

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

Description

**Describe the bug**
In a BDD style test, a GIVEN section in a function isn't always executed.
Its execution depends on the function parameters used in the string passed to the GIVEN macro.
Removing the GIVEN macro appears to fix the bug.

**Expected behavior**
Every test should be executed.
The first two should pass and the last two should fail.

**Reproduction steps**
Execute the MWE below without any additional flags.
```
#define CATCH_CONFIG_MAIN
#include
#include

auto test_func(bool flag1, bool flag2){
//with this string all tests will be executed
//GIVEN("Flag1 = " + std::to_string(flag1) + " and Flag2 = " + std::to_string(flag2)){

//with this string only the first two tests are executed
//GIVEN("Flag1 = " + std::to_string(flag1)){

//with this string only the first test is executed
GIVEN("Something"){
if (flag2 == true) REQUIRE(true);
else REQUIRE(false);
}
}

SCENARIO("Testing a function with 2 parameters."){
test_func(true, true);
test_func(false, true);

test_func(true, false);
test_func(false, false);
}
```

**Platform information:**
- OS: **Linux 5.10.10-arch1-1**
- Compiler+version: **GCC v10.2.0 & Clang v11.0.1**
- Catch version: **v2.13.4 & v2.12.4**

**Additional context**
Initially, my application with the same structure crashed with the message "Assertion `m_parent' failed".
While trying to create a MWE, I noticed this odd behavior.
Removing the GIVEN section always seems to fix the problem.

I suspect this might not be the intended use of the GIVEN macro, but I couldn't find any contrary information in the docs.
However, using it this way removes a lot of boilerplate test code for me.
I am fairly certain the function is called and it appears that only the GIVEN section is not executed.

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.