pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.97k stars 2.66k forks source link

The -k expression works with double quotes but not single quotes #9431

Closed taigit closed 1 year ago

taigit commented 2 years ago

When using single quotes and trying a conjunction of expressions: -k 'vet_a and vet_a' I get the error message: ERROR: file or directory not found: and

I finally tried double quotes and it worked.

Note: the docs and -h help text using single quotes in the -k examples so at least update those as a quick workaround...

pytest 6.2.4 Windows 8.1

bluetech commented 2 years ago

On unix systems, ' is the better option because it avoids shell interpolation.

On windows systems, looks like the correct quoting is double quotes (at least according to a random Stack Overflow answer...).

(edit: NOTE: see comments below) So it would be nice I suppose for pytest to use double quotes on windows and single quotes everywhere else in its help text for -k (and any other options which use quotes in their examples, if any).

PR welcome!

The-Compiler commented 2 years ago

I don't think there is an universal answer to this. With Powershell (which is somewhat commonly used instead of cmd.exe on Windows), single quotes would be the right thing to use. Same with WSL and bash/zsh too, though I suppose that would count as Linux. You can, however, run bash directly on Windows too, e.g. via git bash.

So this is really only specific to cmd.exe, checking for Windows as suggested by @bluetech would likely cause more problems than it solves. Detecting the shell pytest is running in is not trivial at all. It involves checking the parent processes and trying to find their path until encountering a shell...

I suppose we should mention this in the docs somewhere and maybe even link to that page in the --help output, but in the end, how the shell works should not be pytest's concern.

bluetech commented 2 years ago

@The-Compiler I agree. I guess we can't win this one, and using ' probably is preferred. I think explaining this in the --help is probably too much, but a note in the docs would be nice.

SauravMaheshkar commented 2 years ago

@bluetech Can I work on this ?? i.e. adding a note in the docs ?

bluetech commented 2 years ago

@SauravMaheshkar that would be great. I think maybe the best place for it is here: https://github.com/pytest-dev/pytest/blob/main/doc/en/how-to/usage.rst#specifying-which-tests-to-run (under "Run tests by keyword expressions"). We should switch the example from using " to ', as discussed above, and add a note about Windows cmd below it.

taigit commented 2 years ago

@SauravMaheshkar here's the docs that I used for -k expressions: https://docs.pytest.org/en/6.2.x/reference.html#command-line-flags

taigit commented 2 years ago

hmm, I don't know how the github system works for docs, but I still see single quotes in the -k expression docs (and the same text is used in the -h output so it needs to be changed there too...)

https://pytest--9454.org.readthedocs.build/en/9454/reference/reference.html#command-line-flags

SauravMaheshkar commented 2 years ago

@taigit I added double quotes for -k expression docs in 96376ac. But I'm not exactly sure what you mean by the -h output.

taigit commented 2 years ago

@taigit I added double quotes for -k expression docs in 96376ac. But I'm not exactly sure what you mean by the -h output.

-h output is the output users get when they type: pytest -h

dolfinus commented 2 years ago

-h output is set here: https://github.com/pytest-dev/pytest/blob/176d2d7b4e21b19fb08afdb3f9db61495eff5647/src/_pytest/mark/__init__.py#L86

Also, doesn't -m option have the same behavior about quotes as -k?

nshuman1 commented 1 year ago

@SauravMaheshkar that would be great. I think maybe the best place for it is here: https://github.com/pytest-dev/pytest/blob/main/doc/en/how-to/usage.rst#specifying-which-tests-to-run (under "Run tests by keyword expressions"). We should switch the example from using " to ', as discussed above, and add a note about Windows cmd below it.

Hello,

I'd be happy to take a pass at this, 96376ac went in the opposite direction.

The-Compiler commented 1 year ago

Done in #10974