Skip to main content

A pytest fixture for testing flake8 plugins.

Project description

https://img.shields.io/travis/adamchainz/pytest-flake8dir/master.svg https://img.shields.io/pypi/v/pytest-flake8dir.svg

A pytest fixture for testing flake8 plugins.

A quick example:

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

Installation

Use pip:

pip install pytest-flake8dir

Tested on Python 2.7 and Python 3.6.

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.

flake8dir.make_py_files(**kwargs)

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 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_setup_cfg(contents)

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.run_flake8(extra_args=None)

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

The full string of output generated by flake8.

Flake8Result.out_lines

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

History

Pending Release

1.0.0 (2017-06-22)

  • First version with basic fixture supporting make_py_files, make_setup_cfg and run_flake8.

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-1.0.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

pytest_flake8dir-1.0.0-py2.py3-none-any.whl (6.6 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for pytest-flake8dir-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d9a3f563f8324e4db4c6d54df5f953d0883fbd059426e21d1760eb39ee6cf2ac
MD5 6a7f721d7ca37c5ad81f9e32701e9e82
BLAKE2b-256 29b1e56da3d5afdef2fbfeb64c9d7c0e2eec2be7be18d36bab03c2a56d5bd96c

See more details on using hashes here.

File details

Details for the file pytest_flake8dir-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_flake8dir-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 21f58f59b774eea4298ccc4c6d6b1e5a8b8320a21bdb60068b4c92eacb3a9b10
MD5 d30530cc78eecd17befb0841d2da8607
BLAKE2b-256 364525e88ee4720eb2b42a1528fba570fd3c90018159081327bbf021a216ba3d

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