# pytest browser test setup

> Write pytest JUnit XML, prepare the project's virtual environment, and make browser fixtures visible to KwikQA.

Use pytest's JUnit XML for diagnosis, repair, and validation. A failure group qualifies for repair when every failing test reaches Selenium, Playwright, Appium, SeleniumBase, or Splinter through your test code or a recognized browser fixture. Pytest unit tests can still be diagnosed.

## Prepare the Python environment

Create a `.venv` or `venv` at the pytest build root or workspace root. It needs to resolve inside the workspace and contain the project's own pytest and dependencies. Set up the browsers and drivers your tests use. KwikQA uses the project's interpreter on macOS, Linux, or Windows.

```
python3 -m venv .venv
.venv/bin/python -m pip install -r requirements.txt
```

On Windows, use `py -m venv .venv` and `.\.venv\Scripts\python.exe -m pip install -r requirements.txt`. If tests use the `page` or `driver` fixture from pytest-playwright, pytest-selenium, SeleniumBase, or pytest-splinter, install that plugin in this same virtual environment. For pytest-playwright, install the browser with `.venv/bin/python -m playwright install chromium` or your project's equivalent.

## Write JUnit XML

Use one of these configurations in the pytest build directory. `xunit1` includes each test's source line. Include `pythonpath` only when your project adds an import directory. The example uses an existing `src` directory.

### pyproject.toml

```
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "--junitxml=reports/pytest.xml"
junit_family = "xunit1"
pythonpath = ["src"]
```

### pytest.ini

```
[pytest]
testpaths = tests
addopts = --junitxml=reports/pytest.xml
junit_family = xunit1
pythonpath = src
```

For a one-off report, pass `--junitxml=reports/pytest.xml -o junit_family=xunit1` on the pytest command line. When using the config above, run:

```
.venv/bin/python -m pytest
npx kwikqa analyze --report reports/pytest.xml --html
```

On Windows use `.\.venv\Scripts\python.exe -m pytest`. Run analysis after pytest writes its report, including when a test fails. Add `--local` to generate a report with failure groups without signing in.

## Make fixtures and imports discoverable

- Put `pytest_plugins` in a top-level `conftest.py` as a literal dotted module name, or a literal list or tuple of those names. For example, `pytest_plugins = ["qa.fixtures"]` loads `qa/fixtures.py` from an import root. Use plain imports and literal fixture names.
- Write `pythonpath` once in the active pytest configuration. Use plain paths relative to that configuration directory. Each directory must be inside the workspace and reachable without symlinks. Use unquoted paths in INI and a string or string array in TOML. Environment variables, shell expansions, globs, and backslashes prevent KwikQA from following those paths reliably.

Dynamic plugin or fixture lookup can leave browser use or shared-test coverage unconfirmed. [End-to-end browser test troubleshooting](https://kwikqa.com/docs/troubleshooting/#browser-tests) explains the reason codes. Use `npx kwikqa fix --report reports/pytest.xml` when you start a repair. For eligible browser tests, continue with [your first repair](https://kwikqa.com/docs/first-repair/).

## Use an existing report

With `allure-pytest` installed, use the following for diagnosis. `--clean-alluredir` clears the output directory first so an analysis covers one run.

```
.venv/bin/python -m pytest --alluredir=reports/allure-results --clean-alluredir
npx kwikqa analyze --report reports/allure-results --html
```

The KwikQA pytest reporter can support repair and capture evidence during the test run. pytest CTRF can support repair when every test has a Python file identity. Use JUnit XML for the simplest validation setup. The CLI's `--capture` flag applies to the JavaScript browser runners.

See [pytest configuration](https://docs.pytest.org/en/stable/reference/customize.html) and [the support matrix](https://kwikqa.com/docs/setup/#supported-frameworks).

---

Source: https://kwikqa.com/docs/frameworks/pytest/, updated 2026-11-27
