Skip to main content

plain.pytest

Run tests with pytest and useful fixtures for Plain applications.

Overview

You can run tests using the plain test command, which wraps pytest and automatically loads environment variables from .env.test if it exists.

plain test

Any additional arguments are passed directly to pytest.

plain test -v --tb=short
plain test tests/test_views.py

A basic test looks like this:

from plain.test import Client


def test_homepage():
    client = Client()
    response = client.get("/")
    assert response.status_code == 200

The Client class comes from plain.test and lets you make requests to your app without starting a server.

Fixtures

settings

The settings fixture provides access to your Plain settings during tests. Any modifications you make are automatically restored when the test completes.

def test_debug_mode(settings):
    settings.DEBUG = True
    assert settings.DEBUG is True
    # After this test, DEBUG is restored to its original value

testbrowser

The testbrowser fixture gives you a TestBrowser instance that wraps Playwright and runs a real Plain server in the background. This is useful for end-to-end browser testing.

def test_login_page(testbrowser):
    page = testbrowser.new_page()
    page.goto("/login/")
    assert page.title() == "Login"

The browser connects to a test server running over HTTPS on a random available port. Self-signed certificates are generated automatically.

Authentication helpers

You can log in a user without going through the login form using force_login.

def test_dashboard(testbrowser, user):
    testbrowser.force_login(user)
    page = testbrowser.new_page()
    page.goto("/dashboard/")
    assert "Welcome" in page.content()

To log out, use logout, which clears all cookies.

def test_logout_clears_session(testbrowser, user):
    testbrowser.force_login(user)
    testbrowser.logout()
    page = testbrowser.new_page()
    page.goto("/dashboard/")
    assert "Login" in page.content()

URL discovery

The discover_urls method crawls your site starting from given URLs and returns all discovered internal links. This is useful for smoke testing.

def test_no_broken_links(testbrowser, user):
    testbrowser.force_login(user)
    urls = testbrowser.discover_urls(["/"])
    assert len(urls) > 0

Database isolation

If plain.postgres is installed, the testbrowser fixture automatically uses the isolated_db fixture and passes the database connection to the test server. This means your browser tests and your test code share the same database state.

FAQs

Do I need Playwright installed?

The testbrowser fixture requires playwright and pytest-playwright to be installed, but they are not dependencies of this package. If you only use the settings fixture, you don't need Playwright.

uv add playwright pytest-playwright --dev
playwright install

How do I use a .env.test file?

Create a .env.test file in your project root with test-specific environment variables. The plain test command automatically loads it before running pytest.

# .env.test
DATABASE_URL=postgres://localhost/myapp_test
SECRET_KEY=test-secret-key

How do I run a specific test?

Pass the test path and any pytest options after plain test.

plain test tests/test_views.py::test_homepage -v

Installation

Install the plain.pytest package from PyPI:

uv add plain.pytest --dev

Create a conftest.py in your tests directory to make the fixtures available:

# tests/conftest.py
from plain.pytest.plugin import settings, testbrowser  # noqa: F401

If you're using the testbrowser fixture, also install Playwright:

uv add playwright pytest-playwright --dev
playwright install chromium

Metadata

Release files for plain.pytest 0.17.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for plain.pytest 0.17.0
File Size Uploaded Provenance Actions
plain_pytest-0.17.0.tar.gz 9.6 kB Without provenance Details for plain_pytest-0.17.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for plain.pytest 0.17.0
File Interpreter ABI Platform Actions
plain_pytest-0.17.0-py3-none-any.whl Python 3 none any Details for plain_pytest-0.17.0-py3-none-any.whl

Total release size: 23.6 kB

Release files / plain_pytest-0.17.0.tar.gz

Download URL plain_pytest-0.17.0.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
30d76ca05ad5e3deb553b53259529857e064a63ae6337cea9bb504a2c1cbc003
BLAKE2b-256 checksum
How to use checksums
5a43893f8e0505c5d9560da2dd2e1336c1488d5d22cbd9102b5e3ec3efe2bc17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / plain_pytest-0.17.0-py3-none-any.whl

Download URL plain_pytest-0.17.0-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03c234392c6d418344798c02b73fc7f70088af6a9fb618e5e2fc2b5af497eb71
BLAKE2b-256 checksum
How to use checksums
f741897d4e61fd8aa7c99eec0efa60364a9e56362fa67a00acc8f0686b6f46dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for plain.pytest 0.17.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.17.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page