Construct a Python unit test

📘Cisco DevNet Associate (200-901 DEVASC)


In software development, a unit test is a way to check that a small piece of your code (called a “unit”) works exactly as expected. In Python, the built-in module used for unit testing is called unittest.

Unit testing is important in IT environments because it ensures that your code behaves correctly before you deploy it to production, saving time and preventing errors.


1. Importing the unittest Module

Before writing any test, you need to import Python’s unittest module.

import unittest

This module provides tools to create and run tests, check results, and report errors.


2. Creating a Test Class

A test class groups related tests together. It must inherit from unittest.TestCase.

Example:

class TestMathOperations(unittest.TestCase):
pass # Placeholder for now
  • TestMathOperations is a custom name you give for your group of tests.
  • unittest.TestCase gives your class all the testing tools.

3. Writing Test Methods

Inside your test class, you write test methods to check different parts of your code.
Each method must:

  • Start with test_ (so Python knows it is a test)
  • Use assertions to check expected results

Example:

def add(a, b):
return a + bclass TestMathOperations(unittest.TestCase):

def test_add_positive_numbers(self):
result = add(5, 7)
self.assertEqual(result, 12) # Checks if result == 12 def test_add_negative_numbers(self):
result = add(-3, -7)
self.assertEqual(result, -10)

Explanation of assertions:

  • self.assertEqual(a, b) → Checks if a equals b
  • self.assertNotEqual(a, b) → Checks if a does not equal b
  • self.assertTrue(x) → Checks if x is True
  • self.assertFalse(x) → Checks if x is False
  • self.assertRaises(ExceptionType, function, args...) → Checks if a function raises an error

4. Running the Tests

At the bottom of your test file, add this block to run the tests automatically:

if __name__ == '__main__':
unittest.main()

When you run the Python file, it will:

  • Find all methods starting with test_
  • Run them
  • Show a report of pass/fail for each test

5. Example of a Complete Unit Test File

import unittest# Code we want to test
def add(a, b):
return a + bdef divide(a, b):
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b# Unit test class
class TestMathOperations(unittest.TestCase):

def test_add_positive_numbers(self):
self.assertEqual(add(5, 7), 12) def test_add_negative_numbers(self):
self.assertEqual(add(-3, -7), -10) def test_divide_numbers(self):
self.assertEqual(divide(10, 2), 5) def test_divide_by_zero(self):
self.assertRaises(ValueError, divide, 10, 0)# Run tests
if __name__ == '__main__':
unittest.main()

Output Example:

....
----------------------------------------------------------------------
Ran 4 tests in 0.001sOK
  • Each . means a test passed
  • If a test fails, Python shows what went wrong

6. Why Unit Tests Are Important in IT Environments

  • Prevents bugs: Catch errors before deploying code
  • Ensures correctness: Confirms that functions return expected results
  • Supports automation: Can integrate with CI/CD pipelines to run tests automatically whenever code is updated
  • Helps maintenance: When code changes, tests quickly verify nothing breaks

7. Key Points for the Exam

  1. Unit Test Basics: Test small units of code (functions, methods)
  2. Module to Use: unittest
  3. Test Class: Must inherit unittest.TestCase
  4. Test Methods: Must start with test_
  5. Assertions: Use assertEqual, assertTrue, assertRaises, etc.
  6. Run Tests: Use unittest.main() to execute tests
  7. Purpose: Ensure code works correctly, prevent bugs, support automation

This is everything you need for the Construct a Python unit test topic in the 200-901 DEVASC exam.

Buy Me a Coffee