Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

https://img.shields.io/github/workflow/status/adamchainz/pytest-flake8dir/CI/main?style=for-the-badge https://img.shields.io/pypi/v/pytest-flake8dir.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Unmaintained: Following temporary directory fixture changes in pytest, I made a successor library, pytest-flake8-path. I recommend you use that instead.


A pytest fixture for testing flake8 plugins.

A quick example:

def test_simple_run(flake8dir):
    flake8dir.make_example_py(
        """
        x  = 1
    """
    )
    result = flake8dir.run_flake8()
    assert result.out_lines == [
        "./example.py:1:2: E221 multiple spaces before operator"
    ]

Installation

Use pip:

python -m pip install pytest-flake8dir

Python 3.6 to 3.10 supported.


Working on a Django project? Check out my book Speed Up Your Django Tests which covers loads of best practices so you can write faster, more accurate tests.


API

flake8dir fixture

A pytest fixture that wraps Pytest’s built-in tmpdir fixture (docs), to create a temporary directory, allow adding files, and running flake8.

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.py entrypoint, which makes tox the easiest way to guarantee this is ready as it will run setup.py install on your project before running tests.

flake8dir.make_py_files(**kwargs: str) -> None

Creates one python file for each passed keyword argument, with name corresponding to the keyword argument + ‘.py’, and content according the string value of the argument. The value will be processed with textwrap.dedent() so mixed indentation is not a problem in your test files.

For example, this creates two python files in the temporary directory, called example1.py and example2.py, each containing one line with an assignment:

def test_sample(flake8dir):
    flake8dir.make_py_files(
        example1="""
            x = 1
        """,
        example2="""
            y = 1
        """,
    )

flake8dir.make_example_py(content: str) -> None

A shortcut for make_py_files(example=content), for when you are using a single file over and over. This creates just example.py, which is often all you need for testing a rule.

For example:

def test_sample(flake8dir):
    flake8dir.make_example_py(
        """
        x = 1
    """
    )

flake8dir.make_setup_cfg(contents: str) -> str

Makes the file setup.cfg in the test directory with contents equal to the string passed in. This is again processed with textwrap.dedent() so indentation is not a worry. You’ll probably want to set the [flake8] section header to configure flake8.

For example, this makes flake8 ignore rule E101:

def test_sample(flake8dir):
    flake8dir.make_setup_cfg(
        """
        [flake8]
        ignore = E101
    """
    )

flake8dir.make_file(filename: str, content: str) -> None

Make an arbitrary file with the given filename - this function is the inner implementation for make_py_files and make_setup_cfg. filename may include directories, like mydir/foo.py, and they will be created. content is subject to the same textwrap.dedent() processing as mentioned above.

For example:

def test_sample(flake8dir):
    flake8dir.make_file(
        "myfile/foo.py",
        """
        x = 1
    """,
    )

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"] would achieve the same thing as the above setup.cfg example. 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.

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

result = flake8dir.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.

Metadata

Release files for pytest-flake8dir 2.6.1

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

Source distribution (sdist)

Source distribution for pytest-flake8dir 2.6.1
File Size Uploaded
pytest-flake8dir-2.6.1.tar.gz 7.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-flake8dir 2.6.1
File Interpreter ABI Platform
pytest_flake8dir-2.6.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / pytest-flake8dir-2.6.1.tar.gz

Download URL pytest-flake8dir-2.6.1.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a368d1f9366b9f8084da24f324a65d312ca129d363f1a0cbdc7409376bb9e66f
BLAKE2b-256 checksum
How to use checksums
173cf056fd10c8608474d950668c7c8628c0f4fc3da502f6ae6bef6d3818eddb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

Release files / pytest_flake8dir-2.6.1-py3-none-any.whl

Download URL pytest_flake8dir-2.6.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f37df8b9e11cdb5c6f2d8c907ce145c686e31d80b2a1f6a887534bda3288ac66
BLAKE2b-256 checksum
How to use checksums
547365ed799674c72939aa1456e1ee18c0b85cfb6e73cacd7670785f80b89a4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6
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