155

我有一个 Pylons 1.0 应用程序,在 test/functional 目录中有一堆测试。我得到了奇怪的测试结果,我只想运行一个测试。鼻子文档说我应该能够在命令行中传递一个测试名称,但是无论我做什么我都会得到 ImportErrors

例如:

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

什么是正确的语法?

4

6 回答 6

237

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

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

于 2010-09-13T22:09:03.837 回答
47

对使用 Nosetests 1.3.0 的我来说,这些变体可以正常工作(但请确保您__init__.py的测试文件夹中有):

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

注意模块名和类名之间的单冒号。

于 2013-06-26T19:14:30.210 回答
2

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

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

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

在文件 /path_to/my_file 中找不到可调用的 test_func_xy:文件不是 python 模块

这虽然我__init__.py在文件夹中有一个/path_to/

于 2015-03-20T11:33:02.957 回答
0

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

#!/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"
于 2017-07-31T19:52:32.890 回答
0

对于 nosetests 1.3.7,您需要执行以下操作:

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

于 2021-01-08T13:02:58.497 回答
0

以下对我很有效:

nosetests test_file.py:method_name

请注意,我的测试不在课堂上。测试方法在一个文件中。

于 2019-05-02T14:48:09.690 回答