Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
trondhindenes opened this issue Apr 19, 2024 · 6 comments
Open
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity

Comments

@trondhindenes
Copy link

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

@RonnyPfannschmidt
Copy link
Member

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

@The-Compiler
Copy link
Member

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 The-Compiler added the status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity label Apr 19, 2024
@RonnyPfannschmidt
Copy link
Member

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

@okken
Copy link
Contributor

okken commented Apr 29, 2024

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
Copy link
Contributor

okken commented Apr 29, 2024

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

@okken
Copy link
Contributor

okken commented Apr 29, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity
Projects
None yet
Development

No branches or pull requests

4 participants