Skip to content

API Testing with Pytest and Playwright

05-08-2026 Roy de Kleijn
API testing with Pytest and Playwright banner

Playwright for Python includes an API testing client, and you can drive it with Pytest through the pytest-playwright plugin. Instead of the requests library, the HTTP calls go through Playwright's APIRequestContext. For a team that already uses Playwright for browser tests in Python, this means one tool covers both the user interface and the API, with Pytest as the familiar runner underneath.

How it works

The plugin gives you a Playwright instance, and from it you build a request context bound to a base URL. That context sends the calls and returns responses you can read and assert on. A session scoped fixture keeps one context alive for the whole run:

@pytest.fixture(scope="session")
def api_request(playwright_instance: Playwright, base_url) -> APIRequestContext:
    request_context = playwright_instance.request.new_context(base_url=base_url)
    yield request_context
    request_context.dispose()

Tests then call methods on that context, read the status and parse the JSON body, and assert with plain Python. A login step captures the token and protected requests send it as a Bearer header. You run the suite with pytest, the same command as any other Pytest project.

Benefits

The main benefit is a shared tool. If Playwright already drives your browser tests in Python, using its request context for API tests keeps everything in one dependency and one mental model. The context handles base URL, headers and cookies cleanly, and it is a modern, well maintained client. Pytest brings its fixtures and its large plugin ecosystem, so you keep the structure and reporting you are used to.

Downsides

For an API only project this pulls in Playwright and its browser download when a plain HTTP library would be lighter and simpler. The request context does not parse or assert on JSON for you, so you write those checks by hand, just as you would with requests. It is worth noting that in this example some tests use full absolute URLs rather than the context base URL, which is easy to do and quietly undoes the benefit of configuring a base URL in one place. Keep paths relative so the base URL stays the single source of truth.

What the example project tests

The repository at github.com/testsmith-io/api-test-automation-python-pytest-playwright tests the Practice Software Testing API at https://api.practicesoftwaretesting.com, the ToolShop backend used across this series. A conftest.py builds the request context fixture.

Three scenarios are covered. A brands test calls GET /brands and checks for status 200 with at least two entries. A login test posts credentials to /users/login and confirms the access_token. A protected invoices test logs in, sends the Bearer token to GET /invoices, and checks the returned list. A GitHub Actions workflow runs pytest on push and on a schedule. The scenarios match the requests based Python example, which makes the two Python approaches easy to compare directly.

Share:
Roy de Kleijn

Test Automation Expert

With over 15 years of experience in software testing and test automation, Roy helps teams improve their testing processes.

Want to learn more?

Check out our trainings and take your test automation skills to the next level.