Skip to main content

py.test plugin to check import ordering using isort

Project description

https://badge.fury.io/py/pytest-isort.svg https://travis-ci.org/moccu/pytest-isort.svg?branch=master

Usage

install using pip:

pip install pytest-isort

Activate isort checks when calling py.test:

py.test --isort

This will execute an isort check against every .py file (if its not ignored).

Example

Given you have some files with incorrect sorted imports:

# content of file1.py

import os
import sys
import random

# content of file2.py

import json
import sys
import os

If you run py.test and activate the isort plugin you’ll se something like this:

$ py.test --isort
========================= test session starts ==========================
platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4
plugins: isort, cache
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 =======================

If you can’t change the import ordering for file2.py, you have the option to exclude file2.py from isort checks.

Simply add the isort_ignore setting to your py.test configuration file:

[pytest]
isort_ignore =
    file2.py

Then re-run the tests:

$ py.test --isort
========================= test session starts ==========================
platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4
plugins: isort, cache
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 =======================

As you can see, file2.py is ignored and not checked. Now fix the import ordering in file1.py and re-run the tests:

$ py.test --isort
========================= test session starts ==========================
platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4
plugins: isort, cache
collected 1 items

file1.py .

======================= 1 passed in 0.01 seconds ======================

Everything is properly again. Congratulations!

If you run your testsuite again and again, py.test will only check changed files to speed up. You see this by adding -rs to your py.test options:

$ py.test --isort -rs
========================= test session starts ==========================
platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4
plugins: isort, cache
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 ======================

Configuration

You can exclude files from isort checks by using the isort_ignore setting in your py.test configuration file (e.g. pytest.ini):

# content of setup.cfg
[pytest]
isort_ignore =
    docs/conf.py
    *migrations/*.py

This will ignore the conf.py python file inside the docs folder and also ignore any python file in migrations folders.

Notes

You can use isort to rewrite your python files and re-order the imports but this is not part of this plugin.

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-isort-0.2.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

pytest_isort-0.2.0-py2-none-any.whl (4.5 kB view details)

Uploaded Python 2

File details

Details for the file pytest-isort-0.2.0.tar.gz.

File metadata

File hashes

Hashes for pytest-isort-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d4d195ecfe33d81e258d251b2679b32216bad84131fb41984da22d9d0328a6fe
MD5 a69f70615bcd9e888c930eb66a501394
BLAKE2b-256 7121277cd8b928fccad8ebc37ac8e51b0f2d80d75ff253b72799a86c0a629569

See more details on using hashes here.

File details

Details for the file pytest_isort-0.2.0-py2-none-any.whl.

File metadata

File hashes

Hashes for pytest_isort-0.2.0-py2-none-any.whl
Algorithm Hash digest
SHA256 ce36b62ca1108e16a8b8fc0d5a82302ba0796a3c352b093e13ccbb18e772edc6
MD5 1ead7ae51198d9a930f593a76d6c3930
BLAKE2b-256 9669582f37321af29a004804b0454f014a8ba16bbb78dc39ae0e4a9a7890c771

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