繁体   English   中英

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

[英]How do I specify a single test in a file with nosetests?

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

这可能有什么问题?

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

nosetests test_web.py:TestWeb.test_checkout

查看文档

您还可以指定一个模块:

nosetests tests.test_integration:IntegrationTests.test_user_search_returns_users

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

我定义了wipd (“work in progress decorator”),如下所示:

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

这定义了一个装饰@wipd将设置在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" ,则插件将选择类中的所有测试。

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

nosetests test_web.py:TestWeb.test_checkout test_web.py:TestWeb.test_another_checkout

在我的测试中, 使用模块名称指定测试不起作用

您必须指定.py的实际路径:

nosetests /path/to/test/file.py:test_function

这与nose==1.3.7

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM