pytest icon indicating copy to clipboard operation
pytest copied to clipboard

support an alternate syntax for "not" (as in `pytest -m "not stuff"`)

Open trondhindenes opened this issue 1 year ago • 7 comments

What's the problem this feature will solve?

Lots of CI systems heavily rely on bash or various bash scripts. And even tho we're in 2024, passing quotes into bash is still tricky. I would ask that an alternate way of specifying "not", such as :

these would be similar

poetry run pytest -m "not mymarker"
poetry run pytest -not-m mymarker
poetry run pytest -m not_mymarker

Describe the solution you'd like

basically an alternate syntax that would not require quotation marks to specify it

Alternative Solutions

Additional context

trondhindenes avatar Apr 19 '24 11:04 trondhindenes

about 10 years ago there was such a syntax, it created numerous issues - now we have a regular language

and right to my point all of your proposals are incorrect applications of the short argument for thats invalid in any case

the syntax before was -marker and created a lot of trouble when composing arguments

imho its on ci systems to finally add save ways to invoke things,

im -1 on re-adding a mess just to avoid quotes as from personal experience the messes to avoid quotes where so far worse

RonnyPfannschmidt avatar Apr 19 '24 11:04 RonnyPfannschmidt

I agree. All of your proposed syntaxes come with their own problems - except :marker maybe, but that is entirely non-obvious. I don't think it's a good idea to have yet another problematic and/or non-obvious way to do the same thing.

How is "passing quotes into bash still tricky"?

The-Compiler avatar Apr 19 '24 12:04 The-Compiler

to be fair bash in yaml in ci with variable pass-over can sill turn into nightmares the moment one makes a variable quoting mistake as bash is demonstrably hostile to writing correct code in any non-trivial capacity

RonnyPfannschmidt avatar Apr 19 '24 12:04 RonnyPfannschmidt

Seems like this could be handled by a plugin or a local conftest.py modification.
The example below doesn't handle fancy logic, but for simple marker deselection, it works fine.
Unless I'm missing some requirement.

# put this in conftest.py to avoid markers 
# Example:
# `pytest --not foo` will deselect `foo` marked tests.
# `pytest --not foo bar` will deselect both `foo` and `bar` marked tests.
# etc.

def pytest_addoption(parser):
    parser.addoption('--not', action='store', dest='not_markers', nargs='*', 
                     help='markers to not run')


def pytest_collection_modifyitems(config, items):
    not_markers = config.option.not_markers

    if not_markers:
        # just in case someone calls --not foo, bar
        # strip out commas
        not_markers = [m.strip(',') for m in not_markers] 
        selected_items = []
        deselected_items = []
        for item in items:
            found_unwanted_marker = False
            for avoid in not_markers:
                if item.get_closest_marker(avoid):
                    found_unwanted_marker = True
            if found_unwanted_marker:
                deselected_items.append(item)
            else:
                selected_items.append(item)

        config.hook.pytest_deselected(items=deselected_items)
        items[:] = selected_items

okken avatar Apr 29 '24 20:04 okken

I think I need this also, maybe I'll write a plugin

okken avatar Apr 29 '24 20:04 okken

Bonus. pytest -m foo --not foo is a way to have 0 failures in your test suite.

okken avatar Apr 29 '24 20:04 okken

This issue is stale because it has the status: needs information label and requested follow-up information was not provided for 14 days.

github-actions[bot] avatar May 14 '24 01:05 github-actions[bot]

This issue was closed because it has the status: needs information label and follow-up information has not been provided for 7 days since being marked as stale.

github-actions[bot] avatar May 21 '24 01:05 github-actions[bot]