Beginning unit test

From 22118
Jump to navigation Jump to search
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

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 :-)

  1. 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.

  2. 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.

  3. 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.

  4. Follow the file structure of having a code (or src) folder for programs, a test folder for tests, and now a testdata folder for files containing test data. Now make unit tests and appropriate test data files for your fasta class from last week. In this exercise you just need to make unit test for the method load. You need to hand in both tests and test data.

  5. Add unit tests for the method save in your fasta class. Hand in same way as above.

  6. Add unit tests for the method delete in your fasta class. Hand in same way as above.

I would not be surprised if you find errors in your fasta class based on these tests. I found flaws in my code.

Exercises for extra practice

  1. Add unit tests for all methods in your fasta class. That will be a bit of work.