How do I specify a single test in a file with nosetests?

PythonTestcaseNosetests

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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 - Python

You can also specify a module:

nosetests tests.test_integration:IntegrationTests.test_user_search_returns_users

Solution 3 - Python

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 - Python

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

Solution 5 - Python

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

Solution 6 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionopstasticView Question on Stackoverflow
Solution 1 - PythonWillView Answer on Stackoverflow
Solution 2 - PythonmichaeljosephView Answer on Stackoverflow
Solution 3 - PythonLouisView Answer on Stackoverflow
Solution 4 - PythonSkurpiView Answer on Stackoverflow
Solution 5 - PythontreadView Answer on Stackoverflow
Solution 6 - PythonGrant ShannonView Answer on Stackoverflow