Python Testing Frameworks: Which One Fits Your Project?
Python testing frameworks range from the built-in unittest to pytest, Robot Framework and Playwright. Compare them on style, scope and what each does best.
On this page
Short answer
Python testing frameworks are tools that run your tests and report results, and the right one depends on what you test. pytest fits most projects, unittest needs no install, Robot Framework suits keyword-driven acceptance tests, and Playwright or Selenium suit browser tests.
What are Python testing frameworks?
Python testing frameworks are tools that discover your test files, run the checks inside them and report which passed and which failed. A framework gives you a way to organise tests, set up and tear down test data, and see results clearly, so testing becomes a repeatable step rather than a manual chore.
Python ships with one framework built in, called unittest, and the wider community maintains several others. The frameworks differ mainly in how tests are written: plain functions with assertions, classes with setup methods, keyword tables, or behaviour descriptions in plain language.
Choosing a framework is mostly a team decision, not a technical one. The best choice is usually the one your team will actually use, that fits the kind of software you build, and that plugs into the tools you already run. See unit testing for how the test levels fit together.
How do the main Python testing frameworks compare?
The main Python testing frameworks cover different kinds of testing, so the comparison is about fit, not ranking. A web team and a data team will often pick different tools for good reasons.
| Framework | Test style | Best for | Install |
|---|---|---|---|
| pytest | Plain functions, fixtures | Most projects | Install separately |
| unittest | Classes, setup methods | No extra install | Built into Python |
| nose2 | unittest-style, plugins | Extending unittest | Install separately |
| Robot Framework | Keyword tables | Acceptance tests | Install separately |
| Behave | Given-when-then text | Behaviour specs | Install separately |
| Hypothesis | Property-based | Finding edge cases | Install separately |
| Playwright | Browser automation | Web app tests | Install separately |
| Selenium | Browser automation | Cross-browser tests | Install separately |
Most teams use two of these: one for unit and integration tests, and one for browser or acceptance tests. Trying to make one framework do everything usually produces awkward tests.
Why do most Python teams pick pytest?
Most Python teams pick pytest because tests are plain functions with simple assertions, and the fixture system handles setup without boilerplate. A new test is often just a function whose name starts with test, which keeps the barrier to writing tests low.
Fixtures are the feature that keeps teams on pytest. A fixture prepares something a test needs, such as a database connection or a temporary file, and pytest handles its setup and cleanup. Fixtures can be shared across test files, so common setup lives in one place.
The plugin ecosystem is the other reason. Plugins add parallel running, coverage reporting, test ordering and many other features without changing how tests are written. If your team grows, pytest grows with it instead of forcing a rewrite of the test suite.
Teams building Python products often pair pytest with a delivery pipeline that runs the suite on every change. For the product side of that work, custom software development covers building and shipping Python systems with testing built in.
When do unittest, Robot Framework and Behave fit better?
unittest fits when you cannot install extra packages or when your team already knows its class-based style. It is part of Python itself, so it works everywhere Python works, and its test discovery and mocking support are solid. The cost is more boilerplate: tests live in classes and setup uses dedicated methods.
Robot Framework fits acceptance tests that non-developers need to read. Tests are written as tables of keywords such as open browser, enter text and check result, so a product owner can follow what a test does. It suits teams where acceptance criteria are shared across roles.
Behave fits behaviour-driven development, where tests are written in plain language as given-when-then scenarios and then linked to Python step functions. It works well when the specification itself is the deliverable, such as in regulated work where tests double as documentation.
How do you test web apps and edge cases?
Web apps need browser tests, and the two common Python options are Playwright and Selenium. Playwright controls modern browsers with a single API, waits for elements automatically and can run tests in parallel. Selenium is the older option, with wider browser and grid support and a large body of existing knowledge.
For edge cases, Hypothesis takes a different approach called property-based testing. Instead of writing individual examples, you describe a property the code should always hold, and Hypothesis generates hundreds of inputs to try to break it. It is good at finding the strange inputs a human would never think to test.
Browser tests are slower than unit tests, so keep them few and focused on critical paths such as login, checkout and form submission. For performance checks, see load testing tools for teams, which covers testing under load rather than correctness.
Example (hypothetical): speeding up a slow test suite
Example (hypothetical): a small web team has 200 unit tests that run one after another in about 7 minutes, since each test takes roughly 2 seconds. Developers stop running the suite before committing because it feels slow, and broken code reaches review more often.
The team switches the suite to pytest and runs it in parallel across 4 workers. The same 200 tests now take about 2 minutes, since 200 tests split across 4 workers is 50 tests per worker at 2 seconds each, or about 100 seconds, plus startup time. The team also splits the suite so unit tests run on every commit and the slower browser tests run once a day.
Within a month, developers run the unit suite before every commit again. The lesson is not that parallel running is magic, but that a fast suite gets used and a slow one gets skipped.
What are the common mistakes?
The most common Python testing mistakes are testing too little of the wrong thing, writing slow tests nobody runs, and letting the suite become hard to maintain.
- Only testing the happy path. Tests that never check errors, empty inputs or bad data miss the failures users actually hit.
- Tests that depend on each other. A test that only passes when run after another test will fail in parallel runs and confuse everyone.
- Slow suites that nobody runs. A suite that takes an hour gets skipped. Keep unit tests fast and move slow tests to a nightly run.
- Sleeping instead of waiting. Fixed pauses in browser tests are flaky. Wait for the element or condition instead.
- No test data cleanup. Tests that leave files or database rows behind interfere with the next run.
- Testing implementation details. Tests tied to internal structure break on every refactor. Test behaviour, not internals.
Key takeaways
- pytest fits most Python projects; unittest needs no install and works everywhere
- Robot Framework suits acceptance tests non-developers must read; Behave suits plain-language behaviour specs
- Playwright and Selenium cover browser tests; Hypothesis finds edge cases by generating inputs
- Keep unit tests fast and run slow browser tests on a schedule
- Test behaviour and failure cases, not internal implementation details
Frequently asked questions
Which Python testing framework should beginners learn first?
pytest is the usual recommendation, because tests are plain functions with simple assertions and the fixture system is easy to learn. unittest is also fine and needs no install, since it ships with Python. Either one teaches the same testing habits.
Is pytest better than unittest?
pytest is more concise and has a larger plugin ecosystem, while unittest needs no install and follows a class-based style some teams prefer. For most new projects pytest is the common choice, but unittest is a sound one, and pytest can run unittest-style tests too.
What is the difference between pytest and Robot Framework?
pytest is written in Python and suits unit and integration tests, while Robot Framework tests are keyword tables that non-developers can read. Use pytest for code-level tests and Robot Framework for acceptance tests shared across roles.
Do I need Selenium if I use Playwright?
Usually not. Playwright covers modern browser testing with automatic waiting and parallel runs, while Selenium offers wider browser and grid support. Pick one for browser tests and keep the suite small, focused on critical paths.
What is property-based testing with Hypothesis?
Instead of writing individual test examples, you describe a property the code should always hold, and Hypothesis generates many inputs to try to break it. It is good at finding edge cases such as empty lists, extreme numbers and unusual strings.
How many tests should a Python project have?
There is no fixed number. Cover the core behaviour, the failure cases and the paths users actually take, and keep the suite fast enough to run often. A smaller suite that runs on every commit beats a large suite that nobody runs.