How do I specify a single test in a file with nosetests?
Specifying names on the command line like the other answers suggest does work and is useful. However, when I'm in the midst of writing tests, I often find that I want to run just the test I'm working on, and the names that I would have to write on the command line get pretty long and cumbersome to write. In such case, I prefer to use a custom decorator and flag.
I define wipd
("work in progress decorator") like this:
from nose.plugins.attrib import attrdef wipd(f): return attr('wip')(f)
This defines a decorator @wipd
which will set the wip
attribute on objects it decorates. For instance:
import unittestclass Test(unittest.TestCase): @wipd def test_something(self): pass
Then -a wip
can be used at the command line to narrow the execution of the test to the ones marked with @wipd
.
Note on names...
I'm using the name @wipd
for the decorator rather than @wip
to avoid this kind of problem:
import unittestclass Test(unittest.TestCase): from mymodule import wip @wip def test_something(self): pass def test_something_else(self): pass
The import
will make the wip
decorator a member of the class, and all tests in the class will be selected. The attrib
plugin checks the parent class of a test method to see if the attribute selected exists there too, and the attributes that are created and tested by attrib
do not exist in a segregated space. So if you test with -a foo
and your class contains foo = "platypus"
, then all tests in the class will be selected by the plugin.