52

当使用 Python 的鼻子测试时,可以通过将测试函数的__test__属性设置为 false 来禁用单元测试。我已经使用以下装饰器实现了这一点:

def unit_test_disabled():
    def wrapper(func):
         func.__test__ = False
         return func

    return wrapper

@unit_test_disabled
def test_my_sample_test()
    #code here ...

但是,这具有将包装器称为单元测试的副作用。Wrapper 将始终通过,但它包含在 nosetests 输出中。是否有另一种构造装饰器的方法,以便测试不会运行并且不会出现在鼻子测试输出中。

4

5 回答 5

156

Nose 已经为此内置了一个装饰器:

from nose.tools import nottest

@nottest
def test_my_sample_test()
    #code here ...

还可以查看鼻子提供的其他好东西:https ://nose.readthedocs.org/en/latest/testing_tools.html

于 2009-12-03T21:39:10.543 回答
74

您还可以使用unittest.skip装饰器:

import unittest


@unittest.skip("temporarily disabled")
class MyTestCase(unittest.TestCase):
    ...
于 2015-02-20T14:06:06.997 回答
32

还有一个用于nosetest 的skiptest 插件,它会导致测试输出中的测试显示为已跳过。这是一个装饰器:

def skipped(func):
    from nose.plugins.skip import SkipTest
    def _():
        raise SkipTest("Test %s is skipped" % func.__name__)
    _.__name__ = func.__name__
    return _

示例输出:

$ nosetests tests
..........................................................................
..................................S.............
----------------------------------------------------------------------
Ran 122 tests in 2.160s

OK (SKIP=1)
于 2011-10-07T13:08:28.233 回答
9

您可以只使用下划线开头的类、方法或函数名称,nose 会忽略它。

@nottest有它的用途,但我发现当类相互派生并且鼻子必须忽略某些基类时它不能很好地工作。当我有一系列类似的 Django 视图要测试时,这种情况经常发生。他们经常共享需要测试的特征。例如,只有具有特定权限的用户才能访问它们。我没有为所有这些编写相同的权限检查,而是将这样的共享测试放在其他类派生的初始类中。但问题是基类只能由后面的类派生出来,而不是要独立运行。这是问题的一个例子:

from unittest import TestCase

class Base(TestCase):

    def test_something(self):
        print "Testing something in " + self.__class__.__name__

class Derived(Base):

    def test_something_else(self):
        print "Testing something else in " + self.__class__.__name__

流鼻涕的输出:

$ nosetests test.py -s
Testing something in Base
.Testing something in Derived
.Testing something else in Derived
.
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

该类Base包含在测试中。

我不能只是打耳光@nottestBase因为它会标记整个层次结构。事实上,如果你只是@nottest在上面的代码前面添加class Base,nose 将不会运行任何测试。

我所做的是在基类前面添加一个下划线:

from unittest import TestCase

class _Base(TestCase):

    def test_something(self):
        print "Testing something in " + self.__class__.__name__

class Derived(_Base):

    def test_something_else(self):
        print "Testing something else in " + self.__class__.__name__

并且在运行时会_Base被忽略:

$ nosetests test3.py -s
Testing something in Derived
.Testing something else in Derived
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

这种行为没有得到很好的记录,但是选择测试的代码明确地检查类名开头的下划线

鼻子对函数和方法名称执行类似的测试,因此可以通过在名称开头添加下划线来排除它们。

于 2016-05-13T16:59:47.853 回答
-17

我认为您还需要将您的装饰器重命名为尚未经过测试的东西。以下仅在我的第二次测试中失败,而第一次没有出现在测试套件中。

def unit_disabled(func):
    def wrapper(func):
         func.__test__ = False
         return func

    return wrapper

@unit_disabled
def test_my_sample_test():
    assert 1 <> 1

def test2_my_sample_test():
    assert 1 <> 1
于 2009-07-13T15:57:44.303 回答