pytest插件,用于检查PEP8要求
项目描述
py.test插件,用于高效检查PEP8合规性
使用方法
通过以下方式安装
pip install pytest-pep8
如果您输入以下内容
py.test --pep8
则从命令行参数开始,将发现所有以.py结尾的文件,并执行pep8检查。
一个小例子
如果您有一个违反PEP8的文件,如下所示
# content of myfile.py somefunc( 123,456)
您可以使用已安装的插件运行它
$ py.test --pep8 =========================== test session starts ============================ platform linux2 -- Python 2.7.6 -- py-1.4.30 -- pytest-2.7.2 rootdir: /tmp/doc-exec-2, inifile: plugins: pep8, cache collected 1 items myfile.py F ================================= FAILURES ================================= ________________________________ PEP8-check ________________________________ /tmp/doc-exec-2/myfile.py:2:10: E201 whitespace after '(' somefunc( 123,456) ^ /tmp/doc-exec-2/myfile.py:2:14: E231 missing whitespace after ',' somefunc( 123,456) ^ ========================= 1 failed in 0.00 seconds =========================
有关(E)错误和(W)警告代码的含义,请参阅运行文件时的错误输出,或查看pep8.py。
现在我们不修复PEP8错误
# content of myfile.py somefunc(123, 456)
再次运行
$ py.test --pep8 =========================== test session starts ============================ platform linux2 -- Python 2.7.6 -- py-1.4.30 -- pytest-2.7.2 rootdir: /tmp/doc-exec-2, inifile: plugins: pep8, cache collected 1 items myfile.py . ========================= 1 passed in 0.00 seconds =========================
现在pep8检查通过了。此外,如果您再次运行它(并报告跳过原因)
$ py.test --pep8 -rs =========================== test session starts ============================ platform linux2 -- Python 2.7.6 -- py-1.4.30 -- pytest-2.7.2 rootdir: /tmp/doc-exec-2, inifile: plugins: pep8, cache collected 1 items myfile.py s ========================= short test summary info ========================== SKIP [1] /home/hpk/p/pytest-pep8/pytest_pep8.py:65: file(s) previously passed PEP8 checks ======================== 1 skipped in 0.00 seconds =========================
您可以看到,pep8检查被跳过,因为文件自上次检查以来未更改。由于pep8插件使用pytest-cache插件来实现其缓存,因此您可以使用其--clearcache选项来删除所有pytest缓存,包括与pep8相关的缓存,这将触发pep8检查代码再次运行
$ py.test --pep8 --clearcache =========================== test session starts ============================ platform linux2 -- Python 2.7.6 -- py-1.4.30 -- pytest-2.7.2 rootdir: /tmp/doc-exec-2, inifile: plugins: pep8, cache collected 1 items myfile.py . ========================= 1 passed in 0.00 seconds =========================
按项目和文件配置PEP8选项
您可以通过在setup.cfg
或setup.cfg
文件中添加一个pep8ignore
条目来配置项目的PEP8检查选项,如下所示:
# content of setup.cfg [pytest] pep8ignore = E201 E231
这将全局防止对两个空格问题的投诉。使用上述示例重新运行现在看起来会更好。
$ py.test -q --pep8 . 1 passed in 0.00 seconds
如果您有一些文件,您想特别忽略某些错误或警告,您可以在pep8ignore行中开始一个glob模式和一个以空格分隔的代码列表。
# content of setup.cfg [pytest] pep8ignore = *.py E201 doc/conf.py ALL
所以如果您的conf.py如下所示:
# content of doc/conf.py func ( [1,2,3]) #this line lots pep8 errors :)
然后再次使用前面的示例运行将显示单个失败,并且将完全忽略doc/conf.py。
$ py.test --pep8 -v # verbose shows what is ignored =========================== test session starts ============================ platform linux2 -- Python 2.7.6 -- py-1.4.30 -- pytest-2.7.2 -- /home/hpk/venv/clean/bin/python cachedir: /tmp/doc-exec-2/.cache rootdir: /tmp/doc-exec-2, inifile: setup.cfg plugins: pep8, cache collecting ... collected 1 items myfile.py PASSED ========================= 1 passed in 0.01 seconds =========================
请注意,doc/conf.py没有被考虑或导入。
如果您想有超过79个字符的较长的行(这是PEP8检查器的默认值),您可以这样配置它:
# content of setup.cfg [pytest] pep8maxlinelength = 99
运行PEP8检查而不运行其他测试
您还可以通过输入来限制您的测试运行仅执行“pep8”测试而不执行任何其他测试:
py.test --pep8 -m pep8
这将仅运行带有“pep8”标记的测试项,这是该插件动态添加的。
注意
此插件的存储库在http://bitbucket.org/pytest-dev/pytest-pep8
有关pytest的更多信息,请参阅https://pytest.cn
代码部分基于Ronny Pfannschmidt的pytest-codecheckers插件。
项目详情
pytest-pep8-1.0.6.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 032ef7e5fa3ac30f4458c73e05bb67b0f036a8a5cb418a534b3170f89f120318 |
|
MD5 | 3debd0bac8f63532ae70c7351e73e993 |
|
BLAKE2b-256 | 1f1cc834344ef39381558b047bea1e3005197fa8457c199d58219996ca07defb |