2010-09-13 44 views
133

我有一个Pylons 1.0应用程序,在测试/功能目录中有一堆测试。 我收到了奇怪的测试结果,我想只运行一次测试。 鼻子文档说我应该能够在测试的名称通过在命令行,但我得到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 

什么是正确的synt斧头?

回答

206

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

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

+1

唉唉,一个组合我没有尝试。 *叹*。谢谢! – Ben 2010-09-14 17:10:23

+2

这将在测试控制器/模块中运行每个测试。运行单一测试方法怎么样?类似于'appname.tests.functional.test_controller.name_of_test_method'。 – 2011-03-21 22:12:42

+66

要运行特定的测试类和方法,请使用“module.path:ClassNameInFile.method_name”形式的路径,即用冒号分隔模块/文件路径和文件内的对象。 – 2011-08-17 16:37:51

42

对我来说,使用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 

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

+1

感谢第二个选项,在bash自动完成的帮助下绝对是最方便的一个。 – 2014-02-06 11:04:06

+0

正是我在找的,谢谢! – radtek 2015-09-09 19:07:58

+0

值得注意的是,对于调用参数化测试(使用@ parameterized.expand的那些测试),您必须使用以下语法:test_file.py:ClassNameInFile.MethodName_TestNumber,其中TestNumber可以是1,2,3,... one每参数化测试 – luca 2017-12-01 15:14:32

2

我必须添加“py”为文件扩展名,也就是

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

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

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

这虽然我的文件夹/path_to/__init__.py

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" 
相关问题