When I use py.test, I often rely on the
-k switch to swiftly select the test I want to run. Instead of having to type the full module, class, and test path as is required with unittest and nose, I can just type a few characters that uniquely match the name of the test.
For example, if I have a test file containing methods
test_basic_clone_notes, I can run the latter test simply by calling
py.test -k clone_no.
However, I often create multiple tests that have similar names. This can make it difficult to run just one test if the test name is a prefix of a longer test name. If I want to run just
test_basic_clone, any substring will also be a substring of the
test_basic_clone_notes test, and both tests are matched by
Since pytest version 2.3.4, the -k keyword supports expressions. So I can build an expression like this:
py.test -k "basic_clone and not notes"
This selects all tests matching “basic_clone”, then excludes any containing the word “notes”. Thus, I run only the test I’m interested in without having to fix my crappy naming scheme. It’s more typing than is normally the case, but is still less cognitive load than trying to remember what module and class I’m editing and constructing a selector based on those attributes.