You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 08-Unit-testing.md
+27-27Lines changed: 27 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -38,23 +38,23 @@ Unit testing is widely used in industry because it is quite effective at keeping
38
38
You can even measure how much of your code is tested by unit tests --- 100% code coverage means that you've found
39
39
at least most of the obvious bugs!
40
40
41
-
This chapter will focus on the Catch unit testing framework for C++.
41
+
This chapter will focus on the Catch2 unit testing framework for C++.
42
42
There are a number of popular unit testing frameworks; Boost has one,[^sink] Google makes one called `gtest`, etc.
43
-
However, Catch is easy to install, easy to write tests in, and downright beautiful compared to Boost's test framework.
43
+
However, Catch2 is easy to install, easy to write tests in, and downright beautiful compared to Boost's test framework.
44
44
(It's also popular, in case you were wondering.)
45
45
46
46
### Takeaways
47
47
48
-
- Learn how to write unit tests with Catch
48
+
- Learn how to write unit tests with Catch2
49
49
- Organize your code and tests to preserve your sanity
50
50
- Measure how much of your code is covered by the tests you've written
51
51
52
52
## Walkthrough
53
53
54
-
### Setting up Catch
54
+
### Setting up Catch2
55
55
56
-
Catch is distributed as a single `.hpp` file that you can download and include in your project.
57
-
Download it from [GitHub](https://github.com/philsquared/Catch) --- the link to the single header is in the `README`.
56
+
Catch2 is distributed as a single `.hpp` file that you can download and include in your project.
57
+
Download it from [GitHub](https://github.com/catchorg/Catch2) --- the link to the single header is in the `README`.
58
58
59
59
In *exactly one*`.cpp` file, you must include the following lines:
60
60
@@ -66,7 +66,7 @@ In *exactly one* `.cpp` file, you must include the following lines:
66
66
This generates a `main()` function that runs your unit tests.
67
67
You will have two programs now --- your actual program, and a program that runs your unit tests.
68
68
69
-
Every other file you write tests in should include Catch:
69
+
Every other file you write tests in should include Catch2:
70
70
71
71
```c++
72
72
#include"catch.hpp"
@@ -95,11 +95,11 @@ int fibonacci(int n)
95
95
}
96
96
```
97
97
98
-
In Catch, every test lives inside a `TEST_CASE` block.
98
+
In Catch2, every test lives inside a `TEST_CASE` block.
99
99
You can make these as fine-grained as you want, but generally you'll find it easy to collect a bunch of related checks
100
100
into one `TEST_CASE`.
101
101
Each test case has a name and a tag; generally you'll tag all test cases for a function/class with the same tag.
102
-
(You can tell Catch to run only tests with specific names or tags if you like.)
102
+
(You can tell Catch2 to run only tests with specific names or tags if you like.)
103
103
104
104
Inside a test case, you can put one or more `REQUIRE` or `CHECK` assertions.
105
105
A `REQUIRE` statement checks that a certain condition holds; if it does not, it reports a test failure and stops the execution of that test case.
@@ -153,13 +153,13 @@ If we fix that and re--run our tests, everything is kosher:
153
153
All tests passed (4 assertions in 1 test case)
154
154
```
155
155
156
-
Now, you may notice that Catch expands the thing inside the `CHECK` function --- it prints the value that `fibonacci` returns.
156
+
Now, you may notice that Catch2 expands the thing inside the `CHECK` function --- it prints the value that `fibonacci` returns.
157
157
It does this by the power of *template magic*.
158
158
This magic is only so powerful.[^errors]
159
-
So, if you want to write a more complex expression, you'll need to either break it into individual assertions or tell Catch to not attempt to expand it.
159
+
So, if you want to write a more complex expression, you'll need to either break it into individual assertions or tell Catch2 to not attempt to expand it.
160
160
For 'and' statements, rather than `CHECK(x && y);`, write `CHECK(x); CHECK(y);`.
161
161
For 'or' statements, enclose your expression in an extra pair of parentheses: `CHECK((x || y));`.
162
-
(The extra parentheses tell Catch to not attempt to expand the expression; you can do this with 'and' statements as well, but expansion is nice to have.)
162
+
(The extra parentheses tell Catch2 to not attempt to expand the expression; you can do this with 'and' statements as well, but expansion is nice to have.)
163
163
164
164
There are also matching assertions `REQUIRE_FALSE` and `CHECK_FALSE` that check to make sure a statement is false, rather than true.
165
165
@@ -188,7 +188,7 @@ int fibonacci(int n)
188
188
}
189
189
```
190
190
191
-
Catch provides a number of assertions for testing whether expressions throw exceptions and what kinds of exceptions are thrown.
191
+
Catch2 provides a number of assertions for testing whether expressions throw exceptions and what kinds of exceptions are thrown.
192
192
As before, each assertion comes in a `CHECK` and a `REQUIRE` flavor.
193
193
194
194
- `CHECK_NOTHROW(expression)`: Asserts the expression does not throw an exception.
These matchers can also be used in the `THROWS_WITH` assertions!
378
378
379
379
Testing floating point code presents a challenge because floating point operations may have some round-off that prevents exact equality checks from working.
380
-
Catch provides a class named `Approx` that performs approximate equality checks; for instance, `CHECK(PI == Approx(3.14));`.
380
+
Catch2 provides a class named `Approx` that performs approximate equality checks; for instance, `CHECK(PI == Approx(3.14));`.
381
381
By default, the comparison can be off by 0.001%, but you can change this!
382
382
For a more precise comparison, you can set the `epsilon` to a smaller percentage: `CHECK(PI = Approx(3.1415).epsilon(0.0001));`.
0 commit comments