stephrdev / pytest-isort

py.test plugin to check import ordering using isort
MIT License
40 stars 15 forks source link

Improve pytest failure output #26

Open GergelyKalmar opened 4 years ago

GergelyKalmar commented 4 years ago

It seems that tools like pylint use [pylint] <filename> as the header of failed tests:

image

It would be excellent if pytest-isort could do it similarly instead of using isort-check in each header and noting the filename in the first line as ERROR: <filename> Imports are incorrectly sorted..

stephrdev commented 4 years ago

Interesting point, I checked other plugins I use and it seems like there is no "standard". pytest-black has a fixed string too, pytest-flake8 reports the ignored checks. I'm not sure if we should change it as it make break users code/infrastructure in case they check for the "isort-check" string in stdout. World is weird sometimes ;-)

GergelyKalmar commented 4 years ago

Fair enough. Maybe I'll just create a pytest plugin to normalize this :).

def unified_reportinfo(item, prefix):
    def reportinfo():
        return (item.fspath, None, prefix + item.config.invocation_dir.bestrelpath(item.fspath))
    return reportinfo
def pytest_collection_modifyitems(config, items):
    for item in items:
        source = item.nodeid.split('::')[-1].lower()
        if source in ['isort', 'mypy', 'pylint']:
            item.reportinfo = unified_reportinfo(item=item, prefix=f'[{source}] ')

Works like magic! I wish there was a nicer way than parsing the nodeid, but I suppose you don't plan to change that anytime soon given that it may break things too.