import unittest
def add(x, y):
return x + y
class TestAddFunction(unittest.TestCase):
def test_add(self):
self.assertEqual(add(1, 2), 3)
self.assertEqual(add(0, 0), 0)
self.assertEqual(add(-1, 1), 0)
if __name__ == '__main__':
unittest.main()
Unit testing
- If you are dividing a big project into smaller parts, make sure you can test each part individually.
- Python has a module called
unittest
that is very useful for that. You can use it to test functions, classes and modules.
Setting up test cases
- You will make a subclass out of the
unitest.TestCase
class. See the example:
- The function
unittest.main()
will create objects for every test case class you created, and then run it. - Usually, you will create methods inside your TestCase class with the name “test_” +
- Inside these methods, you will use “assertion” statements to check for conditions. Some of them are:
Assertion Statement | Description |
---|---|
assertEqual(a, b) |
Asserts that a is equal to b . |
assertNotEqual(a, b) |
Asserts that a is not equal to b . |
assertTrue(x) |
Asserts that x is True . |
assertFalse(x) |
Asserts that x is False . |
assertIs(a, b) |
Asserts that a and b are the same object (identity check). |
assertIsNot(a, b) |
Asserts that a and b are not the same object. |
assertIsNone(x) |
Asserts that x is None . |
assertIsNotNone(x) |
Asserts that x is not None . |
assertIn(a, b) |
Asserts that a is in b . |
assertNotIn(a, b) |
Asserts that a is not in b . |
assertIsInstance(a, b) |
Asserts that a is an instance of class b . |
assertNotIsInstance(a, b) |
Asserts that a is not an instance of class b . |
assertAlmostEqual(a, b) |
Asserts that a is approximately equal to b , within a certain tolerance. |
assertNotAlmostEqual(a, b) |
Asserts that a is not approximately equal to b , within a certain tolerance. |
assertGreater(a, b) |
Asserts that a is greater than b . |
assertGreaterEqual(a, b) |
Asserts that a is greater than or equal to b . |
assertLess(a, b) |
Asserts that a is less than b . |
assertLessEqual(a, b) |
Asserts that a is less than or equal to b . |
assertRegex(s, r) |
Asserts that the string s matches the regular expression r . |
assertNotRegex(s, r) |
Asserts that the string s does not match the regular expression r . |
- If everything runs ok, your ouput will say it!
Another example: testing a class
import unittest
# User-defined class
class Circle:
def __init__(self, radius):
self.radius = radius
def compute_area(self):
return 3.14 * self.radius**2
# Class to test Circle
class TestCircle(unittest.TestCase):
def test_compute_area(self):
= Circle(0)
c self.assertEqual(c.compute_area(), 0.0)
= Circle(5)
c self.assertEqual(c.compute_area(), 78.5)
def test_will_fail(self):
= Circle(5)
c self.assertLess(c.compute_area(), 0)
if __name__ == "__main__":
unittest.main()
Some testing tips
Keep tests simple, focused, and readable.
Ensure test cases are independent and do not rely on the state of other tests.
Sometimes you will have to set up and tear down a testing environment (setting some mockup variables, etc). Organize and point this clearly.
If you are testing functions or methods:
- Test the function’s behavior under different input conditions. This includes testing for edge cases, such as empty inputs or boundary conditions.
If you are testing classes and objects:
- Test its methods;
- Test inheritance and method overriding if applicable.
If you are testing modules and packages:
- Test its classes, test its functions;
- Test any global variables or constants;
- Consider using integration tests to test the interaction between different modules.