如何在具有nosetests的文件中指定单个测试?

ops*_*tic 92 python nosetests testcase

我有一个名为test_web.py的文件,其中包含一个TestWeb类和许多名为test_something()的方法.

我可以像这样在类中运行每个测试:

$ nosetests test_web.py 
...
======================================================================
FAIL: checkout test
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/me/path/here/test_web.py", line 187, in test_checkout
...
Run Code Online (Sandbox Code Playgroud)

但我似乎无法进行个别测试.在同一个PWD中运行时,这些给我"没有这样的测试"错误:

$ nosetests test_web.py:test_checkout
$ nosetests TestWeb:test_checkout
Run Code Online (Sandbox Code Playgroud)

这可能有什么问题?

Wil*_*ill 136

你必须这样指定:nosetests <file>:<Test_Case>.<test_method>,或

nosetests test_web.py:TestWeb.test_checkout
Run Code Online (Sandbox Code Playgroud)

查看文档

  • 为什么地球上的图书馆使用':'而不是'.'?;) (6认同)
  • 也许是为了在模块和类之间轻松划分? (2认同)
  • 哇,那是可怕的经典python库,不在乎现有接口 (2认同)

mic*_*eph 16

您还可以指定一个模块:

nosetests tests.test_integration:IntegrationTests.test_user_search_returns_users
Run Code Online (Sandbox Code Playgroud)

  • 我不知道是不同版本的 Python 还是 `nosetests` 还是什么,但该语法失败了。不过,* 确实* 有效的是:`nosetests tests/test_integration:IntegrationTests.test_user_search_returns_users`,意思是 - 将文件作为文件而不是 Python 模块引用,使用 `/` 而不是 `.` (2认同)

Lou*_*uis 11

在命令行上指定名称就像其他答案建议一样有效并且很有用.但是,当我正在编写测试时,我经常发现我只想运行我正在进行的测试,并且我必须在命令行上编写的名称变得非常冗长而且编写起来很麻烦.在这种情况下,我更喜欢使用自定义装饰器和标志.

我定义wipd("正在进行装饰工程师"),如下所示:

from nose.plugins.attrib import attr
def wipd(f):
    return attr('wip')(f)
Run Code Online (Sandbox Code Playgroud)

这定义了一个装饰器@wipd,它将wip在它装饰的对象上设置属性.例如:

import unittest
class Test(unittest.TestCase):

    @wipd
    def test_something(self):
        pass
Run Code Online (Sandbox Code Playgroud)

然后-a wip可以在命令行中使用,以将测试的执行范围缩小到标记为的@wipd.

关于名字的说明......

我正在使用@wipd装饰器的名称,而不是@wip避免这种问题:

import unittest
class Test(unittest.TestCase):

    from mymodule import wip    
    @wip
    def test_something(self):
        pass

    def test_something_else(self):
        pass
Run Code Online (Sandbox Code Playgroud)

import将使wip装饰成员类的,以及所有在课堂测试将被选中.该attrib插件检查父类的测试方法,看看是否选择的属性也存在那里,并且被创建和测试属性attrib不以隔离空间存在.因此,如果您使用-a foo并且您的类包含foo = "platypus"测试,那么插件将选择类中的所有测试.


Sku*_*rpi 5

要运行多个特定测试,您只需将它们添加到命令行中,以空格分隔。

nosetests test_web.py:TestWeb.test_checkout test_web.py:TestWeb.test_another_checkout
Run Code Online (Sandbox Code Playgroud)