如何使用 Pylons 中的 Nose 运行单个测试

我有一个 Pylons 1.0应用程序,它在 test/function 目录中包含许多测试。 我收到了奇怪的测试结果,我只想做一个测试。 Nose 文档说明我应该能够在命令行中传递测试名称,但是不管我做什么,我都会得到 Import Errors

例如:

nosetests -x -s sometestname

给予:

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

我得到相同的错误

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

正确的语法是什么?

61947 次浏览

nosetests appname.tests.functional.test_controller应该可以工作,其中文件名为 test_controller.py

要运行特定的测试类和方法,使用形式为 module.path:ClassNameInFile.method_name的路径,即用冒号分隔模块/文件路径和文件中的对象。module.path是文件的相对路径(例如 tests/my_tests.py:ClassNameInFile.method_name)。

对于我来说,使用 Nosetest1.3.0这些变体是可行的(但是要确保在您的测试文件夹中有 __init__.py) :

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

注意,模块名和类名之间只有一个冒号。

我必须添加“ . py”文件扩展名,即,

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

也许这是因为我没有任何类在文件中。 没有 .py,鼻子就在抱怨:

在 file/path _ to/my _ file: file is not 中找不到可调用的 test _ func _ xy 一个巨蟒模块

尽管我在文件夹 /path_to/中有一个 __init__.py

我根据之前的答案写了这个小脚本:

#!/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"

以下几点对我很有效:

nosetests test_file.py:method_name

注意,我的测试不在 class. Test 方法中,而是在单个文件中。

对于鼻测试 1.3.7,你需要:

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