skip to navigation
skip to content

pytest-flake8dir 1.1.0

A pytest fixture for testing flake8 plugins.

A pytest fixture for testing flake8 plugins.

A quick example:

def test_simple_run(flake8dir):
        x  = 1
    result = flake8dir.run_flake8()
    assert result.out_lines == [
        './ E221 multiple spaces before operator'


Use pip:

pip install pytest-flake8dir

Tested on Python 2.7 and Python 3.6.


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.


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 indentation is not a problem when writing tests.

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

def test_sample(flake8dir):
            x = 1
            y = 1


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

For example:

def test_sample(flake8dir):
        x = 1


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):
        ignore = E101

flake8dir.make_file(filename, content)

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/, 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/', '''
        x = 1


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.


Represents the parsed output of a flake8 run.


The full string of output generated by flake8.


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 = './ E221 multiple spaces before operator'
assert expected in result.out_lines


Pending Release

1.1.0 (2017-06-23)

  • Add convenience methods make_example_py and make_file.

1.0.0 (2017-06-22)

  • First version with basic fixture supporting make_py_files, make_setup_cfg and run_flake8.
File Type Py Version Uploaded on Size
pytest-flake8dir-1.1.0.tar.gz (md5, pgp) Source 2017-06-23 5KB
pytest_flake8dir-1.1.0-py2.py3-none-any.whl (md5, pgp) Python Wheel 2.7 2017-06-23 7KB