How to specify one test in a file with nosetests?

I have a test_web.py file containing the TestWeb class, and many methods are called test_something ().

I can run each test in the class as follows:

$ 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 don't seem to be doing individual tests. They give me "No such tests" errors when running in the same PWD:

 $ nosetests test_web.py:test_checkout $ nosetests TestWeb:test_checkout 

What could be wrong here?

+65
python testcase nosetests
Jul 02 2018-12-12T00:
source share
4 answers

You should specify it like this: nosetests <file>:<Test_Case>.<test_method> or

 nosetests test_web.py:TestWeb.test_checkout 

See documents

+96
Jul 02 2018-12-12T00:
source share

You can also specify a module:

 nosetests tests.test_integration:IntegrationTests.test_user_search_returns_users 
+14
Aug 20 '13 at 10:19
source share

Specifying names on the command line, like the other answers, says this is helpful and helpful. However, when I am in the process of writing tests, I often find that I want to run only the tests that I work on, and the names that I have to write on the command line become quite long and cumbersome to write, In this case, I prefer use custom decorator and flag.

I define wipd ("work in progress decorator") as follows:

 from nose.plugins.attrib import attr def wipd(f): return attr('wip')(f) 

This defines the @wipd decorator, which will set the wip attribute on the objects that it decorates. For example:

 import unittest class Test(unittest.TestCase): @wipd def test_something(self): pass 

Then -a wip can be used on the command line to narrow down the execution of the test to those marked with @wipd .

Note on the names ...

I use the name @wipd for the decorator, not @wip to avoid such a problem:

 import unittest class Test(unittest.TestCase): from mymodule import wip @wip def test_something(self): pass def test_something_else(self): pass 

import will make the wip decorator a member of the class, and all tags in the class will be selected. The attrib plugin checks the parent class of the test method to see if the selected attribute also exists there, and attributes created and tested with attrib do not exist in isolated space. Therefore, if you test with -a foo and your class contains foo = "platypus" , then all tests in the class will be selected by the plugin.

+6
Jul 08 '16 at 15:09
source share

To run a few specific tests, you can simply add them to the command line, separated by a space.

 nosetests test_web.py:TestWeb.test_checkout test_web.py:TestWeb.test_another_checkout 
+1
May 22 '17 at 9:21 a.m.
source share



All Articles