Package: pytest-isort
SupportPython 3.7, 3.8, 3.9 and 3.10. pytest>=5.Usageinstall usingpip:pip install pytest-isortActivate isort checks when callingpy.test:py.test --isortThis will execute an isort check against every.pyfile (if its not ignored).ExampleGiven 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 osIf you runpy.testand 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
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 forfile2.py, you have the option to
excludefile2.pyfrom isort checks.Simply add theisort_ignoresetting to yourpy.testconfiguration file:[pytest]
isort_ignore =
    file2.pyThen 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
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.pyis ignored and not checked. Now fix the
import ordering infile1.pyand 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
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.testwill only check changed
files to speed up. You see this by adding-rsto yourpy.testoptions:$ 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 ======================ConfigurationYou can exclude files from isort checks by using theisort_ignoresetting in yourpy.testconfiguration file (e.g.pytest.ini):# content of setup.cfg
[pytest]
isort_ignore =
    docs/conf.py
    *migrations/*.pyThis will ignore theconf.pypython file inside thedocsfolder and
also ignore any python file inmigrationsfolders.In addition, excluded files in isort’s configuration will be ignored too.NotesYou can useisortto rewrite your python files and re-order the imports but
this is not part of this plugin.