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.
Latest public signals
Not refreshed yet. Refresh reads the public site and, when the entry has a GitHub repository, the latest release.
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
- Standard library: No dependencies; available in every Python install.
- xUnit structure: TestCase classes, fixtures and suites.
- unittest.mock: Mocks, patching and async mocks.
- Discovery and subtests: Automatic discovery and per-case reporting.
Pros
- Free: Included with Python.
- Universal: Available on every Python installation.
Cons
- Verbose: Class-based tests and
self.assert*methods are wordier than pytest. - Fewer features: No fixtures-by-name, rich plugins or parametrisation decorators.
Usage and Links (Example)
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).
Recommended for?
- Projects that avoid third-party dependencies, such as standard library-style tools.
- 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.
- Ppytestpytest is the usual Python test runner: plain asserts, fixtures, parametrization, and a large plugin set.Compare with Python unittest
HHypothesisHypothesis is the MPL-2.0 property-based testing library for Python: describe the range of inputs, let it generate cases including edge cases, and get the simplest failing example when a property breaks.Compare with Python unittest
CCoverage.pyCoverage.py is the standard Apache-2.0 code coverage tool for Python, measuring line and branch coverage and producing terminal, HTML, XML, JSON and LCOV reports; pytest-cov builds on it.Compare with Python unittest
Ttoxtox is an MIT-licensed command-line test automation tool for Python that creates isolated environments to run tests across Python versions and dependency sets, locally and in CI.Compare with Python unittest
NNoxNox is an Apache-2.0 Python test automation tool that defines sessions in a Python file (noxfile.py), running tests across interpreters and dependency sets in isolated environments.Compare with Python unittest
Ffactory_boyfactory_boy is an MIT-licensed test fixtures replacement for Python, inspired by thoughtbot's factory_bot, that builds test objects and ORM records for Django, SQLAlchemy and MongoEngine with Faker integration.Compare with Python unittest