Overview of Automated Testing
1. Overview of automated testing
Now, you will dive into the world of automated testing.2. Roadmap
Great work with the documentation! Now that you have it, let's explore what you can do with it!3. Automated Testing and the Development Workflow
As a loan officer at the prestigious PyBank, it's important that your code gives correct, predictable results. To this end, you decide to include automated testing in the development workflow. Automated testing is the practice of using software tools to execute predefined tests on a software application automatically. Automated testing helps guarantee that code behaves as expected, ensures that newly written code does not break previously written code, and facilitates continuous integration and deployment processes.4. The doctest library
You have several options for automated testing. The two that come to mind first are the `doctest` library and `pytest`. Let's cover the `doctest` library now and save `pytest` for later. Recall, docstrings are string literals that appear right after the definition of a function, method, class, or module. They are used to describe the purpose, usage, and behavior of the code. The `doctest` module allows you to test by running examples embedded in docstings. It extracts the examples from the docstrings, executes them, and compares the actual output to the expected output.5. Features of the doctest library
There are several features of the doctest library that make it particularly useful. Inline Testing allows you to write test cases directly in the docstrings of functions, methods, and classes. The doctest library is also simple to set up and use, making it easy to add tests to existing code. Documentation as Tests ensures that the documentation stays up to date with the code by testing the examples provided. Regression Testing is useful because it verifies that the code behaves as documented.6. Example Use
Let's look at a simple example of doctest before applying it to the finance case. To run doctest, you need to import it and run the `testmod` command to test the examples included in the docstring. In this example, `doctest` will run the area method with arguments 1 and 1. The expected result is the value 1. Do you think this code will pass or fail? If it fails, what is the error?7. Example Use
You see that the test did in fact fail. It expected 1, but got 2. It looks like the numbers should have been multiplied, not added.8. Example Use
You correct the code as shown here. Since everything is ok, there will be no error messages and no output.9. Let's practice!
Now practice with some examples from your calculators.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.