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

发布于 2024-09-18 19:47:39 字数 768 浏览 3 评论 0原文

我有一个 Pylons 1.0 应用程序,在 test/function 目录中包含一堆测试。 我得到了奇怪的测试结果,我只想运行一个测试。 鼻子文档说我应该能够在命令行中传递测试名称,但无论我做什么,我都会得到 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

什么是正确的语法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

遮了一弯 2024-09-25 19:47:39

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

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

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).

末蓝 2024-09-25 19:47:39

对于我使用 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

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

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.

鹊巢 2024-09-25 19:47:39

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

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

也许这是因为我的文件中没有任何类。
如果没有 .py,nose 就会抱怨:

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

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

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/.

追我者格杀勿论 2024-09-25 19:47:39

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

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

I wrote this small script, based on the previous answers:

#!/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"
网白 2024-09-25 19:47:39

以下对我来说效果很好:

nosetests test_file.py:method_name

请注意,我的测试不是在课堂上进行的。测试方法位于单个文件中。

The following worked for me just well:

nosetests test_file.py:method_name

Note that my tests where not in a class. Test methods were in a single file.

想挽留 2024-09-25 19:47:39

对于nosetests 1.3.7,您需要执行:

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

For nosetests 1.3.7, you need to do:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文