Automated tests help ensure code reliability and quickly catch regressions or bugs introduced during development.
Arithmetic Functions Overview
The table below summarizes the arithmetic functions, their operations, and examples:Testing the Add Function
We begin with a basic test of the add function. Below is the sample test code:Expanding Tests to Additional Functions
Next, add the other arithmetic operations to your calculations module. Below is an example implementation:If you are new to pytest, consider referring to the pytest documentation for more detailed guidance and best practices.
Introducing a Bug and Catching It with Tests
To illustrate the importance of automated testing, imagine that a colleague accidentally introduces a bug in the add function by adding an extra number. For instance, modifying the function to return 9 for add(5, 3) instead of 8. Running the tests in this scenario would produce output similar to the following:Always run your test suite after making changes to critical functions. Automated testing is key to maintaining code stability and quickly catching unwanted bugs.