📘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
TestMathOperationsis a custom name you give for your group of tests.unittest.TestCasegives 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 ifaequalsbself.assertNotEqual(a, b)→ Checks ifadoes not equalbself.assertTrue(x)→ Checks ifxis Trueself.assertFalse(x)→ Checks ifxis Falseself.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
- Unit Test Basics: Test small units of code (functions, methods)
- Module to Use:
unittest - Test Class: Must inherit
unittest.TestCase - Test Methods: Must start with
test_ - Assertions: Use
assertEqual,assertTrue,assertRaises, etc. - Run Tests: Use
unittest.main()to execute tests - 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.
