pytest vs unittest
unittest ships with Python: tests are methods on TestCase subclasses with setUp, tearDown and assertEqual-style methods. pytest is a third-party framework that uses plain assert statements with detailed introspection, modular fixtures, parametrize and over a thousand plugins, and it runs unittest suites unchanged. Choose pytest for new automation work; choose unittest when adding a dependency is not an option.
- pytest
- A third-party Python testing framework with plain assert statements, modular fixtures, parametrisation and a plugin architecture.
- unittest
- The unit testing framework in Python's standard library, inspired by JUnit, with tests as methods on TestCase subclasses.
How do pytest and unittest compare?
| Dimension | pytest | unittest |
|---|---|---|
| Where it comes from | pytestThird-party package installed with pip | unittestPython standard library, nothing to install |
| How a test looks | pytestA plain function named test_* in a test_*.py file | unittestA method named test* on a unittest.TestCase subclass |
| Assertions | pytestPlain assert with detailed introspection of the failing expression | unittestassertEqual, assertTrue, assertIn, assertRaises and other methods |
| Setup and teardown | pytestFixtures: modular, scoped (function, class, module, session), injected by argument name | unittestsetUp and tearDown per test, setUpClass and setUpModule, addCleanup |
| Parameterisation | pytest@pytest.mark.parametrize runs one test per case and reports each separately | unittestsubTest() inside a loop distinguishes cases within one test method |
| Discovery and running | pytestAuto-discovery of test modules and functions; pytest at the command line | unittestpython -m unittest discover, or a module, class or method path |
| Plugins and ecosystem | pytestA plugin architecture with over 1,300 external plugins, including pytest-playwright and pytest-xdist | unittestNo plugin system; extend by subclassing |
| Running the other's tests | pytestRuns unittest suites out of the box | unittestCannot run pytest-style function tests or fixtures |
| Python version | pytestPython 3.10 or later, or PyPy 3, for current releases | unittestWhatever Python version you have |
When should you choose pytest, and when unittest?
Choose pytest when
- You are starting a new Selenium, Playwright or API automation project in Python.
- Tests need shared, scoped resources such as a browser session or a database connection per module.
- The same test must run against many inputs and each case should be reported on its own.
- You want plugins for parallel runs, HTML reports, retries and Playwright without writing them.
Choose unittest when
- Adding third-party packages is restricted, for example in a locked-down build environment.
- The codebase already has a large unittest suite and there is no appetite to change style.
- The team knows JUnit-style xUnit frameworks and wants the same shape in Python.
- You are writing a small script or library where a dependency is not worth it.
How does pytest vs unittest come up in QA interviews?
Python automation interviews use this question to find out whether you have built a framework or only written test functions. Interviewers expect you to explain fixtures and parametrize with an example from your own project, and to know that pytest can run the unittest code you inherited.
- 01What is a pytest fixture, what scopes exist, and how would you share one browser session across a module of tests?
- 02Show how you would run the same login test against ten sets of credentials in pytest and in unittest.
- 03You inherit 2,000 unittest tests. How would you move to pytest without a big-bang rewrite?
- 04Why do pytest failures show the values in a failing assert, and how does unittest achieve the same?
No account needed · Scored in under a minute against a senior rubric
Common questions about pytest vs unittest
Is pytest better than unittest?
For most automation work, yes: plain asserts, fixtures, parametrize and the plugin ecosystem remove boilerplate and add capabilities unittest does not have. unittest wins when you cannot add dependencies or want a pure standard-library project. pytest also runs unittest tests, so the choice is rarely exclusive.
Can pytest run unittest tests?
Yes. pytest runs unittest test suites, including trial, out of the box, which is why most migrations start by running the existing suite under pytest and converting tests only as they are touched.
Does unittest support parameterised tests?
Not as separate tests. unittest provides subTest(), a context manager that distinguishes cases inside one test method so that a failure in one case does not stop the others. pytest's parametrize creates a separate, individually reported test per case.
Which should I learn for QA interviews?
pytest, because Python automation roles almost always use it and interviewers ask about fixtures and parametrize. Know enough unittest to read a TestCase class and to explain how you would run one under pytest.
Where was this checked?
What should you read next?
Ready to be asked about pytest or unittest?
Practice explaining the trade-off to an interviewer who knows both.
Join 500+ QA engineers already practicing with AssertHired.
A test passes locally but fails in CI about one run in five. Walk me through what you check first, and why.
Scored on the same four dimensions as the real thing: Technical accuracy · Coverage · Clarity · Best practices.
Rather skip ahead? Create a free account