pytest vs unittest: Choosing a Python Testing Framework
pytest vs unittest: pytest needs less boilerplate and adds fixtures and plugins, while unittest is built in. How they differ and which one to choose.
unittest ships with every Python installation. pytest requires pip install pytest. That one-line installation cost is nearly the only thing unittest has going for it in this comparison.
Assertion syntax
unittest uses class-based test cases with specific assertion methods:
import unittest
class TestMath(unittest.TestCase):
def test_addition(self):
self.assertEqual(2 + 2, 4)
if __name__ == "__main__":
unittest.main()pytest uses plain Python assert statements, with no test-case class required:
def test_addition():
assert 2 + 2 == 4pytest’s assertion introspection is the bigger practical difference: when a plain assert fails, pytest rewrites it to show you the actual values on both sides of the comparison in the failure output. unittest’s assertEqual gives a similarly detailed message, but only for the specific assertion methods it defines. This was checked with pytest 9.2.0 on Python 3.14. With total = 2 + 2 and assert total == 5, pytest reported assert 4 == 5, while the same plain assert run by python -m unittest failed with a bare AssertionError and no values. A list comparison with self.assertEqual([1, 2, 3], [1, 2, 4]) gave a detailed diff under both runners, so the gap is in plain assert statements, not in assertEqual. A plain assert inside a unittest.TestCase also gets the detailed message when pytest runs the file.
Parametrization and subtests
pytest 9.0 (November 2025) added built-in subtests, a way to report each failure in a loop individually when the values aren’t known up front, and the release notes say unittest.TestCase.subTest is now supported by pytest too. unittest has had subTest for much longer. For the common case of running one test with several inputs, pytest’s @pytest.mark.parametrize is still the usual tool; the version of pytest checked here (9.2.0) requires Python 3.10 or later.
Fixtures vs setUp/tearDown
unittest handles test setup and teardown through setUp()/tearDown() methods on the test class, run before and after every test in that class. pytest’s fixture system is more flexible — fixtures can be shared across multiple test files, scoped to run once per session or once per test, and composed by simply requesting one fixture as a parameter of another.
Running unittest tests with pytest
You don’t have to choose exclusively — pytest can discover and run unittest-style TestCase classes without modification, which is why many codebases migrate gradually: keep existing unittest.TestCase tests running as-is, write all new tests in pytest’s plain-function style, and run the whole suite with the pytest command either way.
Which one to use
For a new project: pytest, without much debate — better failure messages, less boilerplate, a fixture system that scales better as a suite grows, and a large plugin ecosystem (coverage reporting, parallel test runs, mocking helpers) built around it. Reach for plain unittest specifically when you can’t add a dependency at all, or you’re working in an existing unittest-based codebase where a full migration isn’t worth the churn.
Frequently Asked Questions
What’s the main difference between pytest and unittest?
unittest ships with every Python installation and uses class-based test cases with specific assertion methods. pytest requires installation but uses plain assert statements and rewrites failure output to show the actual values being compared.
Can pytest run unittest tests?
Yes — pytest can discover and run unittest-style TestCase classes without modification, which is why many codebases migrate gradually rather than rewriting an entire suite at once.
Which one should you use for a new project?
pytest, without much debate — better failure messages, less boilerplate, and a fixture system that scales better as a suite grows. Use plain unittest only when you can’t add a dependency at all.
How do pytest fixtures differ from unittest’s setUp/tearDown?
unittest’s setUp()/tearDown() run before and after every test in a class. pytest’s fixtures are more flexible — they can be shared across multiple test files, scoped per session or per test, and composed by requesting one fixture as a parameter of another.