Skip to main content

A pytest fixture for testing flake8 plugins.

Project description

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

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: Mapping[str, str])

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)

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)

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)

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) -> Flake8Result

Runs flake8 in the current process, 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 it runs in the same process without multiprocessing - see source.

Flake8Result

Represents the parsed output of a flake8 run.

Flake8Result.out: str

The full string of output 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

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-flake8dir-2.5.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

pytest_flake8dir-2.5.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest-flake8dir-2.5.0.tar.gz.

File metadata

  • Download URL: pytest-flake8dir-2.5.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for pytest-flake8dir-2.5.0.tar.gz
Algorithm Hash digest
SHA256 1588fb9e146cb106a6cdc945d390ce982564c09cdf5a996dae12a985fa04cf9b
MD5 5f64069825515a5b2740541089b000a1
BLAKE2b-256 4d47b268308d335c187342c487365936775dcf57bcd2169da644bcf0c0898daa

See more details on using hashes here.

File details

Details for the file pytest_flake8dir-2.5.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_flake8dir-2.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for pytest_flake8dir-2.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e82642da14594616b30847745834a0b063a0bd9295cc720f92512f0972e7df1
MD5 e58d56af239668fa538caceb75cb9b99
BLAKE2b-256 c4d61fee469f5ee3fad94dc53be256b9cf70a25b6f35e5028d5b03b79ba4833d

See more details on using hashes here.

Supported by

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