Every now and then programs do not do what we intend; they have defects or bugs. (We discuss the impact bugs can have on research in Section ). Sometimes these bugs are easy to detect when running the program code. However, in larger software, bugs may be harder to detect. Which function causes the result to be wrong? In my experience, the bug is always found in the function that is assumed to be correct and not properly checked. Therefore, there is a need to systematically check that the function does what it should do. This can be automated. Check that with specific inputs the outcome value corresponds to what we know it should be. Code Example 8.13 shows the idea of this kind of testing. Function positive_sum returns the sum of all positive numbers in the list given as an attribute. We have manually computed the correct result and compared it with the actual result. The importance is thinking about cases where the program might not behave as expected and examining the output values with these.
In larger software projects, regression is an important source of errors. Regression means simply presenting new bugs in a software that previously did not have a bug. For example, changing how a function behaves - but forgetting to correctly implement these changes in all the places where the function was applied would create regression. Also refactoring code for clarity could introduce regression errors if the refactoring is not correct. Automated testing helps to detect these kinds of mistakes before they are brought into the software. Automated tests can be run before each commit to check that the commit does not include dangerous code. For both R and Python, there are several libraries that help in automating testing, such as PyTest and testhat.