Beginning unit test
| Previous: Intermediate classes | Next: Intermediate unit test |
Required course material for the lesson
Powerpoint: Testing
Online: pytest documentation
Resource: Example code - Unit test
Resource: Unit test - start of reverse polish notation class
Installation of pytest
If you are using an anaconda python installation pytest is included. You can test in your python has pytest.
# On command line both should work or none python3 -c "import pytest" which pytest # It is pretty clear if you fail. Success can be more obscure. # Here is a fail test, so you can compare python3 -c "import yptest" which yptest
To install pytest, you need to be root/administrator.
# An install on a basic system pip install -U pytest # or maybe (depending on your system) sudo pip install -U pytest # Using ubuntu, which is also WSL in this situation, there is a package apt install python3-pytest
Subjects covered
Overview of test methods
Unit test using pytest framework.
Exercises to be handed in
You should make a special folder for the exercises. I will refer to my special folder as unittest in these exercises. You will also see some __pycache__ folders appear in places. This is Pythons cache for "compiled" programs. It is safe to ignore and also to delete, because it may become outdated.
In some of these exercise you need to hand in not just a single file, but multiple files in a folder hierarchy. Zip the folder structure for your entire solution folder. Make sure it is clear to see what exercise your code/data is solving. And learn to zip :-)
- Use your normalize function from exercise 3 in Functions, namespace, memory management. If you did not do so already, change it to use exceptions instead of sys.exit(), when an error occurs. Now make simple unit tests for the following test cases: a list of positive numbers, a list of negative numbers, a list of integers, a list of mixed floats and integers, a single number in the list, empty list, a list with at least one non-number (string, list, set), a set containing numbers, and a dict where the keys are numbers. Get other ideas if you can. The normalize function and all test functions must be in one single file (normalize_test.py in unittest), which you can run pytest on.
- Now remove the normalize function from normalize_test.py and put it in its own file normalize.py. Import it from the normalize_test.py like from normalize import normalize. The first normalize is the name of the .py file, the second normalize is the name of your normalize function. Just run pytest (no file name) in the folder to check it works. It is more normal to have test and function separated.
- Above we removed test code from function code by creating two files. Next, put the files in their own folder in unittest. I would put my normalize.py in unittest/src and normalize_test.py in unittest/test. This way there is a very clear separation between function and test. The problem is making sure the test code loads the function code. Do it wrong a couple of times - it is very instructive for how the search path works. Try to run the test from a different folder (unittest), like "pytest test/normalize_test.py" to see how vulnerable just using the path is.
- You hopefully remember the MyMath class you made in exercise 1 & 2 in Beginning classes. Make a folder classes in your unittest/src folder and put the MyMath class in a file MyMath.py in unittest/src/classes. Now make a unit test file MyMath_factorial_test.py in unittest/test that - surprise - tests your factorial method. Test it with at least these input: 12, 2, 1, 0, -1, 3.0, 3.4, "3", "3.1.", "ABC". Parametrize at least the tests that should succeed. Bonus for parametrizing the exceptions.
- MyMath class also contains the supply method. Make unit tests for that method in the file unittest/test/MyMath_supply_test.py. Make the "usual" tests, which are the same as you did in the first exercise.
- Now make unit tests for the average method in MyMath. Make the tests in the file unittest/test/MyMath_average_test.py. Notice how that interacts with the supply unit tests because average depends on correct working of supply. Sometimes you won't even find the flaw in one method before you test another that depends on it. I am not going to tell you this time what unit tests you should make. You should know by now.