UNIT TESTING IN PYTHON PROGRAMMING
Unit testing in Python is a method used to verify that small, isolated parts of a codebase (units) are working as expected. It involves writing test cases for individual functions or methods to ensure they produce the correct output for various inputs. The `unittest` module in Python provides a framework for creating and running unit tests.
The following is an explanation of each assertion method provided by the `unittest` module, along with examples:
1. `assertEqual(a, b)`: Checks if the values of `a` and `b` are equal. If they are equal, the test passes; otherwise, it fails.
“`python
self.assertEqual(5, 5) # Passes
self.assertEqual(5, 6) # Fails
“`
2. `assertNotEqual(a, b)`: Checks if the values of `a` and `b` are not equal. If they are not equal, the test passes; otherwise, it fails.
“`python
self.assertNotEqual(5, 6) # Passes
self.assertNotEqual(5, 5) # Fails
“`
3. `assertTrue(x)`: Checks if the value of `x` evaluates to `True`. If it does, the test passes; otherwise, it fails.
“`python
self.assertTrue(True) # Passes
self.assertTrue(False) # Fails
“`
4. `assertFalse(x)`: Checks if the value of `x` evaluates to `False`. If it does, the test passes; otherwise, it fails.
“`python
self.assertFalse(False) # Passes
self.assertFalse(True) # Fails
“`
5. `assertIs(a, b)`: Checks if `a` and `b` refer to the same object. If they do, the test passes; otherwise, it fails.
“`python
a = [1, 2, 3]
b = a
self.assertIs(a, b) # Passes
“`
6. `assertIsNot(a, b)`: Checks if `a` and `b` do not refer to the same object. If they don’t, the test passes; otherwise, it fails.
“`python
a = [1, 2, 3]
b = [1, 2, 3]
self.assertIsNot(a, b) # Passes
“`
7. `assertIsNone(x)`: Checks if the value of `x` is `None`. If it is, the test passes; otherwise, it fails.
“`python
a = None
self.assertIsNone(a) # Passes
“`
8. `assertIsNotNone(x)`: Checks if the value of `x` is not `None`. If it is not `None`, the test passes; otherwise, it fails.
“`python
a = 5
self.assertIsNotNone(a) # Passes
“`
9. `assertIn(a, b)`: Checks if `a` is a member of `b`. If `a` is in `b`, the test passes; otherwise, it fails.
“`python
self.assertIn(2, [1, 2, 3]) # Passes
“`
10. `assertNotIn(a, b)`: Checks if `a` is not a member of `b`. If `a` is not in `b`, the test passes; otherwise, it fails.
“`python
self.assertNotIn(4, [1, 2, 3]) # Passes
“`
11. `assertIsInstance(a, b)`: Checks if `a` is an instance of the class or type `b`. If `a` is an instance of `b`, the test passes; otherwise, it fails.
“`python
self.assertIsInstance(5, int) # Passes
“`
12. `assertNotIsInstance(a, b)`: Checks if `a` is not an instance of the class or type `b`. If `a` is not an instance of `b`, the test passes; otherwise, it fails.
“`python
self.assertNotIsInstance(5, str) # Passes
“`
These assertion methods are commonly used in unit testing to verify the correctness of code behavior. They help ensure that the code behaves as expected under different conditions.
IMPLEMENT UNIT TESTS
To demonstrate unit tests with different files, we can organize our code into separate modules. Let’s create two files: one containing the function we want to test, and another containing the unit tests.
Here’s the directory structure:
“`
project/
│
├── calculator.py
└── test_calculator.py
“`
In `calculator.py`, we’ll define the `add` function:
“`python
# calculator.py
def add(a, b):
return a + b
“`
And in `test_calculator.py`, we’ll write unit tests for the `add` function:
“`python
# test_calculator.py
import unittest
from calculator import add
class TestAddFunction(unittest.TestCase):
def test_add_positive_numbers(self):
self.assertEqual(add(3, 5), 8) # 3 + 5 = 8
def test_add_negative_numbers(self):
self.assertEqual(add(-3, -5), -8) # -3 + (-5) = -8
def test_add_mixed_numbers(self):
self.assertEqual(add(3, -5), -2) # 3 + (-5) = -2
def test_add_zero(self):
self.assertEqual(add(3, 0), 3) # 3 + 0 = 3
if __name__ == ‘__main__’:
unittest.main()
“`
To run the tests, navigate to the directory containing the files (`project` in this case) and execute the command:
“`
python -m unittest test_calculator.py
“`
This command will discover and run all the tests defined in `test_calculator.py`. If all tests pass, you’ll see an output indicating that all tests were successful. If any test fails, you’ll see details about which test failed and why.
Using separate files for code and tests helps organize your project and keeps your codebase modular and maintainable.
Â