Writing a Unit Test: Example and Best Practices

GitHub Copilot · AI-Assisted Testing · By Super Admin

A worked Python example shows how to structure unit tests with the unittest framework and assertion methods, and a set of best practices—paths, assert once, independence, simplicity, and test doubles—helps make unit tests more effective.

Understanding how a unit test is actually constructed, and following established best practices, helps ensure that unit tests are effective rather than a source of wasted effort.

Example of a Unit Test

Consider a Python function, calculate_total_price, that takes a list of products and applies a tax calculation, returning 0.0 for an empty list and otherwise summing item price times quantity before applying the tax rate. To unit test this function, you first determine what framework to use, since frameworks provide functions for creating test cases and helping with evaluations. Python offers several frameworks: unittest, a standard system built into the language; doctest, which uses docstrings to embed test examples; and Behave, a behavior-driven development (BDD) framework that allows writing tests in natural language. Using unittest, you write test cases covering common use cases, edge cases, and error conditions, such as an empty list, zero quantity, zero price, and zero tax rate. You create a class that inherits from unittest.TestCase, which has assertion methods; an assertion method verifies whether a condition is true or false. For example, self.assertEqual checks whether the output from calculate_total_price matches the expected result, such as 0.0 for an empty list. You then run the tests to see if any fail to meet the conditions, and use the results to fix problems and refactor the code.

Best Practices for Unit Testing

Because unit testing has known issues and difficulties, several best practices can help. For paths, try to anticipate the potential pathways for the code as much as possible; this takes time and planning but results in robust unit tests. For assert once, each unit test should have only one outcome, true or false, since otherwise the results become difficult to interpret. For independence, one unit test should not rely on the outcome of another, which helps avoid cascading failures and allows for identifying the root cause of problems. For simplicity, unit tests should not involve complex logic, keeping the focus on being clear about what is being tested. For test doubles, if the code uses data for processing, you should use mocks, which are simulated actions, so that tests focus on the behavior of the function rather than outside factors.

Key takeaways

  • Python offers frameworks like unittest, doctest, and Behave for writing unit tests.
  • Test cases should cover common use cases, edge cases, and error conditions, such as empty lists or zero values.
  • Assertion methods like self.assertEqual verify whether a function's output matches an expected result.
  • Best practices include anticipating code paths, asserting only one outcome per test, keeping tests independent and simple, and using mocks (test doubles) to isolate behavior.
0.0 (0 ratings)

Discussion (0)

No discussion on this article yet — be the first to comment.

Please sign in to join the discussion.