[pytest-dev] improve -k/-m exercise / want to help pytest?

Dinu Gherman gherman at darwin.in-berlin.de
Wed Oct 22 10:20:08 CEST 2014


holger krekel:

> Hi all,
> 
> is anyone interested in a little exercise that would improve pytest usage?
> It doesn't require deep pytest knowledge.
> 
> This is about the "-k" and "-m" selection mini-language, 
> namely expressions like:
> 
>    marker1 and marker2
>    not marker1 and (marker2 or marker3)
> 
> and so forth.  The thing is that pytest currently uses Python's "eval" with
> a custom dictionary which means that names such as "marker1"
> cannot have dots or other punctuation and also other things are not
> possible like running all tests that don't have "4.3" (coming from a
> parametrized test) in it:
> 
>    -k 'not 4.3'
> 
> This would currently select all tests because "4.3" is true.  Various
> people have run into traps like this.
> 
> So what is needed here are tests and code that, given a set of names
> and an expression string, determines if the expression is true or not.
> And everything except "and" and "or" are valid words. To use the latter
> two words probably need to have some escaping like "\and" or so but
> that's a bonus task :)
> 
> Anyone up for this little exercise?  Or other comments?

Not sure I have the time, but still a few comments:

- I like the planned feature.
- It would be nice to have a dotted syntax allowing to specify a specific test method in a specific test class, something like -k TestClassX.test_method_y (like it is possible with unittest).
- Is pyparsing an option for doing this (maybe a somewhat heavy solution, not sure)?
- Or, could the expression be formulated somewhere else, like in conftest.py (in pure Python), as a test selection function, and referenced by the -k option by name? That way these expressions would also be easier to reuse, since on the command-line they simply get lost.

Cheers,

Dinu



More information about the pytest-dev mailing list