Unit Testing
What is unit testing?
Unit testing simply verifies that individual units of code (mostly functions) work as expected. Usually you write the test cases yourself, but some can be automatically generated.
The unit testing should be done as often as possible. The most obvious benefit is knowing down the road that when a change is made, no other individual units of code were affected by it if they all pass the tests.
Example of testing Student
class
Here I’m going to make a very simple example to show how it works.
First, build the Student
class:
1 | class Student(): |
Then, build the unit testing script:
1 | import unittest |
Execution
Execute the script to make testing:
1 | python test_student.py |
to make the outut more verbose, add the -v
into the command.
1 | python test_student.py -v |
Reference
- blog of liaoxuefeng: https://www.liaoxuefeng.com/wiki/1016959663602400/1017604210683936
- doc of Python 3.8: https://docs.python.org/3/library/unittest.html