How do I specify a single test in a file with nosetests?
I have a file called test_web.py containing a class TestWeb and many methods named like test_something().
I can run every test in the class like so:
$ nosetests test_web.py
...
======================================================================
FAIL: checkout test
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/me/path/here/test_web.py", line 187, in test_checkout
...
But I can’t seem to run individual tests. These give me “No such test” errors when run in the same PWD:
$ nosetests test_web.py:test_checkout
$ nosetests TestWeb:test_checkout
What could be wrong here?
Solution 1:
You must specify it like so: nosetests <file>:<Test_Case>.<test_method>
, or
nosetests test_web.py:TestWeb.test_checkout
See the docs
Solution 2:
You can also specify a module:
nosetests tests.test_integration:IntegrationTests.test_user_search_returns_users
Solution 3:
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 attr
def wipd(f):
return attr('wip')(f)
This defines a decorator @wipd
which will set the wip
attribute on objects it decorates. For instance:
import unittest
class 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 unittest
class 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.
Solution 4:
To run multiple specific tests, you can just add them to the command line, separated by space.
nosetests test_web.py:TestWeb.test_checkout test_web.py:TestWeb.test_another_checkout