Plain is headed towards 1.0! Subscribe for development updates →

Testing - pytest

Write and run tests with pytest.

Django includes its own test runner and unittest classes. But a lot of people (myself included) prefer pytest.

In Plain I've removed the Django test runner and a lot of the implications that come with it. There are a few utilities that remain to make testing easier, and plain-test is a wrapper around pytest.

 1import os
 2import sys
 3
 4import click
 5
 6import pytest
 7from plain.runtime import settings
 8
 9
10@click.command(
11    context_settings={
12        "ignore_unknown_options": True,
13    }
14)
15@click.argument("pytest_args", nargs=-1, type=click.UNPROCESSED)
16def cli(pytest_args):
17    """Run tests with pytest"""
18
19    plain_tmp_dir = str(settings.PLAIN_TEMP_PATH)
20
21    if not os.path.exists(os.path.join(plain_tmp_dir, ".gitignore")):
22        os.makedirs(plain_tmp_dir, exist_ok=True)
23        with open(os.path.join(plain_tmp_dir, ".gitignore"), "w") as f:
24            f.write("*\n")
25
26    # Turn deprecation warnings into errors
27    #     if "-W" not in pytest_args:
28    #         pytest_args = list(pytest_args)  # Make sure it's a list instead of tuple
29    #         pytest_args.append("-W")
30    #         pytest_args.append("error::DeprecationWarning")
31
32    # has to happen before setup() to be more useful? initial setup() may fail if doesn't have the required variables
33    # or don't set it by default at all... could warn on != TEST?
34    os.environ.setdefault("PLAIN_ENV", "test")
35
36    click.secho(f"Running pytest with PLAIN_ENV={os.environ['PLAIN_ENV']}", bold=True)
37
38    returncode = pytest.main(list(pytest_args))
39    if returncode:
40        sys.exit(returncode)