CheckOps home

Tools directory

P

CheckOps

Python unittest

unittest is Python's built-in xUnit-style testing framework, with TestCase classes, fixtures, test discovery, subtests, async test cases and the unittest.mock library, available in every Python install.

Visit website

Latest public signals

Not refreshed yet. Refresh reads the public site and, when the entry has a GitHub repository, the latest release.

Visit Website

Introduction

unittest is the unit testing framework in Python’s standard library. Originally inspired by JUnit, it supports test automation, shared setup and teardown, aggregation of tests into collections, and independence of tests from the reporting framework. Because it ships with Python, it needs no installation and is available wherever Python is.

Tests are methods on unittest.TestCase subclasses, with assertions such as assertEqual, assertTrue, assertRaises and assertAlmostEqual. Fixtures run via setUp/tearDown, setUpClass/tearDownClass and module-level hooks, and addCleanup registers cleanups. subTest reports parameterised cases individually, IsolatedAsyncioTestCase tests coroutines, and skipping and expected failures are supported with decorators. python -m unittest discover finds tests automatically. The companion unittest.mock module provides Mock, MagicMock, AsyncMock and patch, which are widely used even in pytest suites. pytest can run unittest-style tests unchanged, which makes migration easy.

For larger suites, unittest test cases can also be grouped into TestSuite objects and run with alternative runners, and many CI systems collect JUnit XML from unittest runs through small add-on packages. The documentation also covers doctest integration via load_tests, which lets examples embedded in docstrings run alongside regular test cases.

Features

  1. Standard library: No dependencies; available in every Python install.
  2. xUnit structure: TestCase classes, fixtures and suites.
  3. unittest.mock: Mocks, patching and async mocks.
  4. Discovery and subtests: Automatic discovery and per-case reporting.

Pros

  1. Free: Included with Python.
  2. Universal: Available on every Python installation.

Cons

  1. Verbose: Class-based tests and self.assert* methods are wordier than pytest.
  2. Fewer features: No fixtures-by-name, rich plugins or parametrisation decorators.
import unittest
from unittest.mock import patch
from shop import price_with_tax, fetch_rate

class PriceTests(unittest.TestCase):
    def test_tax(self):
        for net, gross in [(100, 120), (0, 0)]:
            with self.subTest(net=net):
                self.assertEqual(price_with_tax(net, rate=0.2), gross)

    @patch("shop.fetch_rate", return_value=0.2)
    def test_uses_rate(self, mock_rate):
        price_with_tax(100)
        mock_rate.assert_called_once()

if __name__ == "__main__":
    unittest.main()
python -m unittest discover -s tests -v

Resources:

Pricing

Free as part of the Python standard library (PSF licence).

  1. Projects that avoid third-party dependencies, such as standard library-style tools.
  2. Teams using unittest.mock with any runner, including pytest.

Conclusion

unittest is Python’s dependable built-in framework; many teams now write tests with pytest, but unittest and unittest.mock remain foundational.

Alternatives

Other tools on CheckOps that cover similar ground. Open a page, or compare one with Python unittest side by side.