Section 5.12 Testing Wrapup
The special doctest unit tests have a few advantages over the handwritten tests we first saw:
-
We do not have to read the output to decide if each test is correct - the testing code does that for us and prints extra information for each test that fails.
-
Tools can be used to automate the tests and parse the results as doctest will always use a consistent output format.
-
If we want, we can specify that only some of the tests get run. In a large program with thousands of tests it can be useful to say βjust run the heronsFormula test right nowβ.
-
The
doctest::Approx
feature is very useful for testing double values.
For now, we have to decide if a program is going to have tests, or a
main
function that does some other work. Later we will learn how to make a project that has both a test program and a βrealβ program.
Unit testing is just one type of testing. There are many other types of testing, such as integration testing, system testing, and acceptance testing. Each type of testing has its own purpose and its own techniques. But unit testing is a good place to start. It is easy to do, and it can catch many bugs early in the development process. Many of the exercises in the rest of this book will ask you to implement a function and then test the behavior of that function with unit tests.
You have attempted of activities on this page.