| 1 | <a id="top"></a> |
| 2 | # Tutorial |
| 3 | |
| 4 | **Contents**<br> |
| 5 | [Getting Catch2](#getting-catch2)<br> |
| 6 | [Writing tests](#writing-tests)<br> |
| 7 | [Test cases and sections](#test-cases-and-sections)<br> |
| 8 | [BDD style testing](#bdd-style-testing)<br> |
| 9 | [Data and Type driven tests](#data-and-type-driven-tests)<br> |
| 10 | [Next steps](#next-steps)<br> |
| 11 | |
| 12 | |
| 13 | ## Getting Catch2 |
| 14 | |
| 15 | Ideally you should be using Catch2 through its [CMake integration](cmake-integration.md#top). |
| 16 | Catch2 also provides pkg-config files and two file (header + cpp) |
| 17 | distribution, but this documentation will assume you are using CMake. If |
| 18 | you are using the two file distribution instead, remember to replace |
| 19 | the included header with `catch_amalgamated.hpp`. |
| 20 | |
| 21 | |
| 22 | ## Writing tests |
| 23 | |
| 24 | Let's start with a really simple example ([code](../examples/010-TestCase.cpp)). Say you have written a function to calculate factorials and now you want to test it (let's leave aside TDD for now). |
| 25 | |
| 26 | ```c++ |
| 27 | unsigned int Factorial( unsigned int number ) { |
| 28 | return number <= 1 ? number : Factorial(number-1)*number; |
| 29 | } |
| 30 | ``` |
| 31 | |
| 32 | ```c++ |
| 33 | #include <catch2/catch_test_macros.hpp> |
| 34 | |
| 35 | unsigned int Factorial( unsigned int number ) { |
| 36 | return number <= 1 ? number : Factorial(number-1)*number; |
| 37 | } |
| 38 | |
| 39 | TEST_CASE( "Factorials are computed", "[factorial]" ) { |
| 40 | REQUIRE( Factorial(1) == 1 ); |
| 41 | REQUIRE( Factorial(2) == 2 ); |
| 42 | REQUIRE( Factorial(3) == 6 ); |
| 43 | REQUIRE( Factorial(10) == 3628800 ); |
| 44 | } |
| 45 | ``` |
| 46 | |
| 47 | This will compile to a complete executable which responds to [command line arguments](command-line.md#top). If you just run it with no arguments it will execute all test cases (in this case there is just one), report any failures, report a summary of how many tests passed and failed and return the number of failed tests (useful for if you just want a yes/ no answer to: "did it work"). |
| 48 | |
| 49 | Anyway, as the tests above as written will pass, but there is a bug. |
| 50 | The problem is that `Factorial(0)` should return 1 (due to [its |
| 51 | definition](https://en.wikipedia.org/wiki/Factorial#Factorial_of_zero)). |
| 52 | Let's add that as an assertion to the test case: |
| 53 | |
| 54 | ```c++ |
| 55 | TEST_CASE( "Factorials are computed", "[factorial]" ) { |
| 56 | REQUIRE( Factorial(0) == 1 ); |
| 57 | REQUIRE( Factorial(1) == 1 ); |
| 58 | REQUIRE( Factorial(2) == 2 ); |
| 59 | REQUIRE( Factorial(3) == 6 ); |
| 60 | REQUIRE( Factorial(10) == 3628800 ); |
| 61 | } |
| 62 | ``` |
| 63 | |
| 64 | After another compile & run cycle, we will see a test failure. The output |
| 65 | will look something like: |
| 66 | |
| 67 | ``` |
| 68 | Example.cpp:9: FAILED: |
| 69 | REQUIRE( Factorial(0) == 1 ) |
| 70 | with expansion: |
| 71 | 0 == 1 |
| 72 | ``` |
| 73 | |
| 74 | Note that the output contains both the original expression, |
| 75 | `REQUIRE( Factorial(0) == 1 )` and the actual value returned by the call |
| 76 | to the `Factorial` function: `0`. |
| 77 | |
| 78 | We can fix this bug by slightly modifying the `Factorial` function to: |
| 79 | ```c++ |
| 80 | unsigned int Factorial( unsigned int number ) { |
| 81 | return number > 1 ? Factorial(number-1)*number : 1; |
| 82 | } |
| 83 | ``` |
| 84 | |
| 85 | |
| 86 | ### What did we do here? |
| 87 | |
| 88 | Although this was a simple test it's been enough to demonstrate a few |
| 89 | things about how Catch2 is used. Let's take a moment to consider those |
| 90 | before we move on. |
| 91 | |
| 92 | * We introduce test cases with the `TEST_CASE` macro. This macro takes |
| 93 | one or two string arguments - a free form test name and, optionally, |
| 94 | one or more tags (for more see [Test cases and Sections](#test-cases-and-sections)). |
| 95 | * The test automatically self-registers with the test runner, and user |
| 96 | does not have do anything more to ensure that it is picked up by the test |
| 97 | framework. _Note that you can run specific test, or set of tests, |
| 98 | through the [command line](command-line.md#top)._ |
| 99 | * The individual test assertions are written using the `REQUIRE` macro. |
| 100 | It accepts a boolean expression, and uses expression templates to |
| 101 | internally decompose it, so that it can be individually stringified |
| 102 | on test failure. |
| 103 | |
| 104 | On the last point, note that there are more testing macros available, |
| 105 | because not all useful checks can be expressed as a simple boolean |
| 106 | expression. As an example, checking that an expression throws an exception |
| 107 | is done with the `REQUIRE_THROWS` macro. More on that later. |
| 108 | |
| 109 | |
| 110 | ## Test cases and sections |
| 111 | |
| 112 | Like most test frameworks, Catch2 supports a class-based fixture mechanism, |
| 113 | where individual tests are methods on class and setup/teardown can be |
| 114 | done in constructor/destructor of the type. |
| 115 | |
| 116 | However, their use in Catch2 is rare, because idiomatic Catch2 tests |
| 117 | instead use _sections_ to share setup and teardown code between test code. |
| 118 | This is best explained through an example ([code](../examples/100-Fix-Section.cpp)): |
| 119 | |
| 120 | ```c++ |
| 121 | TEST_CASE( "vectors can be sized and resized", "[vector]" ) { |
| 122 | |
| 123 | std::vector<int> v( 5 ); |
| 124 | |
| 125 | REQUIRE( v.size() == 5 ); |
| 126 | REQUIRE( v.capacity() >= 5 ); |
| 127 | |
| 128 | SECTION( "resizing bigger changes size and capacity" ) { |
| 129 | v.resize( 10 ); |
| 130 | |
| 131 | REQUIRE( v.size() == 10 ); |
| 132 | REQUIRE( v.capacity() >= 10 ); |
| 133 | } |
| 134 | SECTION( "resizing smaller changes size but not capacity" ) { |
| 135 | v.resize( 0 ); |
| 136 | |
| 137 | REQUIRE( v.size() == 0 ); |
| 138 | REQUIRE( v.capacity() >= 5 ); |
| 139 | } |
| 140 | SECTION( "reserving bigger changes capacity but not size" ) { |
| 141 | v.reserve( 10 ); |
| 142 | |
| 143 | REQUIRE( v.size() == 5 ); |
| 144 | REQUIRE( v.capacity() >= 10 ); |
| 145 | } |
| 146 | SECTION( "reserving smaller does not change size or capacity" ) { |
| 147 | v.reserve( 0 ); |
| 148 | |
| 149 | REQUIRE( v.size() == 5 ); |
| 150 | REQUIRE( v.capacity() >= 5 ); |
| 151 | } |
| 152 | } |
| 153 | ``` |
| 154 | |
| 155 | For each `SECTION` the `TEST_CASE` is executed from the start. This means |
| 156 | that each section is entered with a freshly constructed vector `v`, that |
| 157 | we know has size 5 and capacity at least 5, because the two assertions |
| 158 | are also checked before the section is entered. Each run through a test |
| 159 | case will execute one, and only one, leaf section. |
| 160 | |
| 161 | Section can also be nested, in which case the parent section can be |
| 162 | entered multiple times, once for each leaf section. Nested sections are |
| 163 | most useful when you have multiple tests that share part of the set up. |
| 164 | To continue on the vector example above, you could add a check that |
| 165 | `std::vector::reserve` does not remove unused excess capacity, like this: |
| 166 | |
| 167 | ```cpp |
| 168 | SECTION( "reserving bigger changes capacity but not size" ) { |
| 169 | v.reserve( 10 ); |
| 170 | |
| 171 | REQUIRE( v.size() == 5 ); |
| 172 | REQUIRE( v.capacity() >= 10 ); |
| 173 | SECTION( "reserving down unused capacity does not change capacity" ) { |
| 174 | v.reserve( 7 ); |
| 175 | REQUIRE( v.size() == 5 ); |
| 176 | REQUIRE( v.capacity() >= 10 ); |
| 177 | } |
| 178 | } |
| 179 | ``` |
| 180 | |
| 181 | Another way to look at sections is that they are a way to define a tree |
| 182 | of paths through the test. Each section represents a node, and the final |
| 183 | tree is walked in depth-first manner, with each path only visiting only |
| 184 | one leaf node. |
| 185 | |
| 186 | There is no practical limit on nesting sections, as long as your compiler |
| 187 | can handle them, but keep in mind that overly nested sections can become |
| 188 | unreadable. From experience, having section nest more than 3 levels is |
| 189 | usually very hard to follow and not worth the removed duplication. |
| 190 | |
| 191 | |
| 192 | ## BDD style testing |
| 193 | |
| 194 | Catch2 also provides some basic support for BDD-style testing. There are |
| 195 | macro aliases for `TEST_CASE` and `SECTIONS` that you can use so that |
| 196 | the resulting tests read as BDD spec. `SCENARIO` acts as a `TEST_CASE` |
| 197 | with "Scenario: " name prefix. Then there are `GIVEN`, `WHEN`, `THEN` |
| 198 | (and their variants with `AND_` prefix), which act as a `SECTION`, |
| 199 | similarly prefixed with the macro name. |
| 200 | |
| 201 | For more details on the macros look at the [test cases and |
| 202 | sections](test-cases-and-sections.md#top) part of the reference docs, |
| 203 | or at the [vector example done with BDD macros](../examples/120-Bdd-ScenarioGivenWhenThen.cpp). |
| 204 | |
| 205 | |
| 206 | ## Data and Type driven tests |
| 207 | |
| 208 | Test cases in Catch2 can also be driven by types, input data, or both |
| 209 | at the same time. |
| 210 | |
| 211 | For more details look into the Catch2 reference, either at the |
| 212 | [type parametrized test cases](test-cases-and-sections.md#type-parametrised-test-cases), |
| 213 | or [data generators](generators.md#top). |
| 214 | |
| 215 | |
| 216 | ## Next steps |
| 217 | |
| 218 | This page is a brief introduction to get you up and running with Catch2, |
| 219 | and to show the basic features of Catch2. The features mentioned here |
| 220 | can get you quite far, but there are many more. However, you can read |
| 221 | about these as you go, in the ever-growing [reference section](Readme.md#top) |
| 222 | of the documentation. |
| 223 | |
| 224 | |
| 225 | --- |
| 226 | |
| 227 | [Home](Readme.md#top) |