Skip to content

Commit fa0ca85

Browse files
committed
Catch -> Catch2
1 parent a0e4b85 commit fa0ca85

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

08-Unit-testing.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ Unit testing is widely used in industry because it is quite effective at keeping
3838
You can even measure how much of your code is tested by unit tests --- 100% code coverage means that you've found
3939
at least most of the obvious bugs!
4040

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++.
4242
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.
4444
(It's also popular, in case you were wondering.)
4545

4646
### Takeaways
4747

48-
- Learn how to write unit tests with Catch
48+
- Learn how to write unit tests with Catch2
4949
- Organize your code and tests to preserve your sanity
5050
- Measure how much of your code is covered by the tests you've written
5151

5252
## Walkthrough
5353

54-
### Setting up Catch
54+
### Setting up Catch2
5555

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`.
5858

5959
In *exactly one* `.cpp` file, you must include the following lines:
6060

@@ -66,7 +66,7 @@ In *exactly one* `.cpp` file, you must include the following lines:
6666
This generates a `main()` function that runs your unit tests.
6767
You will have two programs now --- your actual program, and a program that runs your unit tests.
6868

69-
Every other file you write tests in should include Catch:
69+
Every other file you write tests in should include Catch2:
7070

7171
```c++
7272
#include "catch.hpp"
@@ -95,11 +95,11 @@ int fibonacci(int n)
9595
}
9696
```
9797
98-
In Catch, every test lives inside a `TEST_CASE` block.
98+
In Catch2, every test lives inside a `TEST_CASE` block.
9999
You can make these as fine-grained as you want, but generally you'll find it easy to collect a bunch of related checks
100100
into one `TEST_CASE`.
101101
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.)
103103
104104
Inside a test case, you can put one or more `REQUIRE` or `CHECK` assertions.
105105
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:
153153
All tests passed (4 assertions in 1 test case)
154154
```
155155

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.
157157
It does this by the power of *template magic*.
158158
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.
160160
For 'and' statements, rather than `CHECK(x && y);`, write `CHECK(x); CHECK(y);`.
161161
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.)
163163

164164
There are also matching assertions `REQUIRE_FALSE` and `CHECK_FALSE` that check to make sure a statement is false, rather than true.
165165

@@ -188,7 +188,7 @@ int fibonacci(int n)
188188
}
189189
```
190190
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.
192192
As before, each assertion comes in a `CHECK` and a `REQUIRE` flavor.
193193
194194
- `CHECK_NOTHROW(expression)`: Asserts the expression does not throw an exception.
@@ -215,7 +215,7 @@ TEST_CASE("Fibonacci Domain", "[Fibonacci]")
215215
At this point you know enough to start writing tests for functions.
216216
Before you go too hog--wild, shoving test cases every which where, let's talk about how to organize tests so they're easy to find and use.
217217

218-
First, we can't have our `main()` function and Catch's auto-generated `main()` in the same program.
218+
First, we can't have our `main()` function and Catch2's auto-generated `main()` in the same program.
219219
You'll need to organize your code so that you can compile your test cases without including your `main()` function.
220220
So make your program's `main()` as small as possible and have it call other functions that can be unit tested.
221221

@@ -226,10 +226,10 @@ then make a separate test file for each implementation file.
226226
For example, if we made `fibonacci.h` and `fibonacci.cpp` files for our function above, we'd also make a `test_fibonacci.cpp` file
227227
that contains our unit tests.
228228

229-
Third, compiling Catch's auto-generated `main()` function takes a while.
229+
Third, compiling Catch2's auto-generated `main()` function takes a while.
230230
This is doubly annoying because it never changes!
231231
Rather than rebuilding it all the time, we can harness the power of makefiles and incremental compilation by making a separate `test_main.cpp` file
232-
that just contains Catch's `main()`.
232+
that just contains Catch2's `main()`.
233233
This file looks exactly like this:
234234

235235
```{.cpp .numberLines}
@@ -248,7 +248,7 @@ Then in `test_fibonacci.cpp`, we just have the following includes:
248248
Building this code is done as follows:
249249

250250
```
251-
$ g++ -c test_main.cpp # Compile Catch's main()
251+
$ g++ -c test_main.cpp # Compile Catch2's main()
252252
$ g++ -c test_fibonacci.cpp # Compile Fibonacci tests
253253
$ g++ test_main.o test_fibonacci.o -o testsuite # Link testsuite
254254
```
@@ -301,7 +301,7 @@ class Vector
301301
To test the `[]` operator or the copy constructor, we need to make a vector that contains elements to access or copy.
302302
You could write a bunch of test cases and duplicate the same test setup code in each, but there is a better option!
303303
Each `TEST_CASE` can be split into multiple `SECTION`s, each of which has a name.
304-
For each section, Catch runs the test case from the beginning but only executes one section each run.
304+
For each section, Catch2 runs the test case from the beginning but only executes one section each run.
305305
306306
We can use this to set up a test vector once to test the constructor and accessor functions:
307307
@@ -344,7 +344,7 @@ TEST_CASE("Vector Elements", "[vector]")
344344
}
345345
```
346346

347-
In this example, Catch runs lines 1--16, then starts over and runs lines 1--8 and 18--26, then lines 1--8, 18, and 28--35.
347+
In this example, Catch2 runs lines 1--16, then starts over and runs lines 1--8 and 18--26, then lines 1--8, 18, and 28--35.
348348
Since we get a fresh `v` vector for each section, the code inside each section can mutate `v` however it likes without impacting any of the other
349349
sections' tests!
350350
Even better, we can add more setup as we go through the test case; our `copy` vector is only created for the sections that test the copy constructor.
@@ -355,11 +355,11 @@ test assertions.
355355

356356
### Advanced Tests
357357

358-
Catch provides some advanced features that come in handy when testing code that uses strings and floating point arithmetic.
358+
Catch2 provides some advanced features that come in handy when testing code that uses strings and floating point arithmetic.
359359

360360
When testing code that produces strings, sometimes you do not know what the entire string produced will be,
361361
but you want to check that it contains some particular substring.
362-
Catch offers a pair of assertions, `CHECK_THAT` and `REQUIRE_THAT`, that use a *matcher* to check only parts of strings.
362+
Catch2 offers a pair of assertions, `CHECK_THAT` and `REQUIRE_THAT`, that use a *matcher* to check only parts of strings.
363363
For instance, we can test that a string starts with "Dear Prudence" like so:
364364

365365
```
@@ -377,7 +377,7 @@ REQUIRE_THAT(my_string, StartsWith("Dear Prudence") &&
377377
These matchers can also be used in the `THROWS_WITH` assertions!
378378

379379
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));`.
381381
By default, the comparison can be off by 0.001%, but you can change this!
382382
For a more precise comparison, you can set the `epsilon` to a smaller percentage: `CHECK(PI = Approx(3.1415).epsilon(0.0001));`.
383383

@@ -541,11 +541,11 @@ Floating Point:
541541
542542
## Further Reading
543543
544-
- [Catch Tutorial](https://github.com/philsquared/Catch/blob/master/docs/tutorial.md)
545-
- [Catch Manual](https://github.com/philsquared/Catch/blob/master/docs/Readme.md)
546-
- [Floating Point Comparisons](https://github.com/philsquared/Catch/blob/master/docs/assertions.md#floating-point-comparisons)
547-
- [Matcher Expressions](https://github.com/philsquared/Catch/blob/master/docs/matchers.md)
548-
- [Catch GitHub Repository](https://github.com/philsquared/Catch)
544+
- [Catch2 Tutorial](https://github.com/catchorg/Catch2/blob/master/docs/tutorial.md#top)
545+
- [Catch2 Manual](https://github.com/catchorg/Catch2/blob/master/docs/Readme.md#top)
546+
- [Floating Point Comparisons](https://github.com/catchorg/Catch2/blob/master/docs/assertions.md#floating-point-comparisons)
547+
- [Matcher Expressions](https://github.com/catchorg/Catch2/blob/master/docs/matchers.md#top)
548+
- [Catch2 GitHub Repository](https://github.com/catchorg/Catch2)
549549
550550
<!-- -->
551551

0 commit comments

Comments
 (0)