Skip to main content

A pytest fixture for testing flake8 plugins.

Project description

https://img.shields.io/github/actions/workflow/status/adamchainz/pytest-flake8-path/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/pypi/v/pytest-flake8-path.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

A pytest fixture for testing flake8 plugins.

A quick example:

def test_simple_run(flake8_path):
    (flake8_path / "example.py").write_text("x  = 1\n")

    result = flake8_path.run_flake8()

    assert result.out_lines == [
        "./example.py:1:2: E221 multiple spaces before operator"
    ]

Working on a Django project? Check out my book Speed Up Your Django Tests which covers loads of ways to write faster, more accurate tests.


Installation

Use pip:

python -m pip install pytest-flake8-path

Python 3.9 to 3.14 supported.

API

flake8_path fixture

A pytest fixture that wraps Pytest’s built-in tmp_path fixture (docs), to create a temporary directory, allow adding files, and running flake8. It acts like a pathlib.Path object, with one extra method.

If you’re using this to test a flake8 plugin, make sure flake8 is picking up your plugin during tests. Normally this is done with a setup.cfg entrypoint, which makes tox the easiest way to guarantee this is ready as it will install your project before running tests.

flake8dir.run_flake8(extra_args: list[str] | None = None) -> Flake8Result

Runs flake8 and returns a Flake8Result representing the results.

extra_args may be a list of extra flags to pass to flake8, for example passing ["--ignore", "E101"]. Note some arguments are already passed to ensure Flake8 runs in an isolated manner - see source.

Flake8Result

Represents the parsed output of a flake8 run.

Flake8Result.out: str

The full string of output (stdout) generated by flake8.

Flake8Result.err: str

The full string of error output (stderr) generated by flake8.

Flake8Result.exit_code: int

The exit code that the flake8 run exited with.

Flake8Result.out_lines: list[str]

A list of individual lines of output, without trailing newlines. This is the most useful tool for making assertions against.

On Windows, file paths are normalized into the Unix format (\ is replaced with /). This allows test suites to run the same on all operating systems.

For example, given a result you can check for a particular line being output:

result = flake8_path.run_flake8()
expected = "./example.py:1:2: E221 multiple spaces before operator"
assert expected in result.out_lines

Flake8Result.err_lines: list[str]

Like out_lines, but for error output.

Examples

Basic

Using Path.write_text() (docs) we can create an example.py file with code. Then with flake8_path.run_flake8() we can run flake8 and check for an expected error:

def test_simple(flake8_path):
    (flake8_path / "example.py").write_text("x  = 1\n")

    result = flake8_path.run_flake8()

    assert result.out_lines == [
        "./example.py:1:2: E221 multiple spaces before operator"
    ]
    assert result.err_lines == []
    assert result.exit_code == 1

With dedent

The standard library’s textwrap.dedent() (docs) is useful for including multi-line files. Use a triple quoted multi-line string, with an initial backslash to prevent a blank first line:

def test_multi_line(flake8_path):
    (flake8_path / "example.py").write_text(
        dedent(
            """\
            x  = 1
            y  = 2
            """
        )
    )

    result = flake8_path.run_flake8()

    assert result.out_lines == [
        "./example.py:1:2: E221 multiple spaces before operator",
        "./example.py:2:2: E221 multiple spaces before operator",
    ]
    assert result.err_lines == []
    assert result.exit_code == 1

Configuring flake8

Write a setup.cfg file to configure flake8 before running it:

def test_with_setup_cfg(flake8_path):
    (flake8_path / "setup.cfg").write_text(
        dedent(
            """\
            [flake8]
            ignore = E221
            """
        )
    )
    (flake8_path / "example.py").write_text("x  = 1\n")

    result = flake8_path.run_flake8()

    assert result.out_lines == []
    assert result.err_lines == []
    assert result.exit_code == 0

History

pytest-flake8-path is the successor to pytest-flake8dir. pytest-flake8dir was based upon pytest’s tmpdir fixture, which returned a legacy py.path.local object. Since version 3.9.0, pytest has provided the tmp_path fixture, which returns a standard library pathlib.Path object. pytest-flake8-path is a rewrite of pytest-flake8dir to use tmp_path instead of tmpdir.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_flake8_path-1.7.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_flake8_path-1.7.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_flake8_path-1.7.0.tar.gz.

File metadata

  • Download URL: pytest_flake8_path-1.7.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_flake8_path-1.7.0.tar.gz
Algorithm Hash digest
SHA256 bbb696a61d5845f44668302ad0c48af106c0eeef70bcc807ad59999ef45467fd
MD5 66aa3dbb73f2fa50e836c0d4fa7b9e9c
BLAKE2b-256 70e7df41320fcca7530d28150d55c2b459d1b1673ee5ad287eaef01d93846bfc

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_flake8_path-1.7.0.tar.gz:

Publisher: main.yml on adamchainz/pytest-flake8-path

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_flake8_path-1.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_flake8_path-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5fe2aab287853bfbccfc3e9ded8566490ed598232509ac97fb383099c61e3e60
MD5 4fe185b3dcae397c6f34d5a28ebed0e7
BLAKE2b-256 41109514f48548094287860dd4dfa1281c7c78b150c8cb7d35e4b59f71856972

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_flake8_path-1.7.0-py3-none-any.whl:

Publisher: main.yml on adamchainz/pytest-flake8-path

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page