Package: pytest-flake8dir
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:deftest_simple_run(flake8dir):flake8dir.make_example_py("""
        x  = 1
    """)result=flake8dir.run_flake8()assertresult.out_lines==["./example.py:1:2: E221 multiple spaces before operator"]InstallationUsepip:python-mpipinstallpytest-flake8dirPython 3.6 to 3.10 supported.Working on a Django project?Check out my bookSpeed Up Your Django Testswhich covers loads of best practices so you can write faster, more accurate tests.APIflake8dirfixtureA pytest fixture that wraps Pytest’s built-intmpdirfixture
(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 asetup.pyentrypoint,
which makestoxthe easiest way to guarantee this is ready as it will runsetup.py installon your project before running tests.flake8dir.make_py_files(**kwargs:str)->NoneCreates 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 withtextwrap.dedent()so mixed indentation is not a problem in your test files.For example, this creates two python files in the temporary directory, calledexample1.pyandexample2.py, each containing one line with an
assignment:deftest_sample(flake8dir):flake8dir.make_py_files(example1="""
            x = 1
        """,example2="""
            y = 1
        """,)flake8dir.make_example_py(content: str)->NoneA shortcut formake_py_files(example=content), for when you are using a
single file over and over. This creates justexample.py, which is often
all you need for testing a rule.For example:deftest_sample(flake8dir):flake8dir.make_example_py("""
        x = 1
    """)flake8dir.make_setup_cfg(contents: str)->strMakes the filesetup.cfgin the test directory with contents equal to the
string passed in. This is again processed withtextwrap.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:deftest_sample(flake8dir):flake8dir.make_setup_cfg("""
        [flake8]
        ignore = E101
    """)flake8dir.make_file(filename: str, content: str)->NoneMake an arbitrary file with the given filename - this function is the inner
implementation formake_py_filesandmake_setup_cfg.filenamemay
include directories, likemydir/foo.py, and they will be created.contentis subject to the sametextwrap.dedent()processing as
mentioned above.For example:deftest_sample(flake8dir):flake8dir.make_file("myfile/foo.py","""
        x = 1
    """,)flake8dir.run_flake8(extra_args: list[str] | None = None)->Flake8ResultRuns flake8 and returns aFlake8Resultrepresenting the results.extra_argsmay be a list of extra flags to pass to flake8, for example
passing["--ignore","E101"]would achieve the same thing as the abovesetup.cfgexample. Note some arguments are already passed to ensure flake8
runs in an isolated manner - see source.Flake8ResultRepresents the parsed output of a flake8 run.Flake8Result.out: strThe full string of output (stdout) generated by flake8.Flake8Result.err: strThe full string of error output (stderr) generated by flake8.Flake8Result.exit_code: intThe 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"assertexpectedinresult.out_linesFlake8Result.err_lines: list[str]Likeout_lines, but for error output.