Python Testing Design and Clarity Guide
Testing
- All functions require examples/test cases, including helper functions and testing functions
- Examples/test cases should reflect various input scenarios so that you exercise the possible main behaviors of your function. You do not need to test every input or every input combination, but rather enough to be confident that your function works with various inputs.
- Test the "edge cases" of functions. For example, functions that take in numeric inputs and work differently depending on the range that number is in should test the boundaries of those ranges.
- Even if your function does not work as expected, you can still write examples/test cases for it and receive full testing credit.
- If a function has a side effect (like modifying a list), test that side effect.
- If a function both has a side effect and returns information, test both the main return and the side effect.
- If a function fails in certain cases, test that failure (see below example on how to test errors).
- If a function modifies (or mutates) data, set up testing data rather than working on a global piece of data/dataset.
Setting up pytest
- To use pytest, you need to install the package first:
- Open a terminal in VS Code (
Terminal > New Terminal
menu)
- Install the pytest package in your terminal by running
pip3 install pytest
Note: If you get an error, you might have to run pip3 install --user pytest
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
-
Then, put import pytest
at the top of your Python test file.
-
All of your tests must be contained within functions that begin with test_
-
Example of good testing:
Design
- Use constants where appropriate.
- Use helper functions where appropriate.
- Make sure your helper functions and constants are not redundant.
There is no point in making this function:
since you could always use s.lower()
instead.
Clarity
-
Write docstrings for all functions, including helper and nested functions.
A good docstring gives a description of the function, including its input(s) and output. Ideally, by looking at the docstring you know what the function does and how to use it without looking at the function body itself.
You can include docstrings as a comment with """
just below your function signatures. E.g.
-
Give constants and helper functions useful names.
-
All functions require type annotations on inputs and output. You can omit output annotation for functions that have no return (for example functions that only need to print
).
-
Names of constants and functions should be lower case and underscore separated. For configuration constants (for example the height of a character) it is acceptable to use all caps names.
-
Keep lines under 80 characters.
-
Indent your code properly. If you are using Pycharm, this should be happening automatically. If you are not using Pycharm, use 4 spaces to indent your code (not the tab character) and keep arguments of functions lined up:
return
statements should be not be written like this:
but rather like this:
if
statements should be written with newlines: