pytest-isort

4.0.0 · active · verified Fri Apr 17

pytest-isort is a pytest plugin that integrates isort's import ordering and formatting checks directly into the pytest test suite. It ensures that all Python files discoverable by pytest adhere to isort's configured rules, failing the test run if any incorrectly sorted imports are found. The current version is 4.0.0, released in February 2024, with active maintenance and updates to support newer Python and pytest versions.

Common errors

Warnings

Install

Quickstart

To quickly use `pytest-isort`, simply install it and run `pytest`. The plugin automatically discovers and checks Python files collected by pytest for correct import ordering based on your project's `isort` configuration. If any files have unsorted imports, `pytest` will report a failure.

import os

# test_imports.py

import os
import sys
from collections import defaultdict

def test_correct_imports():
    # This test will pass isort checks if imports are sorted correctly
    assert True

def test_bad_imports():
    # This test (or file content) will cause pytest-isort to fail
    # if imports are not sorted.
    x = 1 # Added to avoid 'Empty file' issue with isort check itself
    assert True

# To run this, save it as `test_imports.py` and run `pytest` in the same directory.
# If the imports within the file are not sorted according to isort rules,
# pytest-isort will report a failure.

view raw JSON →