Testing

Libraries for testing codebases and generating test data. Also see awesome-python-testing.

pytest runs your unittest suites unchanged, so switching Python testing frameworks costs no rewrite. Hypothesis and Playwright tests run inside it too.

How to choose:

  • Unit and integration tests: pytest
  • Tests that generate their own inputs: Hypothesis
  • End-to-end browser tests: Playwright
  • Acceptance tests that non-programmers can read: Robot Framework
  • Testing across Python versions: tox, or Nox to configure it in Python
  • WebDriver tests on remote machines: Selenium, or SeleniumBase with waits and reports built in
  • Load tests written as Python code: Locust
  • API tests generated from an OpenAPI or GraphQL schema: Schemathesis
  • Replacing objects in a test: unittest.mock, or time-machine for the clock
  • Faking HTTP calls: responses for Requests, RESPX for HTTPX, VCR.py to record real traffic
  • Test objects: factory_boy for ORM models, Polyfactory for typed classes
  • Code coverage: Coverage.py
  • Fake names and addresses: Faker, or Mimesis for speed

Listed in editorial order, grouped by use case. Click a column to re-sort the whole list.

Press / to search. Tap a tag to filter. Click any row for details.

Search and filter

Results

Row number Tags

Frameworks 3 projects

A mature full-featured Python testing tool.
Also see: awesome-pytest
pytest-dev/github.com/pytest-dev/pytest / /780,138,529 downloads/month
Hypothesis is an advanced Quickcheck style property based testing library.
HypothesisWorks/github.com/HypothesisWorks/hypothesis / /52,853,067 downloads/month
A generic test automation framework.
robotframework/github.com/robotframework/robotframework / /4,073,950 downloads/month

Test Runners 2 projects

Auto builds and tests distributions in multiple Python versions.
tox-dev/github.com/tox-dev/tox / /14,532,693 downloads/month
Flexible test automation for Python.
wntrblm/github.com/wntrblm/nox / /3,848,837 downloads/month

Browser Automation 3 projects

Python version of the Playwright testing and automation library.
microsoft/github.com/microsoft/playwright-python / /95,806,221 downloads/month
Python bindings for Selenium WebDriver.
SeleniumHQ/github.com/SeleniumHQ/selenium / /28,624,046 downloads/month
Python framework for web automation & testing, with stealth options.
seleniumbase/github.com/seleniumbase/SeleniumBase / /1,342,323 downloads/month

Load Testing 1 project

Scalable user load testing tool written in Python.
locustio/github.com/locustio/locust / /9,831,728 downloads/month

API Testing 1 project

A tool for automatic property-based testing of web APIs from OpenAPI or GraphQL schemas.
schemathesis/github.com/schemathesis/schemathesis / /3,161,969 downloads/month

Mock 5 projects

(Python standard library) A mocking and patching library.
A utility library for mocking out the requests Python library.
getsentry/github.com/getsentry/responses / /53,392,813 downloads/month
Record and replay HTTP interactions on your tests.
kevin1024/github.com/kevin1024/vcrpy / /22,723,782 downloads/month
Mock HTTPX with awesome request patterns and response side effects.
lundberg/github.com/lundberg/respx / /23,663,246 downloads/month
Travel through time in your tests by mocking the current time at the C level.
adamchainz/github.com/adamchainz/time-machine / /19,997,178 downloads/month

Object Factories 2 projects

A test fixtures replacement for Python.
FactoryBoy/github.com/FactoryBoy/factory_boy / /20,868,231 downloads/month
A mock data generation library based on type hints (continuation of pydantic-factories).
litestar-org/github.com/litestar-org/polyfactory / /11,696,823 downloads/month

Code Coverage 1 project

Code coverage measurement.
coveragepy/github.com/coveragepy/coveragepy / /288,579,309 downloads/month

Fake Data 2 projects

A Python package that generates fake data.
joke2k/github.com/joke2k/faker / /66,679,846 downloads/month
A Python library for generating fake but realistic data in multiple languages and locales.
lk-geimfari/github.com/lk-geimfari/mimesis / /2,063,316 downloads/month

Testing guide

Write pytest tests as plain functions with plain assert statements: pytest's assertion introspection reports the intermediate values when one fails, so there are no assert methods to remember. Share setup through fixtures, which a test requests by name, instead of xUnit-style setup and teardown. pytest also runs unittest-based suites, so you can move an old suite over a test at a time.

Hypothesis tests take a @given decorator with a strategy describing the inputs, and Hypothesis picks which inputs to try, edge cases included. They're still regular functions that pytest or unittest runs.

Playwright's docs call its pytest plugin the recommended way to write end-to-end tests, and every test gets a fresh browser context. Playwright waits for elements to be actionable before each action, and its assertions wait for the expected condition. Locate elements by role and other user-facing attributes, not CSS or XPath, which break as the DOM changes.

Robot Framework writes test cases in a tabular, keyword-driven syntax, builds higher-level keywords from existing ones, and reports results in HTML. Custom keyword libraries are plain Python. In behavior-driven style, a test case reads as a requirement stakeholders who don't code can follow.

tox and Nox both create a virtual environment per Python version or dependency set and run your tests in each. tox defines its environments in a config file. Nox is configured in a noxfile.py, where each session is a Python function, so any logic your test matrix needs is ordinary code.

Selenium drives browsers through WebDriver, a W3C standard, and Selenium Grid runs those tests on other machines and platforms. Its guidelines model each page as a page object that makes no assertions, and use explicit waits for the exact condition each step needs. SeleniumBase builds on Selenium and runs under pytest: its methods wait for an element before acting, and failing tests save screenshots.

A Locust test is a Python program: an HttpUser class whose tasks make requests, so complex user flows are just code.

Schemathesis reads your OpenAPI or GraphQL schema and generates property-based tests from it, with no per-endpoint tests to maintain. Its FAQ recommends the CLI for most users, with a pytest integration for existing suites.

With unittest.mock, patch a name where it's looked up, which isn't always where it's defined. Autospec your mocks, so a test fails when your code calls an API the real object lacks. For the clock, time-machine mocks time functions everywhere they're referenced, where a patch only reaches the import location it targets.

For HTTP, pick by client. responses mocks Requests calls in a test wrapped in @responses.activate, and RESPX mocks HTTPX, with a respx_mock fixture for pytest. VCR.py records real HTTP interactions to a cassette file on the first run and replays them after, across many client libraries.

factory_boy replaces static fixtures with factories, where a test declares only the fields it cares about. It has base classes for Django, SQLAlchemy, and MongoEngine models. Polyfactory instead reads the type hints on dataclasses, TypedDicts, Pydantic models, and msgspec Structs to generate the data.

Run your suite under Coverage.py with coverage run -m pytest, then coverage report prints the results. For most purposes, you need no pytest plugin.

Faker generates names, addresses, and other data per locale. Mimesis covers the same ground, fully typed, and its benchmarks show it faster than Faker.

Random test data makes a failing build hard to reproduce, so seed it: Faker, factory_boy, and Mimesis all take a seed. For more tools, see awesome-python-testing.

Know a project that belongs here?

Tell us what it does and why it stands out.