A flake8 plugin checking common style issues or inconsistencies with pytest-based tests.
Project description
flake8-pytest-style
Description
A flake8 plugin checking common style issues or inconsistencies with pytest-based tests.
Currently the following errors are reported:
-
PT001 use @pytest.fixture() over @pytest.fixture
The preferred style is controlled by the configuration optionpytest-fixture-no-parentheses -
PT002 configuration for fixture '{name}' specified via positional args, use kwargs
e.g.@pytest.fixture(scope='module')is OK, and@pytest.fixture('module')is an error -
PT003 scope='function' is implied in @pytest.fixture()
e.g.@pytest.fixture(scope='function')should be replaced with@pytest.fixture() -
PT004 fixture '{name}' does not return anything, add leading underscore -
PT005 fixture '{name}' returns a value, remove leading underscore -
PT006 wrong name(s) type in @pytest.mark.parametrize, expected {expected_type}
For a single name the expected type is always a plain string. For multiple names the expected type is controlled by the configuration variablepytest-parametrize-names-type. -
PT007 wrong values type in @pytest.mark.parametrize, expected {expected_type}The expected type of the list of rows is controlled by the configuration variablepytest-parametrize-values-type. The expected type of each row in case of multiple arguments is controlled by the configuration variablepytest-parametrize-values-row-type. -
PT008 use return_value= instead of patching with lambda
e.g.mocker.patch('target', return_value=7)is OK, andmocker.patch('target', lambda *args: 7)is an error -
PT009 use a regular assert instead of unittest-style '{assertion}' -
PT010 set the expected exception in pytest.raises() -
PT011 set the match parameter in pytest.raises({exception})
e.g.pytest.raises(ValueError, match='exception text')is OK, andpytest.raises(ValueError)is an error.
The exception list is controlled by the configuration optionpytest-raises-require-match-for. -
PT012 pytest.raises() block should contain a single simple statement
this forbids multiple statements and control flow structures withinpytest.raises()blocks. -
PT013 found incorrect import of pytest, use simple 'import pytest' instead -
PT014 found duplicate test cases {indexes} in @pytest.mark.parametrize -
PT015 assertion always fails, replace with pytest.fail()
e.g.pytest.fail()should be preferred toassert Falseorassert 0 -
PT016 no message passed to pytest.fail() -
PT017 found assertion on exception {name} in except block, use pytest.raises() instead
e.g.:try: 1 / 0 except ZeroDivisionError as e: assert e.args
is an error and should be replaced with
with pytest.raises(ZeroDivisionError) as e: 1 / 0 assert e.value.args
-
PT018 assertion should be broken down into multiple parts
e.g.assert something and something_elseshould be rewritten as separateassert somethingandassert something_else, andassert not (something or something_else)should be rewritten asassert not somethingandassert not something_else -
PT019 fixture {name} without value is injected as parameter, use @pytest.mark.usefixtures instead
Installation
pip install flake8-pytest-style
Configuration
The plugin has the following configuration options:
-
pytest-fixture-no-parentheses
Boolean flag specifying whether@pytest.fixture()without parameters should have parentheses, as checked byPT001.
If the option is set to false (the default),@pytest.fixture()is valid and@pytest.fixtureis an error.
If set to true,@pytest.fixtureis valid and@pytest.fixture()is an error. -
pytest-parametrize-names-type
Expected type for multiple argument names in@pytest.mark.parametrize, as checked byPT006. The following values are supported:csv— a comma-separated list, e.g.@pytest.mark.parametrize('name1,name2', ...)tuple(default) — e.g.@pytest.mark.parametrize(('name1', 'name2'), ...)list— e.g.@pytest.mark.parametrize(['name1', 'name2'], ...)
-
pytest-parametrize-values-type
Expected type for the list of values rows in@pytest.mark.parametrize, as checked byPT007. The following values are supported:tuple— e.g.@pytest.mark.parametrize('name', (1, 2, 3))list(default) — e.g.@pytest.mark.parametrize('name', [1, 2, 3])
-
pytest-parametrize-values-row-type
Expected type for each row of values in@pytest.mark.parametrizein case of multiple parameters, as checked byPT007. The following values are supported:tuple(default) — e.g.@pytest.mark.parametrize(('name1', 'name2'), [(1, 2), (3, 4)])list— e.g.@pytest.mark.parametrize(('name1', 'name2'), [[1, 2], [3, 4]])
-
pytest-raises-require-match-for
Comma-separated list of exception names that require amatch=parameter in apytest.raises()call, as checked byPT011. By default the list contains the following exceptions:BaseException,ExceptionValueErrorOSError,IOError,EnvironmentError,socket.error
For developers
Install deps and setup pre-commit hook
make init
Run linters, autoformat, tests etc.
make format lint test
Bump new version
make bump_major
make bump_minor
make bump_patch
License
MIT
Change Log
Unreleased
...
1.1.1 - 2020-04-17
- fix
PT011not reportingmatch=''as a violation
1.1.0 - 2020-04-14
- add
PT015(checks forassert False) - add
PT016(checks forpytest.fail()without message) - add
PT017(checks for assertions on exceptions inexceptblocks) - add
PT018(checks for composite assertions) - add
PT019(checks for fixtures without value injected as parameters)
1.0.0 - 2020-03-26
- add
PT014(checks for duplicate test cases in@pytest.mark.parametrize)
0.6.0 - 2020-03-21
- add configuration option
pytest-parametrize-names-typeforPT006 - add configuration options
pytest-parametrize-values-typeandpytest-parametrize-values-row-typeforPT007
0.5.0 - 2020-03-09
- add configuration option
pytest-fixture-no-parenthesesforPT001 - add
PT013(checks forfrom-imports frompytest)
0.4.0 - 2020-03-09
- add
PT012(checks for multiple statements inwith pytest.raises())
0.3.1 - 2020-03-09
- fix default value of
pytest-raises-require-match-forconfig option
0.3.0 - 2020-03-09
- add
PT010andPT011(checks forpytest.raisesparameters)
0.2.0 - 2020-03-01
- add
PT009(ported from flake8-pytest)
0.1.3 - 2019-05-24
- add
yieldfixtures support - fix changelog entry for 0.1.2
0.1.2 - 2019-05-23
- fix parametrize checkers not working in decorators
0.1.1 - 2019-05-23
- update PyPI description
0.1.0 - 2019-05-23
- initial
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flake8-pytest-style-1.1.1.tar.gz.
File metadata
- Download URL: flake8-pytest-style-1.1.1.tar.gz
- Upload date:
- Size: 14.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.8.0 Linux/4.15.0-1028-gcp
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d91ffba047964e5bb4491dc71771008fa0438988870d59d4162bdba3674fb7a8
|
|
| MD5 |
f2229fa261492ca53b1b57f80da969b2
|
|
| BLAKE2b-256 |
72379e1d87f4801668e256efb08d5fb98d7e404f19669d5dad84988f015a3816
|
File details
Details for the file flake8_pytest_style-1.1.1-py3-none-any.whl.
File metadata
- Download URL: flake8_pytest_style-1.1.1-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.8.0 Linux/4.15.0-1028-gcp
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
690ef9aece18561b0e674e4e4ee7e80a0d38f27bfb268806fac5e55801396ee4
|
|
| MD5 |
0b1ff8dcfbdad0d8372ba7ce9432cae6
|
|
| BLAKE2b-256 |
43fcc5522c02fa534b36993228ac31baca60c641fd5bbca01b077a7508cd1574
|