使用isort检查导入顺序的pytest插件
项目描述
支持
Python 3.7, 3.8, 3.9和3.10。pytest>=5。
用法
使用pip安装
pip install pytest-isort
在调用pytest时激活isort检查
py.test --isort
这将针对每个.py文件执行isort检查(如果未忽略)。
示例
假设您有一些导入顺序错误的文件
# content of file1.py import os import sys import random # content of file2.py import json import sys import os
如果您运行pytest并激活isort插件,您将看到如下内容
$ py.test --isort ========================= test session starts ========================== platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4 plugins: isort collected 2 items file1.py F file2.py F =============================== FAILURES =============================== _____________________________ isort-check ______________________________ ERROR: file1.py Imports are incorrectly sorted. import os +import random import sys -import random _____________________________ isort-check ______________________________ ERROR: file2.py Imports are incorrectly sorted. import json +import os import sys -import os ======================= 2 failed in 0.02 seconds =======================
如果您无法更改file2.py的导入顺序,您可以选择从isort检查中排除file2.py。
只需将isort_ignore设置添加到您的pytest配置文件中
[pytest] isort_ignore = file2.py
然后重新运行测试
$ py.test --isort ========================= test session starts ========================== platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4 plugins: isort collected 1 items file1.py F =============================== FAILURES =============================== _____________________________ isort-check ______________________________ ERROR: file1.py Imports are incorrectly sorted. import os +import random import sys -import random ======================= 1 failed in 0.02 seconds =======================
如您所见,file2.py被忽略且未进行检查。现在修复file1.py中的导入顺序并重新运行测试
$ py.test --isort ========================= test session starts ========================== platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4 plugins: isort collected 1 items file1.py . ======================= 1 passed in 0.01 seconds ======================
一切又恢复正常。恭喜!
如果您反复运行测试套件,pytest将仅检查更改的文件以提高速度。您可以通过将-rs添加到pytest选项中看到这一点
$ py.test --isort -rs ========================= test session starts ========================== platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4 plugins: isort collected 1 items file1.py s ======================= short test summary info ======================== SKIP [1] pytest_isort.py:145: file(s) previously passed isort checks ====================== 1 skipped in 0.01 seconds ======================
配置
您可以通过在您的 py.test 配置文件(例如 pytest.ini)中使用 isort_ignore 设置来排除 isort 检查的文件。
# content of setup.cfg [pytest] isort_ignore = docs/conf.py *migrations/*.py
这将忽略 docs 文件夹中的 conf.py Python 文件,并忽略 migrations 文件夹中的任何 Python 文件。
此外,isort 配置中排除的文件也将被忽略。
注意事项
您可以使用 isort 重写您的 Python 文件并重新排序导入,但这不是此插件的一部分。
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。
源分布
pytest_isort-4.0.0.tar.gz (5.6 kB 查看哈希值)
构建分布
pytest_isort-4.0.0-py3-none-any.whl (6.9 kB 查看哈希值)
关闭
pytest_isort-4.0.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 00e99642e282b00b849cf9b49d9102a02ab8c4ec549ace57d7868b723713aaa9 |
|
MD5 | c2b49b086976c98194b8d4da3bc01ce1 |
|
BLAKE2b-256 | 69f0024f4a2ee0b4799a720f352dd7ef9a99077b48bd2c050ff2b08c14feb41d |
关闭
pytest_isort-4.0.0-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 14bb3281bab587d6beb53129481e8885232249ec5cfeaf5d903a561ff0589620 |
|
MD5 | ad6d22c21c26359ae0d1d4a6859f5481 |
|
BLAKE2b-256 | f1a6848a7bdec751e5648e7200c0e5339398eb9607666d83848840c8c1553a80 |