Package: pytest-flake8-path
A pytest fixture for testing flake8 plugins.A quick example:deftest_simple_run(flake8_path):(flake8_path/"example.py").write_text("x  = 1\n")result=flake8_path.run_flake8()assertresult.out_lines==["./example.py:1:2: E221 multiple spaces before operator"]InstallationUsepip:python-mpipinstallpytest-flake8-pathPython 3.8 to 3.12 supported.Working on a Django project?Check out my bookSpeed Up Your Django Testswhich covers loads of ways to write faster, more accurate tests.APIflake8_pathfixtureA pytest fixture that wraps Pytest’s built-intmp_pathfixture
(docs), to create
a temporary directory, allow adding files, and running flake8. It acts like apathlib.Pathobject, 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 asetup.cfgentrypoint, which makestoxthe 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)->Flake8ResultRuns flake8 and returns aFlake8Resultrepresenting the results.extra_argsmay 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.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.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"assertexpectedinresult.out_linesFlake8Result.err_lines: list[str]Likeout_lines, but for error output.ExamplesBasicUsingPath.write_text()(docs) we can create anexample.pyfile with code.
Then withflake8_path.run_flake8()we can run flake8 and check for an expected error:deftest_simple(flake8_path):(flake8_path/"example.py").write_text("x  = 1\n")result=flake8_path.run_flake8()assertresult.out_lines==["./example.py:1:2: E221 multiple spaces before operator"]assertresult.err_lines==[]assertresult.exit_code==1With dedentThe standard library’stextwrap.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:deftest_multi_line(flake8_path):(flake8_path/"example.py").write_text(dedent("""\
            x  = 1
            y  = 2
            """))result=flake8_path.run_flake8()assertresult.out_lines==["./example.py:1:2: E221 multiple spaces before operator","./example.py:2:2: E221 multiple spaces before operator",]assertresult.err_lines==[]assertresult.exit_code==1Configuring flake8Write asetup.cfgfile to configure flake8 before running it:deftest_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()assertresult.out_lines==[]assertresult.err_lines==[]assertresult.exit_code==0Historypytest-flake8-path is the successor topytest-flake8dir.
pytest-flake8dir was based upon pytest’stmpdirfixture, which returned a legacypy.path.localobject.
Since version 3.9.0, pytest has provided thetmp_pathfixture, which returns a standard librarypathlib.Pathobject.
pytest-flake8-path is a rewrite of pytest-flake8dir to usetmp_pathinstead oftmpdir.