116

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?

Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
opstastic
  • 1,262
  • 2
  • 8
  • 6
  • can't help you unless you can post your test cases or a [SSCCE](http://sscce.org/), I just tried the syntax you used with nose on my machine and it worked fine. – Jeff Tratner Jul 02 '12 at 00:34

6 Answers6

173

You must specify it like so: nosetests <file>:<Test_Case>.<test_method>, or

nosetests test_web.py:TestWeb.test_checkout

See the docs

Will
  • 4,498
  • 1
  • 21
  • 20
21

You can also specify a module:

nosetests tests.test_integration:IntegrationTests.test_user_search_returns_users
michaeljoseph
  • 7,023
  • 5
  • 26
  • 27
  • 2
    I don't know if it's a different version of Python or `nosetests` or what, but that syntax fails. What *does* work, though, is: `nosetests tests/test_integration:IntegrationTests.test_user_search_returns_users`, meaning - reference files as files, not Python modules, using `/` rather than `.` – dwanderson Mar 13 '17 at 02:35
  • 1
    @dwanderson both usages should work, as per http://nose.readthedocs.io/en/latest/usage.html#selecting-tests. Your failure could be caused by `tests` not being a module in your setup? – michaeljoseph Mar 13 '17 at 10:04
  • 1
    Ahh, that's right, I forgot an `__init__.py` in the `tests` directory. Well done! Thanks – dwanderson Mar 15 '17 at 18:25
11

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.

Louis
  • 146,715
  • 28
  • 274
  • 320
5

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
Skurpi
  • 1,000
  • 1
  • 12
  • 22
0

In my tests, specifying tests with module names do not work

You must specify the actual path to the .py:

nosetests /path/to/test/file.py:test_function

This with nose==1.3.7

tread
  • 10,133
  • 17
  • 95
  • 170
0

My requirement was to run a single test in a test file that was in another windows directory - this was done from the anaconda command prompt as follows:

ran nosetests from:

(base) C:\Users\ABC\Documents\work\

but test_MyTestFile.py and methodsFile.py were in:

 (base) C:\Users\ABC\Documents\work\daily\

run single test by including path with quotes as follows:

(base) C:\Users\ABC\Documents\work>nosetests "daily\\test_MyTestFile.py:MyTestClass.test_add_integers"

test_MyTestFile.py looked like this:

import methodsFile
import unittest

class MyTestClass(unittest.TestCase):

    def test_add_integers(self):
        assert methodsFile.add(5, 3) == 8

    def test_add_integers_zero(self):
        assert methodsFile.add(3, 0) == 3

methodsFile.py looked like this:

def add(num1, num2):
        return num1 + num2
Grant Shannon
  • 4,709
  • 1
  • 46
  • 36