How do I run a single test with Nose in Pylons
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/
.
Related videos on Youtube
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, 2021Comments
-
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 almost 14 yearsAhhh, the one combination I didn't try. sigh. Thanks!
-
ryonlife over 13 yearsThat 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 almost 13 yearsTo 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 almost 12 yearsFor 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 animport
statement -
schlamar about 11 years@bcoughlan I added this to the answer! This was really confusing.
-
Peter Kilczuk over 10 yearsThanks for the second option, with the help of bash autocomplete definitely the most convenient one.
-
Rafay almost 10 yearsThanks for the answer buddy!
-
falsePockets about 7 yearsHow 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 usenose.run(argv=['my_tests.py:ClassNameInFile.method_name'])
then it runs all tests, not justmethod_name
. -
falsePockets about 7 yearsAh nevermind, I figured it out. The trick is to prepend
nosetests
as an argument.nose.run(argv=['nosetests','sample_test.py:TestLambda.test_cylp'])
-
luca over 6 yearsIt 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 over 6 yearsIt 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