如何在包含nosetests的文件中指定单个测试?

2024-04-25 03:49:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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

但我似乎不能进行单独的测试。当在同一个PWD中运行时,它们会给我“没有这样的测试”错误:

$ nosetests test_web.py:test_checkout
$ nosetests TestWeb:test_checkout

这里可能有什么问题?


Tags: 文件方法pytestwebmostcallnosetests
3条回答

也可以指定模块:

nosetests tests.test_integration:IntegrationTests.test_user_search_returns_users

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

nosetests test_web.py:TestWeb.test_checkout

the docs

像其他答案建议的那样在命令行上指定名称确实有用。然而,当我正在编写测试时,我经常发现我只想运行我正在进行的测试,而且我必须在命令行中编写的名称会变得非常长和繁琐。在这种情况下,我更喜欢使用自定义的decorator和flag。

我定义wipd(“正在工作的装饰器”)如下:

from nose.plugins.attrib import attr
def wipd(f):
    return attr('wip')(f)

这将定义一个decorator@wipd,该decorator将对其所修饰的对象设置wip属性。例如:

import unittest
class Test(unittest.TestCase):

    @wipd
    def test_something(self):
        pass

然后,可以在命令行使用-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

import将使wip装饰程序成为类的成员,并且将选择类中的所有测试。attrib插件检查测试方法的父类,以查看所选属性是否也存在,并且由attrib创建和测试的属性不存在于隔离空间中。因此,如果使用-a foo进行测试,并且类包含foo = "platypus",则该插件将选择类中的所有测试。

相关问题 更多 >