154

I have a Pylons 1.0 app with a bunch of tests in the test/functional directory. I'm getting weird test results and I want to just run a single test. The nose documentation says I should be able to pass in a test name at the command line but I get ImportErrors no matter what I do

For example:

nosetests -x -s sometestname

Gives:

Traceback (most recent call last):
  File "/home/ben/.virtualenvs/tsq/lib/python2.6/site-packages/nose-0.11.4-py2.6.egg/nose/loader.py", line 371, in loadTestsFromName
   module = resolve_name(addr.module)
  File "/home/ben/.virtualenvs/tsq/lib/python2.6/site-packages/nose-0.11.4-py2.6.egg/nose/util.py", line 334, in resolve_name
   module = __import__('.'.join(parts_copy))
ImportError: No module named sometestname

I get the same error for

nosetests -x -s appname.tests.functional.testcontroller

What is the correct syntax?

Nick T
  • 22,202
  • 10
  • 72
  • 110
Ben
  • 9,591
  • 9
  • 32
  • 40

6 Answers6

236

nosetests appname.tests.functional.test_controller should work, where the file is named test_controller.py.

To run a specific test class and method use a path of the form module.path:ClassNameInFile.method_name, that is, with a colon separating the module/file path and the objects within the file. module.path is the relative path to the file (e.g. tests/my_tests.py:ClassNameInFile.method_name).

Nick T
  • 22,202
  • 10
  • 72
  • 110
Antoine Leclair
  • 15,994
  • 3
  • 24
  • 17
  • 1
    Ahhh, the one combination I didn't try. *sigh*. Thanks! – Ben Sep 14 '10 at 17:10
  • 2
    That will run every test in a test controller/module. What about running a single test method? Something like `appname.tests.functional.test_controller.name_of_test_method`. – ryonlife Mar 21 '11 at 22:12
  • 69
    To run a specific test class and method use a path of the form `module.path:ClassNameInFile.method_name`, that is, with a colon separating the module/file path and the objects within the file. – James Murty Aug 17 '11 at 16:37
  • 9
    For anyone else confused: `module.path` is the relative path to the file (e.g. `my_tests.py:ClassNameInFile.method_name`), not the path you would use in an `import` statement – bcoughlan Sep 05 '12 at 19:24
  • 1
    @bcoughlan I added this to the answer! This was really confusing. – schlamar Apr 26 '13 at 11:31
  • Thanks for the answer buddy! – Rafay Aug 07 '14 at 08:06
  • How do I do this from within python? If I try `nose.run('my_tests.py:ClassNameInFile.method_name')` I get the error: `ImportError: No module named py:ClassNameInFile.method_name`. If I use `nose.run(argv=['my_tests.py:ClassNameInFile.method_name'])` then it runs all tests, not just `method_name`. – falsePockets May 19 '17 at 02:26
  • Ah nevermind, I figured it out. The trick is to prepend `nosetests` as an argument. `nose.run(argv=['nosetests','sample_test.py:TestLambda.test_cylp'])` – falsePockets May 19 '17 at 03:42
  • It would be worth adding that for calling parameterized tests (the ones that use @parameterized.expand) you have to use this syntax: my_tests.py:ClassNameInFile.method_name_testnumber, where testnumber is 1, 2, 3, ... one per parametrized test – luca Oct 29 '17 at 13:21
47

For me using Nosetests 1.3.0 these variants are working (but make sure you have __init__.py in your tests folder):

nosetests [options] tests.ui_tests
nosetests [options] tests/ui_tests.py
nosetests [options] tests.ui_tests:TestUI.test_admin_page

Note that single colon between module name and class name.

Thomas
  • 5,824
  • 1
  • 26
  • 45
Kee
  • 1,196
  • 10
  • 14
  • 1
    Thanks for the second option, with the help of bash autocomplete definitely the most convenient one. – Peter Kilczuk Feb 06 '14 at 11:04
  • It would be worth note that for calling parameterized tests (the ones that use @parameterized.expand) you have to use this syntax: test_file.py:ClassNameInFile.MethodName_TestNumber, where TestNumber could be 1, 2, 3, ... one per parametrized test – luca Dec 01 '17 at 15:14
2

I have to add the ".py" file extension, that is,

r'/path_to/my_file.py:' +  r'test_func_xy'

Maybe this is because I don't have any classes in the file. Without the .py, nose was complaining:

Can't find callable test_func_xy in file /path_to/my_file: file is not a python module

And this although I have an __init__.py in the folder /path_to/.

gogo_gorilla
  • 2,547
  • 1
  • 22
  • 34
0

I wrote this small script, based on the previous answers:

#!/usr/bin/env bash

# 
# Usage:
# 
#     ./noseTest <filename> <method_name>
# 
# e.g.:
# 
#     ./noseTest test/MainTest.py mergeAll
#     
# It is assumed that the file and the test class have the _same name_ 
# (e.g. the test class `MainTest` is defined in the file `MainTest.py`).
# If you don't follow this convention, this script won't work for you.
#

testFile="$1"
testMethod="$2"

testClass="$(basename "$testFile" .py)"

nosetests "$testFile:$testClass.test_$testMethod"
tfga
  • 51
  • 5
0

The following worked for me just well:

nosetests test_file.py:method_name

Note that my tests where not in a class. Test methods were in a single file.

Raptor
  • 3,039
  • 2
  • 30
  • 30
0

For nosetests 1.3.7, you need to do:

nosetests --tests=tests.test_something.py,tests.test_something_else.py.

Maciej Kozik
  • 191
  • 1
  • 8