Browse Courses

Unit Testing

This document introduces unit testing in Python, covering the process, naming conventions, test structure, and result interpretation. It explains how to build, execute, and review unit tests for reliable code quality.

This document explains the fundamentals of unit testing in Python, including the test process, naming conventions, test structure, and how to interpret results. Readers will learn to build, execute, and review unit tests for robust code quality.


Introduction to Unit Testing

Unit testing validates that individual units of code operate as intended. A unit is a small, testable part of an application, such as a function or method.


The Unit Test Process

The unit test process involves two main phases:

  1. Local Testing: Test each unit on the local system. If a test fails, identify and fix the issue, then retest.
  2. Server Testing: After passing locally, test the unit in a server environment (e.g., CI/CD server). If the test fails, review the failure details, fix the issue, and retest. Once the unit passes, it is integrated into the final code base.

Building and Naming Unit Tests

  • Use the unittest Python library to create unit tests.
  • Name the unit test file by appending or prepending test to the module name (e.g., test_mymodule.py).
  • Import the functions to test (e.g., from mymodule import square, doubler).
  • Create a test class prefixed with Test and inherit from unittest.TestCase (e.g., class TestMyModule(unittest.TestCase):).
  • Define test functions within the class, each starting with test (e.g., def test_square(self):).

Writing Test Cases and Assertions

  • Use assertion methods like assertEqual to compare actual and expected values.
  • Each test function should call the function under test and compare its output to the expected result.
Assertion MethodPurpose
assertEqualChecks if two values are equal
assertTrueChecks if a condition is true
assertFalseChecks if a condition is false

Reviewing Unit Test Output

  • After running the test file, the output displays the number of tests run and their results.
  • If all tests pass, the output shows OK.
  • If a test fails, the output details the failure, including the function and assertion that failed, helping to identify and correct mistakes before deployment.

Summary Table: Unit Testing Steps

StepDescription
Local TestingTest units on local system, fix issues, retest
Server TestingTest units on CI/CD server, fix issues, retest
IntegrationMerge passing units into final code base

Conclusion

Unit testing ensures that code units function as designed, improving reliability and maintainability. Following proper naming conventions, structuring tests, and reviewing outputs are essential for effective testing and robust software development.


FAQ

  1. To validate that individual units of code operate as intended
  2. To deploy code to production
  3. To optimize code performance
  4. To document code changes
(1) Unit testing checks that each small, testable part of an application works as designed.

  1. The code is immediately deployed
  2. The issue is identified and fixed before retesting
  3. The test is ignored
  4. The code is merged without review
(2) If a unit test fails locally, the issue should be fixed and the test rerun before proceeding.

  1. Prefixing test classes with ‘Test’
  2. Appending or prepending ’test’ to the file name
  3. Using random names unrelated to the module
  4. Inheriting from unittest.TestCase
(3) Test files and classes should be clearly named to indicate their purpose and relation to the module.

  1. It checks if two values are equal
  2. It checks if a value is true
  3. It checks if a value is false
  4. It runs all tests in the module
(1) assertEqual compares the actual output of a function to the expected value.

StepDescription
A. Local Testing1. Merge passing units into final code base
B. Server Testing2. Test units on local system, fix issues, retest
C. Integration3. Test units on CI/CD server, fix issues, retest
A-2, B-3, C-1.

  1. It only shows the number of tests run
  2. It provides details on test results and failures
  3. It hides failed test information
  4. It is not useful for debugging
(2) Unit test output details which tests passed or failed, aiding in debugging.

Only functions that start with ’test’ in a unit test class are executed by the unittest framework.

True. The unittest framework runs only those functions whose names begin with ’test'.