Skip to main content
Tool comparison
COMPARE  /  pytest-vs-unittest

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?

pytest vs unittest, compared by dimension
Where it comes frompytestThird-party package installed with pipunittestPython standard library, nothing to install
How a test lookspytestA plain function named test_* in a test_*.py fileunittestA method named test* on a unittest.TestCase subclass
AssertionspytestPlain assert with detailed introspection of the failing expressionunittestassertEqual, assertTrue, assertIn, assertRaises and other methods
Setup and teardownpytestFixtures: modular, scoped (function, class, module, session), injected by argument nameunittestsetUp and tearDown per test, setUpClass and setUpModule, addCleanup
Parameterisationpytest@pytest.mark.parametrize runs one test per case and reports each separatelyunittestsubTest() inside a loop distinguishes cases within one test method
Discovery and runningpytestAuto-discovery of test modules and functions; pytest at the command lineunittestpython -m unittest discover, or a module, class or method path
Plugins and ecosystempytestA plugin architecture with over 1,300 external plugins, including pytest-playwright and pytest-xdistunittestNo plugin system; extend by subclassing
Running the other's testspytestRuns unittest suites out of the boxunittestCannot run pytest-style function tests or fixtures
Python versionpytestPython 3.10 or later, or PyPy 3, for current releasesunittestWhatever 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.

  1. 01What is a pytest fixture, what scopes exist, and how would you share one browser session across a module of tests?
  2. 02Show how you would run the same login test against ten sets of credentials in pytest and in unittest.
  3. 03You inherit 2,000 unittest tests. How would you move to pytest without a big-bang rewrite?
  4. 04Why do pytest failures show the values in a failing assert, and how does unittest achieve the same?
Answer 2 Free Questions

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.

EXEC.NOW

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.

Question 1 · Automation · Mid-levellive scoring

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

FREE.TO.START  ·  7.DAY.TRIAL ON PAID PLANS
Written by , Senior QA Automation Engineer, 50+ QA candidate interviews conductedLast updated July 2026