首页 文章

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

提问于
浏览
141

我有一个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

什么是正确的语法?

4 回答

  • 0

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

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

  • 223

    对于我使用Nosetests 1.3.0这些变体是有效的(但请确保您的tests文件夹中有 __init__.py ):

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

    请注意模块名称和类名之间的单冒号 .

  • 45

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

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

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

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

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

  • 2

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

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

相关问题