How do I run a single test with Nose in Pylons

61,791

Solution 1

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).

Solution 2

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.

Solution 3

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/.

Share:
61,791

Related videos on Youtube

Ben
Author by

Ben

Currently working at MajorBoost Inc. solving healthcare's communications problems one phone call at a time. Formerly: Worked on HoloLens for Microsoft Was the CTO at a location based games company Tech lead and programmer for Ubisoft and Sony Computer Entertainment Dot-bomb survivor and one of the many people who, independently, created something like AJAX but didn't come up with the cool name.

Updated on January 09, 2021

Comments

  • Ben
    Ben over 3 years

    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?

  • Ben
    Ben almost 14 years
    Ahhh, the one combination I didn't try. sigh. Thanks!
  • ryonlife
    ryonlife over 13 years
    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‌​.
  • James Murty
    James Murty almost 13 years
    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.
  • bcoughlan
    bcoughlan almost 12 years
    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
  • schlamar
    schlamar about 11 years
    @bcoughlan I added this to the answer! This was really confusing.
  • Peter Kilczuk
    Peter Kilczuk over 10 years
    Thanks for the second option, with the help of bash autocomplete definitely the most convenient one.
  • Rafay
    Rafay almost 10 years
    Thanks for the answer buddy!
  • falsePockets
    falsePockets about 7 years
    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
    falsePockets about 7 years
    Ah nevermind, I figured it out. The trick is to prepend nosetests as an argument. nose.run(argv=['nosetests','sample_test.py:TestLambda.test_c‌​ylp'])
  • luca
    luca over 6 years
    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
    luca over 6 years
    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