Debian Codegraph @

3rdparty/catch2/docs/test-fixtures.md

raw symbols SCIP
1 <a id="top"></a>
2 # Test fixtures
3
4 ## Defining test fixtures
5
6 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. You define the test fixture as a simple structure:
7
8 ```c++
9 class UniqueTestsFixture {
10 private:
11 static int uniqueID;
12 protected:
13 DBConnection conn;
14 public:
15 UniqueTestsFixture() : conn(DBConnection::createConnection("myDB")) {
16 }
17 protected:
18 int getID() {
19 return ++uniqueID;
20 }
21 };
22
23 int UniqueTestsFixture::uniqueID = 0;
24
25 TEST_CASE_METHOD(UniqueTestsFixture, "Create Employee/No Name", "[create]") {
26 REQUIRE_THROWS(conn.executeSQL("INSERT INTO employee (id, name) VALUES (?, ?)", getID(), ""));
27 }
28 TEST_CASE_METHOD(UniqueTestsFixture, "Create Employee/Normal", "[create]") {
29 REQUIRE(conn.executeSQL("INSERT INTO employee (id, name) VALUES (?, ?)", getID(), "Joe Bloggs"));
30 }
31 ```
32
33 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.
34
35
36 Catch2 also provides `TEMPLATE_TEST_CASE_METHOD` and
37 `TEMPLATE_PRODUCT_TEST_CASE_METHOD` that can be used together
38 with templated fixtures and templated template fixtures to perform
39 tests for multiple different types. Unlike `TEST_CASE_METHOD`,
40 `TEMPLATE_TEST_CASE_METHOD` and `TEMPLATE_PRODUCT_TEST_CASE_METHOD` do
41 require the tag specification to be non-empty, as it is followed by
42 further macro arguments.
43
44 Also note that, because of limitations of the C++ preprocessor, if you
45 want to specify a type with multiple template parameters, you need to
46 enclose it in parentheses, e.g. `std::map<int, std::string>` needs to be
47 passed as `(std::map<int, std::string>)`.
48 In the case of `TEMPLATE_PRODUCT_TEST_CASE_METHOD`, if a member of the
49 type list should consist of more than single type, it needs to be enclosed
50 in another pair of parentheses, e.g. `(std::map, std::pair)` and
51 `((int, float), (char, double))`.
52
53 Example:
54 ```cpp
55 template< typename T >
56 struct Template_Fixture {
57 Template_Fixture(): m_a(1) {}
58
59 T m_a;
60 };
61
62 TEMPLATE_TEST_CASE_METHOD(Template_Fixture,
63 "A TEMPLATE_TEST_CASE_METHOD based test run that succeeds",
64 "[class][template]",
65 int, float, double) {
66 REQUIRE( Template_Fixture<TestType>::m_a == 1 );
67 }
68
69 template<typename T>
70 struct Template_Template_Fixture {
71 Template_Template_Fixture() {}
72
73 T m_a;
74 };
75
76 template<typename T>
77 struct Foo_class {
78 size_t size() {
79 return 0;
80 }
81 };
82
83 TEMPLATE_PRODUCT_TEST_CASE_METHOD(Template_Template_Fixture,
84 "A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test succeeds",
85 "[class][template]",
86 (Foo_class, std::vector),
87 int) {
88 REQUIRE( Template_Template_Fixture<TestType>::m_a.size() == 0 );
89 }
90 ```
91
92 _While there is an upper limit on the number of types you can specify
93 in single `TEMPLATE_TEST_CASE_METHOD` or `TEMPLATE_PRODUCT_TEST_CASE_METHOD`,
94 the limit is very high and should not be encountered in practice._
95
96 ## Signature-based parametrised test fixtures
97
98 > [Introduced](https://github.com/catchorg/Catch2/issues/1609) in Catch2 2.8.0.
99
100 Catch2 also provides `TEMPLATE_TEST_CASE_METHOD_SIG` and `TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG` to support
101 fixtures using non-type template parameters. These test cases work similar to `TEMPLATE_TEST_CASE_METHOD` and `TEMPLATE_PRODUCT_TEST_CASE_METHOD`,
102 with additional positional argument for [signature](test-cases-and-sections.md#signature-based-parametrised-test-cases).
103
104 Example:
105 ```cpp
106 template <int V>
107 struct Nttp_Fixture{
108 int value = V;
109 };
110
111 TEMPLATE_TEST_CASE_METHOD_SIG(
112 Nttp_Fixture,
113 "A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds",
114 "[class][template][nttp]",
115 ((int V), V),
116 1, 3, 6) {
117 REQUIRE(Nttp_Fixture<V>::value > 0);
118 }
119
120 template<typename T>
121 struct Template_Fixture_2 {
122 Template_Fixture_2() {}
123
124 T m_a;
125 };
126
127 template< typename T, size_t V>
128 struct Template_Foo_2 {
129 size_t size() { return V; }
130 };
131
132 TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG(
133 Template_Fixture_2,
134 "A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that succeeds",
135 "[class][template][product][nttp]",
136 ((typename T, size_t S), T, S),
137 (std::array, Template_Foo_2),
138 ((int,2), (float,6))) {
139 REQUIRE(Template_Fixture_2<TestType>{}.m_a.size() >= 2);
140 }
141 ```
142
143 ## Template fixtures with types specified in template type lists
144
145 Catch2 also provides `TEMPLATE_LIST_TEST_CASE_METHOD` to support template fixtures with types specified in
146 template type lists like `std::tuple`, `boost::mpl::list` or `boost::mp11::mp_list`. This test case works the same as `TEMPLATE_TEST_CASE_METHOD`,
147 only difference is the source of types. This allows you to reuse the template type list in multiple test cases.
148
149 Example:
150 ```cpp
151 using MyTypes = std::tuple<int, char, double>;
152 TEMPLATE_LIST_TEST_CASE_METHOD(Template_Fixture,
153 "Template test case method with test types specified inside std::tuple",
154 "[class][template][list]",
155 MyTypes) {
156 REQUIRE( Template_Fixture<TestType>::m_a == 1 );
157 }
158 ```
159
160 ---
161
162 [Home](Readme.md#top)