![]() |
VOOZH | about |
Unit testing checks small pieces of code (like functions or classes) to confirm they work correctly. In Python, this is done with the unittest framework, which is built into the standard library and follows the xUnit style. The unittest module is widely used because:
This makes unittest a dependable choice for creating systematic and automated tests in Python.
Example: Testing a Simple Addition
Unittest framework allows developers to verify that functions produce the expected results. A test case is created by inheriting from unittest.TestCase and defining methods that start with test_.
Output
.
----------------------------------------------------------------------Ran 1 test in 0.000s
OK
Explanation:
Unittest has many methods to assert on the values, types and existence of variables. Below are some of the methods that are commonly used to write assertions:
Method | Description |
|---|---|
.assertEqual(a, b) | Checks if a is equal to b, similar to the expression a == b. |
.assertTrue(x) | Asserts that the boolean value of x is True, equivalent to bool(x) is True. |
.assertIsInstance(a, b) | Asserts that a is an instance of class b, similar to the expression isinstance(a, b). |
.assertIsNone(x) | Ensures that x is None, similar to the expression x is None. |
.assertFalse(x) | Asserts that the boolean value of x is False, similar to bool(x) is False. |
.assertIs(a, b) | Verifies if a is identical to b, akin to the expression a is b. |
.assertIn(a, b) | Checks if a is a member of b, akin to the expression a in b. |
Unit testing is associated with white-box testing as tests are usually written with knowledge of the internal code structure. However, unit tests can also be designed from a black-box perspective.
Example 1: We will write a simple function add() that returns the sum of two numbers. Using unittest framework, we create a test case class TestAddFunction to verify different scenarios of this function.
To run the unit tests, run the following command in your terminal:
python test.py
Output
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
If a test fails (e.g., expected value is changed), output shows the failed test:
..F
======================================================================
FAIL: test_add_positive_numbers (__main__.TestAddFunction)
----------------------------------------------------------------------
Traceback (most recent call last):
...
AssertionError: 3 != 2
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (failures=1)
Use -v option for detailed results:
python test.py -v
Output
test_add_mixed_numbers (__main__.TestAddFunction) ... ok
test_add_negative_numbers (__main__.TestAddFunction) ... ok
test_add_positive_numbers (__main__.TestAddFunction) ... ok
Ran 3 tests in 0.002s
OK
Example 2: This example demonstrates testing different string operations using unittest framework. The test case class TestStringMethods includes multiple tests to verify string properties and behavior.
Output
.....
----------------------------------------------------------------------
Ran 5 tests in 0.000s
OK
Explanation: