Beginning unit test: Difference between revisions
| Line 34: | Line 34: | ||
# 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.<br><br> | # 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.<br><br> | ||
# 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.<br><br> | # 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.<br><br> | ||
== Exercises for extra practice == | == Exercises for extra practice == | ||
# Add unit tests for all methods in your '''fasta''' class. That will be a bit of work. | # Add unit tests for all methods in your '''fasta''' class. That will be a bit of work. | ||
Revision as of 16:15, 14 April 2026
| 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.
pip install -U pytest # or maybe (depending on your system) sudo pip install -U 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.
Exercises for extra practice
- Add unit tests for all methods in your fasta class. That will be a bit of work.