跳转到主要内容

使用isort检查导入顺序的pytest插件

项目描述

Latest Version CI Status

支持

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 查看哈希值)

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面