Search code examples
pythonnose

Running single test function in Nose NOT associated with unittest subclass


nose discovers tests beginning with test_, as well as subclasses of unittest.TestCase.

If one wishes to run a single TestCase test, e.g.:

# file tests.py
class T(unittest.TestCase):
    def test_something():
        1/0

This can be done on the command line with:

nosetests tests:T.test_something

I sometimes prefer to write a simple function and skip all the unittest boilerplate:

def test_something_else():
    assert False

In that case, the test will still be run by nose when it runs all my tests. But how can I tell nose to run only that test, using the (Unix) command line?


Solution

  • That would be:

    nosetests tests:test_something_else
    

    An additional tip is to use attributes

    from nose.plugins.attrib import attr
    
    @attr('now')
    def test_something_else():
        pass
    

    To run all tests tagged with the attribute, execute:

    nosetests -a now
    

    Inversely, avoid running those tests:

    nosetests -a !now