Skip to content

Add "--deselect" command line option#3201

Merged
nicoddemus merged 1 commit into
pytest-dev:featuresfrom
uSpike:deselect_cli
Feb 16, 2018
Merged

Add "--deselect" command line option#3201
nicoddemus merged 1 commit into
pytest-dev:featuresfrom
uSpike:deselect_cli

Conversation

@uSpike

@uSpike uSpike commented Feb 9, 2018

Copy link
Copy Markdown
Member

Fixes #3198

I implemented this as a new CLI option --deselect since it does have different functionality than --ignore, which completely ignores the file.

The code matches any nodeid that startswith() any --deselect flag.

  • Create a new changelog file in the changelog folder, with a name like <ISSUE NUMBER>.<TYPE>.rst. See changelog/README.rst for details.
  • Target the features branch for new features and removals/deprecations.
  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Add yourself to AUTHORS in alphabetical order

@coveralls

coveralls commented Feb 10, 2018

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 92.65% when pulling 774c539 on uSpike:deselect_cli into 063e2da on pytest-dev:features.

@nicoddemus nicoddemus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @uSpike!

Comment thread testing/test_session.py Outdated
""")
result = testdir.runpytest("--deselect=test_a.py::test_a2[1]")
assert result.ret == 0
result.stdout.fnmatch_lines(["*11 passed, 1 deselected*"])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be 10 passed, 1 deselected right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think it would be better to use -v and deselect two tests and ensure they are not being run. You can parametrize test_a2 using a smaller value (say 3) so the test remains tidy and small.

Comment thread _pytest/main.py Outdated
for colitem in items:
for opt in deselectopt:
if colitem.nodeid.startswith(opt):
deselected.append(colitem)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this needs a break after deselected.append(colitem)

Comment thread _pytest/main.py Outdated
deselected = []
for colitem in items:
for opt in deselectopt:
if colitem.nodeid.startswith(opt):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startswith can take a tuple which can be used as a much better selector

instead of a loop and the for else hack it could simply use colitem.nodeid.startswith(deselect_prefixes)

i presume the break was not added due to the expectation of the prefixes being distinct/non-overlapping

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh cool, thanks I didnt realize str.startswith() takes a tuple. That's great.

Comment thread _pytest/main.py Outdated
help="try to interpret all arguments as python packages.")
group.addoption("--ignore", action="append", metavar="path",
help="ignore path during collection (multi-allowed).")
group.addoption("--deselect", action="append", metavar="item",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i propose a metavar of nodeid_prefix

@uSpike uSpike force-pushed the deselect_cli branch 2 times, most recently from d36ada9 to f7828a8 Compare February 15, 2018 22:03
@uSpike

uSpike commented Feb 15, 2018

Copy link
Copy Markdown
Member Author

@RonnyPfannschmidt @nicoddemus I've implemented your suggestions, thanks much for the feedback.

Comment thread _pytest/main.py Outdated
remaining = []
deselected = []
for colitem in items:
if colitem.nodeid.startswith(tuple(deselect_prefixes)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well done, please also move the call to tuple to the place where deselect_prefixes is declared

@uSpike uSpike force-pushed the deselect_cli branch 2 times, most recently from 7386d9c to 0e2522c Compare February 16, 2018 14:15
@blueyed

blueyed commented Feb 16, 2018

Copy link
Copy Markdown
Contributor

In #3198 you said:

I propose to accept test ids with --ignore:

This would not need a new option then.

btw: I've also thought about this being something negating -k (e.g. -K) - but that would be a different feature (since it uses different logic / evaluation).

@uSpike

uSpike commented Feb 16, 2018

Copy link
Copy Markdown
Member Author

@blueyed yes I had originally thought that I could add to --ignore but the functionality of --ignore ended up being fundamentally different than what I needed. I will update #3198.

@uSpike uSpike mentioned this pull request Feb 16, 2018
@nicoddemus nicoddemus merged commit b1abe5d into pytest-dev:features Feb 16, 2018
@nicoddemus

Copy link
Copy Markdown
Member

Thanks @uSpike!

@blueyed

blueyed commented Feb 19, 2018

Copy link
Copy Markdown
Contributor

I still think that it is confusing to have --ignore and --deselect now, and that support for foo.py::X should get added to --ignore instead.

--deselect requires the filename as a prefix already, and therefore is only a specialized version of --ignore, isn't it?

@twmr

twmr commented Feb 20, 2018

Copy link
Copy Markdown
Contributor

I guess that it makes sense to mentioned in the docs what the differences between --ignore and --deselect are

@uSpike uSpike deleted the deselect_cli branch February 20, 2018 16:23
@uSpike

uSpike commented Feb 20, 2018

Copy link
Copy Markdown
Member Author

--ignore is not exactly the same as --deselect. Paths that are excluded by --ignore are not reported anywhere via pytest_deselected or on the CLI. Items that are deselected by --deselect are passed to pytest_deselected and reported as so on the CLI.

I'm not sure if it's more or less confusing with --ignore if things that look like paths are not considered for collection at all, and things that look like node ids are deselected. It's a simple implementation and I'd be happy to submit another PR if there's interest.

@uSpike

uSpike commented Feb 20, 2018

Copy link
Copy Markdown
Member Author

I have a working implementation here: uSpike@8b98792

@blueyed

blueyed commented Feb 19, 2020

Copy link
Copy Markdown
Contributor

@uSpike
Thanks for following up with merging this with --ignore, and sorry for having missed it previously.
Would you like to follow up on that? (given that there just was confusion with --deselect not handling paths)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants