Skip to content

Commit

Permalink
Renaming TEST_CASE_FIXTURE to TEST_CASE_PERSISTENT_FIXTURE. Also add …
Browse files Browse the repository at this point in the history
…licence to Fixture example
  • Loading branch information
KStocky committed Aug 3, 2024
1 parent 3c7920a commit bf4575a
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 193 deletions.
23 changes: 17 additions & 6 deletions docs/test-fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@

## Non-Templated test fixtures

Although Catch allows you to group tests together as [sections within a test case](test-cases-and-sections.md), it can still be convenient, sometimes, to group them using a more traditional test fixture. Catch fully supports this too with 3 different macros for non-templated test fixtures. They are:
Although Catch2 allows you to group tests together as
[sections within a test case](test-cases-and-sections.md), it can still
be convenient, sometimes, to group them using a more traditional test.
Catch2 fully supports this too with 3 different macros for
non-templated test fixtures. They are:

| Macro | Description |
|----------|-------------|
|1. `TEST_CASE_METHOD(className, ...)`| Creates a uniquely named class which inherits from the class specified by `className`. The test function will be a member of this derived class. An instance of the derived class will be created for every partial run of the test case. |
|2. `METHOD_AS_TEST_CASE(member-function, ...)`| Uses `member-function` as the test function. An instance of the class will be created for each partial run of the test case. |
|3. `TEST_CASE_FIXTURE(className, ...)`| Creates a uniquely named class which inherits from the class specified by `className`. The test function will be a member of this derived class. An instance of the derived class will be created at the start of the test run. That instance will be destroyed once the entire test case has ended. |
|3. `TEST_CASE_PERSISTENT_FIXTURE(className, ...)`| Creates a uniquely named class which inherits from the class specified by `className`. The test function will be a member of this derived class. An instance of the derived class will be created at the start of the test run. That instance will be destroyed once the entire test case has ended. |

### 1. `TEST_CASE_METHOD`

Expand Down Expand Up @@ -73,18 +77,21 @@ METHOD_AS_TEST_CASE( TestClass::testCase, "Use class's method as a test case", "

This type of fixture is similar to [TEST_CASE_METHOD](#1-test_case_method) except in this case it will directly use the provided class to create an object rather than a derived class.

### 3. `TEST_CASE_FIXTURE`
### 3. `TEST_CASE_PERSISTENT_FIXTURE`

> [Introduced](link-to-issue-or-PR) in Catch2 X.Y.Z

`TEST_CASE_FIXTURE` behaves in the same way as [TEST_CASE_METHOD](#1-test_case_method) except that there will only be one instance created throughout the entire run of a test case. To demonstrate this have a look at the following example:
`TEST_CASE_PERSISTENT_FIXTURE` behaves in the same way as
[TEST_CASE_METHOD](#1-test_case_method) except that there will only be
one instance created throughout the entire run of a test case. To
demonstrate this have a look at the following example:

```cpp
struct MyFixture{
int MyInt = 0;
};

TEST_CASE_FIXTURE(MyFixture, "Tests with MyFixture") {
TEST_CASE_PERSISTENT_FIXTURE(MyFixture, "Tests with MyFixture") {

const int val = MyInt++;

Expand All @@ -97,7 +104,11 @@ TEST_CASE_FIXTURE(MyFixture, "Tests with MyFixture") {
}
}
```
This test case will be executed twice as there are two leaf sections. On the first run `val` will be `0` and on the second run `val` will be `1`. This is useful if you would like to share some expensive setup code with all runs of your test case which can't be done at static initialization time.
This test case will be executed twice as there are two leaf sections.
On the first run `val` will be `0` and on the second run `val` will be
`1`. This is useful if you would like to share some expensive setup code
with all runs of your test case which can't be done at static
initialization time.
## Templated test fixtures
Expand Down
7 changes: 6 additions & 1 deletion examples/Fixture.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

// SPDX-License-Identifier: BSL-1.0

// Fixture.cpp
Expand All @@ -15,7 +20,7 @@ struct MyFixture {
mutable int MyInt = 0;
};

TEST_CASE_FIXTURE(MyFixture, "Tests with MyFixture") {
TEST_CASE_PERSISTENT_FIXTURE(MyFixture, "Tests with MyFixture") {

const int val = MyInt++;

Expand Down
8 changes: 4 additions & 4 deletions src/catch2/catch_test_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ )
#define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ )
#define CATCH_METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ )
#define CATCH_TEST_CASE_FIXTURE( className, ... ) INTERNAL_CATCH_TEST_CASE_FIXTURE( className, __VA_ARGS__ )
#define CATCH_TEST_CASE_PERSISTENT_FIXTURE( className, ... ) INTERNAL_CATCH_TEST_CASE_PERSISTENT_FIXTURE( className, __VA_ARGS__ )
#define CATCH_REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ )
#define CATCH_SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ )
#define CATCH_DYNAMIC_SECTION( ... ) INTERNAL_CATCH_DYNAMIC_SECTION( __VA_ARGS__ )
Expand Down Expand Up @@ -98,7 +98,7 @@
#define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEST_ ))
#define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEST_ ))
#define CATCH_METHOD_AS_TEST_CASE( method, ... )
#define CATCH_TEST_CASE_FIXTURE( className, ... )
#define CATCH_TEST_CASE_PERSISTENT_FIXTURE( className, ... )
#define CATCH_REGISTER_TEST_CASE( Function, ... ) (void)(0)
#define CATCH_SECTION( ... )
#define CATCH_DYNAMIC_SECTION( ... )
Expand Down Expand Up @@ -144,7 +144,7 @@
#define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ )
#define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ )
#define METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ )
#define TEST_CASE_FIXTURE( className, ... ) INTERNAL_CATCH_TEST_CASE_FIXTURE( className, __VA_ARGS__ )
#define TEST_CASE_PERSISTENT_FIXTURE( className, ... ) INTERNAL_CATCH_TEST_CASE_PERSISTENT_FIXTURE( className, __VA_ARGS__ )
#define REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ )
#define SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ )
#define DYNAMIC_SECTION( ... ) INTERNAL_CATCH_DYNAMIC_SECTION( __VA_ARGS__ )
Expand Down Expand Up @@ -198,7 +198,7 @@
#define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEST_ ), __VA_ARGS__)
#define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEST_ ))
#define METHOD_AS_TEST_CASE( method, ... )
#define TEST_CASE_FIXTURE( className, ... )
#define TEST_CASE_PERSISTENT_FIXTURE( className, ... )
#define REGISTER_TEST_CASE( Function, ... ) (void)(0)
#define SECTION( ... )
#define DYNAMIC_SECTION( ... )
Expand Down
6 changes: 3 additions & 3 deletions src/catch2/internal/catch_test_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static int catchInternalSectionHint = 0;
INTERNAL_CATCH_TEST_CASE_METHOD2( INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEST_ ), ClassName, __VA_ARGS__ )

///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_TEST_CASE_FIXTURE2( TestName, ClassName, ... ) \
#define INTERNAL_CATCH_TEST_CASE_PERSISTENT_FIXTURE2( TestName, ClassName, ... ) \
CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \
CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \
CATCH_INTERNAL_SUPPRESS_UNUSED_VARIABLE_WARNINGS \
Expand All @@ -187,8 +187,8 @@ static int catchInternalSectionHint = 0;
} \
CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \
void TestName::test() const
#define INTERNAL_CATCH_TEST_CASE_FIXTURE( ClassName, ... ) \
INTERNAL_CATCH_TEST_CASE_FIXTURE2( INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEST_ ), ClassName, __VA_ARGS__ )
#define INTERNAL_CATCH_TEST_CASE_PERSISTENT_FIXTURE( ClassName, ... ) \
INTERNAL_CATCH_TEST_CASE_PERSISTENT_FIXTURE2( INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEST_ ), ClassName, __VA_ARGS__ )


///////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions tests/SelfTest/Baselines/automake.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ Nor would this
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 1
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 3
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 6
:test-result: FAIL A TEST_CASE_FIXTURE based test run that fails
:test-result: PASS A TEST_CASE_FIXTURE based test run that succeeds
:test-result: FAIL A TEST_CASE_METHOD based test run that fails
:test-result: PASS A TEST_CASE_METHOD based test run that succeeds
:test-result: FAIL A TEST_CASE_PERSISTENT_FIXTURE based test run that fails
:test-result: PASS A TEST_CASE_PERSISTENT_FIXTURE based test run that succeeds
:test-result: PASS A Template product test case - Foo<float>
:test-result: PASS A Template product test case - Foo<int>
:test-result: PASS A Template product test case - std::vector<float>
Expand Down
4 changes: 2 additions & 2 deletions tests/SelfTest/Baselines/automake.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 1
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 3
:test-result: PASS A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds - 6
:test-result: FAIL A TEST_CASE_FIXTURE based test run that fails
:test-result: PASS A TEST_CASE_FIXTURE based test run that succeeds
:test-result: FAIL A TEST_CASE_METHOD based test run that fails
:test-result: PASS A TEST_CASE_METHOD based test run that succeeds
:test-result: FAIL A TEST_CASE_PERSISTENT_FIXTURE based test run that fails
:test-result: PASS A TEST_CASE_PERSISTENT_FIXTURE based test run that succeeds
:test-result: PASS A Template product test case - Foo<float>
:test-result: PASS A Template product test case - Foo<int>
:test-result: PASS A Template product test case - std::vector<float>
Expand Down
4 changes: 2 additions & 2 deletions tests/SelfTest/Baselines/compact.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ Class.tests.cpp:<line number>: failed: Nttp_Fixture<V>::value == 0 for: 6 == 0
Class.tests.cpp:<line number>: passed: Nttp_Fixture<V>::value > 0 for: 1 > 0
Class.tests.cpp:<line number>: passed: Nttp_Fixture<V>::value > 0 for: 3 > 0
Class.tests.cpp:<line number>: passed: Nttp_Fixture<V>::value > 0 for: 6 > 0
Class.tests.cpp:<line number>: failed: m_a == 2 for: 1 == 2
Class.tests.cpp:<line number>: passed: m_a == 1 for: 1 == 1
Class.tests.cpp:<line number>: passed: m_a++ == 0 for: 0 == 0
Class.tests.cpp:<line number>: failed: m_a == 0 for: 1 == 0
Class.tests.cpp:<line number>: passed: m_a++ == 0 for: 0 == 0
Class.tests.cpp:<line number>: passed: m_a == 1 for: 1 == 1
Class.tests.cpp:<line number>: failed: m_a == 2 for: 1 == 2
Class.tests.cpp:<line number>: passed: m_a == 1 for: 1 == 1
Misc.tests.cpp:<line number>: passed: x.size() == 0 for: 0 == 0
Misc.tests.cpp:<line number>: passed: x.size() == 0 for: 0 == 0
Misc.tests.cpp:<line number>: passed: x.size() == 0 for: 0 == 0
Expand Down
4 changes: 2 additions & 2 deletions tests/SelfTest/Baselines/compact.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ Class.tests.cpp:<line number>: failed: Nttp_Fixture<V>::value == 0 for: 6 == 0
Class.tests.cpp:<line number>: passed: Nttp_Fixture<V>::value > 0 for: 1 > 0
Class.tests.cpp:<line number>: passed: Nttp_Fixture<V>::value > 0 for: 3 > 0
Class.tests.cpp:<line number>: passed: Nttp_Fixture<V>::value > 0 for: 6 > 0
Class.tests.cpp:<line number>: failed: m_a == 2 for: 1 == 2
Class.tests.cpp:<line number>: passed: m_a == 1 for: 1 == 1
Class.tests.cpp:<line number>: passed: m_a++ == 0 for: 0 == 0
Class.tests.cpp:<line number>: failed: m_a == 0 for: 1 == 0
Class.tests.cpp:<line number>: passed: m_a++ == 0 for: 0 == 0
Class.tests.cpp:<line number>: passed: m_a == 1 for: 1 == 1
Class.tests.cpp:<line number>: failed: m_a == 2 for: 1 == 2
Class.tests.cpp:<line number>: passed: m_a == 1 for: 1 == 1
Misc.tests.cpp:<line number>: passed: x.size() == 0 for: 0 == 0
Misc.tests.cpp:<line number>: passed: x.size() == 0 for: 0 == 0
Misc.tests.cpp:<line number>: passed: x.size() == 0 for: 0 == 0
Expand Down
14 changes: 7 additions & 7 deletions tests/SelfTest/Baselines/console.std.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -287,27 +287,27 @@ with expansion:
6 == 0

-------------------------------------------------------------------------------
A TEST_CASE_FIXTURE based test run that fails
Second partial run
A TEST_CASE_METHOD based test run that fails
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................

Class.tests.cpp:<line number>: FAILED:
REQUIRE( m_a == 0 )
REQUIRE( m_a == 2 )
with expansion:
1 == 0
1 == 2

-------------------------------------------------------------------------------
A TEST_CASE_METHOD based test run that fails
A TEST_CASE_PERSISTENT_FIXTURE based test run that fails
Second partial run
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................

Class.tests.cpp:<line number>: FAILED:
REQUIRE( m_a == 2 )
REQUIRE( m_a == 0 )
with expansion:
1 == 2
1 == 0

-------------------------------------------------------------------------------
A couple of nested sections followed by a failure
Expand Down
40 changes: 20 additions & 20 deletions tests/SelfTest/Baselines/console.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2007,31 +2007,29 @@ with expansion:
6 > 0

-------------------------------------------------------------------------------
A TEST_CASE_FIXTURE based test run that fails
First partial run
A TEST_CASE_METHOD based test run that fails
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................

Class.tests.cpp:<line number>: PASSED:
REQUIRE( m_a++ == 0 )
Class.tests.cpp:<line number>: FAILED:
REQUIRE( m_a == 2 )
with expansion:
0 == 0
1 == 2

-------------------------------------------------------------------------------
A TEST_CASE_FIXTURE based test run that fails
Second partial run
A TEST_CASE_METHOD based test run that succeeds
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................

Class.tests.cpp:<line number>: FAILED:
REQUIRE( m_a == 0 )
Class.tests.cpp:<line number>: PASSED:
REQUIRE( m_a == 1 )
with expansion:
1 == 0
1 == 1

-------------------------------------------------------------------------------
A TEST_CASE_FIXTURE based test run that succeeds
A TEST_CASE_PERSISTENT_FIXTURE based test run that fails
First partial run
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
Expand All @@ -2043,30 +2041,32 @@ with expansion:
0 == 0

-------------------------------------------------------------------------------
A TEST_CASE_FIXTURE based test run that succeeds
A TEST_CASE_PERSISTENT_FIXTURE based test run that fails
Second partial run
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................

Class.tests.cpp:<line number>: PASSED:
REQUIRE( m_a == 1 )
Class.tests.cpp:<line number>: FAILED:
REQUIRE( m_a == 0 )
with expansion:
1 == 1
1 == 0

-------------------------------------------------------------------------------
A TEST_CASE_METHOD based test run that fails
A TEST_CASE_PERSISTENT_FIXTURE based test run that succeeds
First partial run
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................

Class.tests.cpp:<line number>: FAILED:
REQUIRE( m_a == 2 )
Class.tests.cpp:<line number>: PASSED:
REQUIRE( m_a++ == 0 )
with expansion:
1 == 2
0 == 0

-------------------------------------------------------------------------------
A TEST_CASE_METHOD based test run that succeeds
A TEST_CASE_PERSISTENT_FIXTURE based test run that succeeds
Second partial run
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................
Expand Down
40 changes: 20 additions & 20 deletions tests/SelfTest/Baselines/console.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2005,31 +2005,29 @@ with expansion:
6 > 0

-------------------------------------------------------------------------------
A TEST_CASE_FIXTURE based test run that fails
First partial run
A TEST_CASE_METHOD based test run that fails
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................

Class.tests.cpp:<line number>: PASSED:
REQUIRE( m_a++ == 0 )
Class.tests.cpp:<line number>: FAILED:
REQUIRE( m_a == 2 )
with expansion:
0 == 0
1 == 2

-------------------------------------------------------------------------------
A TEST_CASE_FIXTURE based test run that fails
Second partial run
A TEST_CASE_METHOD based test run that succeeds
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................

Class.tests.cpp:<line number>: FAILED:
REQUIRE( m_a == 0 )
Class.tests.cpp:<line number>: PASSED:
REQUIRE( m_a == 1 )
with expansion:
1 == 0
1 == 1

-------------------------------------------------------------------------------
A TEST_CASE_FIXTURE based test run that succeeds
A TEST_CASE_PERSISTENT_FIXTURE based test run that fails
First partial run
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
Expand All @@ -2041,30 +2039,32 @@ with expansion:
0 == 0

-------------------------------------------------------------------------------
A TEST_CASE_FIXTURE based test run that succeeds
A TEST_CASE_PERSISTENT_FIXTURE based test run that fails
Second partial run
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................

Class.tests.cpp:<line number>: PASSED:
REQUIRE( m_a == 1 )
Class.tests.cpp:<line number>: FAILED:
REQUIRE( m_a == 0 )
with expansion:
1 == 1
1 == 0

-------------------------------------------------------------------------------
A TEST_CASE_METHOD based test run that fails
A TEST_CASE_PERSISTENT_FIXTURE based test run that succeeds
First partial run
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................

Class.tests.cpp:<line number>: FAILED:
REQUIRE( m_a == 2 )
Class.tests.cpp:<line number>: PASSED:
REQUIRE( m_a++ == 0 )
with expansion:
1 == 2
0 == 0

-------------------------------------------------------------------------------
A TEST_CASE_METHOD based test run that succeeds
A TEST_CASE_PERSISTENT_FIXTURE based test run that succeeds
Second partial run
-------------------------------------------------------------------------------
Class.tests.cpp:<line number>
...............................................................................
Expand Down
Loading

0 comments on commit bf4575a

Please sign in to comment.