Unittest Module
The unittest
module provides features for unit testing Python code. Similar to unit testing frameworks in other languages ,unittest
allows for the following features:
- Automation of unit tests.
- Sharing of setup and tear-down code for tests.
- Aggregation of tests into collections.
Example
The following example begins with code for a Rectangle
class that needs to be tested:
# saved as rectangle.pyimport mathclass Rectangle():def __init__(self, h = 0, w = 0):self.height = hself.width = wdef area(self):return self.height * self.widthdef perimeter(self):return 2 * (self.height + self.width)def isSquare(self):return self.height == self.width
The unittest
module can set up automated tests for various conditions:
# saved as test.pyimport unittestimport rectangle as rectclass RectangleTestCase(unittest.TestCase):def test_area(self):self.assertEqual(self.rectangle.area(), 50)def test_perimeter(self):self.assertEqual(self.rectangle.perimeter(), 30)def test_isSquare(self):self.assertFalse(self.rectangle.isSquare())self.rectangle.width = 5self.assertTrue(self.rectangle.isSquare())def test_error(self):self.rectangle.width = "A"with self.assertRaises(TypeError):a = self.rectangle.perimeter()def setUp(self):self.rectangle = rect.Rectangle(5 ,10)if __name__ == '__main__':unittest.main()
First the unittest.TestCase
class is subclassed for our test case object. Then tests are defined as methods with names starting with test
. This naming convention is mandatory as it allows the test runner to find the methods that represent tests.
Tests consist of methods containing calls to one or more assert
methods of the test case object:
.assertEqual()
: Test passes when both arguments are equal..assertTrue()
: Test passes when argument isTrue
..assertFalse()
: Test passes when argument isFalse
..assertRaises()
: Test passes when the passed exception is raised.
The test case can have .setUp()
and .tearDown()
methods defined. The .setUp()
method is for any initialization needed for the test, and will run before each test
method is carried out. The .tearDown()
method is run after each test
method to do any needed cleanup after each test.
The call to unittest.main()
runs the tests and provides the test script with a command-line interface.
Running the above produces the following:
$ python test.py....----------------------------------------------------------------------Ran 4 tests in 0.001s
The output shows a dot printed for each successful test.
Using the -v
option on the command line will produce a more verbose test:
$ python test.py -vtest_area (__main__.RectangleTestCase) ... oktest_error (__main__.RectangleTestCase) ... oktest_isSquare (__main__.RectangleTestCase) ... oktest_perimeter (__main__.RectangleTestCase) ... ok----------------------------------------------------------------------Ran 4 tests in 0.001s
The output shows the following:
- The method name for each test.
- The test case class the method belongs to.
- A message telling whether or the test was passed.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours