mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-26 15:26:11 +01:00
Add documentation for templated tests
This commit is contained in:
parent
2d906a92cb
commit
00d4f5d3c6
@ -86,6 +86,65 @@ When any of these macros are used the console reporter recognises them and forma
|
|||||||
|
|
||||||
Other than the additional prefixes and the formatting in the console reporter these macros behave exactly as ```TEST_CASE```s and ```SECTION```s. As such there is nothing enforcing the correct sequencing of these macros - that's up to the programmer!
|
Other than the additional prefixes and the formatting in the console reporter these macros behave exactly as ```TEST_CASE```s and ```SECTION```s. As such there is nothing enforcing the correct sequencing of these macros - that's up to the programmer!
|
||||||
|
|
||||||
|
## Type parametrised test cases
|
||||||
|
|
||||||
|
In addition to `TEST_CASE`s, Catch2 also supports test cases parametrised
|
||||||
|
by type, in the form of `TEMPLATE_TEST_CASE`.
|
||||||
|
|
||||||
|
* **TEMPLATE_TEST_CASE(** _test name_ , _tags_, _type1_, _type2_, ..., _typen_ **)**
|
||||||
|
|
||||||
|
_test name_ and _tag_ are exactly the same as they are in `TEST_CASE`,
|
||||||
|
with the difference that the tag string must be provided (however, it
|
||||||
|
can be empty). _type1_ through _typen_ is the list of types for which
|
||||||
|
this test case should run, and, inside the test code, the current type
|
||||||
|
is available as the `TestType` type.
|
||||||
|
|
||||||
|
Because of limitations of the C++ preprocessor, if you want to specify
|
||||||
|
a type with multiple template parameters, you need to enclose it in
|
||||||
|
parentheses, e.g. `std::map<int, std::string>` needs to be passed as
|
||||||
|
`(std::map<int, std::string>)`.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```cpp
|
||||||
|
TEMPLATE_TEST_CASE( "vectors can be sized and resized", "[vector][template]", int, std::string, (std::tuple<int,float>) ) {
|
||||||
|
|
||||||
|
std::vector<TestType> v( 5 );
|
||||||
|
|
||||||
|
REQUIRE( v.size() == 5 );
|
||||||
|
REQUIRE( v.capacity() >= 5 );
|
||||||
|
|
||||||
|
SECTION( "resizing bigger changes size and capacity" ) {
|
||||||
|
v.resize( 10 );
|
||||||
|
|
||||||
|
REQUIRE( v.size() == 10 );
|
||||||
|
REQUIRE( v.capacity() >= 10 );
|
||||||
|
}
|
||||||
|
SECTION( "resizing smaller changes size but not capacity" ) {
|
||||||
|
v.resize( 0 );
|
||||||
|
|
||||||
|
REQUIRE( v.size() == 0 );
|
||||||
|
REQUIRE( v.capacity() >= 5 );
|
||||||
|
|
||||||
|
SECTION( "We can use the 'swap trick' to reset the capacity" ) {
|
||||||
|
std::vector<TestType> empty;
|
||||||
|
empty.swap( v );
|
||||||
|
|
||||||
|
REQUIRE( v.capacity() == 0 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SECTION( "reserving smaller does not change size or capacity" ) {
|
||||||
|
v.reserve( 0 );
|
||||||
|
|
||||||
|
REQUIRE( v.size() == 5 );
|
||||||
|
REQUIRE( v.capacity() >= 5 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
_While there is an upper limit on the number of types you can specify
|
||||||
|
in single `TEMPLATE_TEST_CASE`, the limit is very high and should not
|
||||||
|
be encountered in practice._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
[Home](Readme.md#top)
|
[Home](Readme.md#top)
|
||||||
|
@ -30,6 +30,36 @@ class UniqueTestsFixture {
|
|||||||
|
|
||||||
The two test cases here will create uniquely-named derived classes of UniqueTestsFixture and thus can access the `getID()` protected method and `conn` member variables. This ensures that both the test cases are able to create a DBConnection using the same method (DRY principle) and that any ID's created are unique such that the order that tests are executed does not matter.
|
The two test cases here will create uniquely-named derived classes of UniqueTestsFixture and thus can access the `getID()` protected method and `conn` member variables. This ensures that both the test cases are able to create a DBConnection using the same method (DRY principle) and that any ID's created are unique such that the order that tests are executed does not matter.
|
||||||
|
|
||||||
|
|
||||||
|
Catch2 also provides `TEMPLATE_TEST_CASE_METHOD` that can be used together
|
||||||
|
with templated fixtures to perform tests for multiple different types.
|
||||||
|
However, unlike `TEST_CASE_METHOD`, `TEMPLATE_TEST_CASE_METHOD` requires
|
||||||
|
the tag specification to be non-empty, as it is followed by further macros
|
||||||
|
arguments.
|
||||||
|
|
||||||
|
Also note that, because of limitations of the C++ preprocessor, if you
|
||||||
|
want to specify a type with multiple template parameters, you need to
|
||||||
|
enclose it in parentheses, e.g. `std::map<int, std::string>` needs to be
|
||||||
|
passed as `(std::map<int, std::string>)`.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```cpp
|
||||||
|
template< typename T >
|
||||||
|
struct Template_Fixture {
|
||||||
|
Template_Fixture(): m_a(1) {}
|
||||||
|
|
||||||
|
T m_a;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEMPLATE_TEST_CASE_METHOD(Template_Fixture,"A TEMPLATE_TEST_CASE_METHOD based test run that succeeds", "[class][template]", int, float, double) {
|
||||||
|
REQUIRE( Template_Fixture<TestType>::m_a == 1 );
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
_While there is an upper limit on the number of types you can specify
|
||||||
|
in single `TEMPLATE_TEST_CASE`, the limit is very high and should not
|
||||||
|
be encountered in practice._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
[Home](Readme.md#top)
|
[Home](Readme.md#top)
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
[Test cases and sections](#test-cases-and-sections)<br>
|
[Test cases and sections](#test-cases-and-sections)<br>
|
||||||
[BDD-Style](#bdd-style)<br>
|
[BDD-Style](#bdd-style)<br>
|
||||||
[Scaling up](#scaling-up)<br>
|
[Scaling up](#scaling-up)<br>
|
||||||
|
[Type parametrised test cases](#type-parametrised-test-cases)<br>
|
||||||
[Next steps](#next-steps)<br>
|
[Next steps](#next-steps)<br>
|
||||||
|
|
||||||
## Getting Catch2
|
## Getting Catch2
|
||||||
@ -256,6 +257,16 @@ In fact it is usually a good idea to put the block with the ```#define``` [in it
|
|||||||
Do not write your tests in header files!
|
Do not write your tests in header files!
|
||||||
|
|
||||||
|
|
||||||
|
## Type parametrised test cases
|
||||||
|
|
||||||
|
Test cases in Catch2 can be also parametrised by type, via the
|
||||||
|
`TEMPLATE_TEST_CASE` macro, which behaves in the same way the `TEST_CASE`
|
||||||
|
macro, but is run for every type.
|
||||||
|
|
||||||
|
For more details, see our documentation on [test cases and sections]
|
||||||
|
(test-cases-and-sections.md#type-parametrised-test-cases).
|
||||||
|
|
||||||
|
|
||||||
## Next steps
|
## Next steps
|
||||||
|
|
||||||
This has been a brief introduction to get you up and running with Catch, and to point out some of the key differences between Catch and other frameworks you may already be familiar with. This will get you going quite far already and you are now in a position to dive in and write some tests.
|
This has been a brief introduction to get you up and running with Catch, and to point out some of the key differences between Catch and other frameworks you may already be familiar with. This will get you going quite far already and you are now in a position to dive in and write some tests.
|
||||||
|
Loading…
Reference in New Issue
Block a user