禁用Python测试

Ric*_*man 50 python nosetests

对Python使用nosetests时,可以通过将test function的__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 ...
Run Code Online (Sandbox Code Playgroud)

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

Chr*_*ard 148

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

from nose.tools import nottest

@nottest
def test_my_sample_test()
    #code here ...
Run Code Online (Sandbox Code Playgroud)

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


war*_*iuc 68

你也可以使用unittest.skip装饰器:

import unittest


@unittest.skip("temporarily disabled")
class MyTestCase(unittest.TestCase):
    ...
Run Code Online (Sandbox Code Playgroud)


And*_*org 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 _
Run Code Online (Sandbox Code Playgroud)

示例输出:

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

OK (SKIP=1)
Run Code Online (Sandbox Code Playgroud)

  • 或者你可以在测试开始时"提升SkipTest".另请注意,您可以执行`from nose import SkipTest`. (5认同)

Lou*_*uis 7

您可以使用下划线启动类,方法或函数名称,而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__
Run Code Online (Sandbox Code Playgroud)

从鼻子上输出的输出:

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

OK
Run Code Online (Sandbox Code Playgroud)

Base课程包含在测试中.

我不能只拍@nottestBase,因为这将标志着整个层次.事实上,如果你只是添加@nottest到前面的代码class Base,那么鼻子将不会运行任何测试.

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

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__
Run Code Online (Sandbox Code Playgroud)

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

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

OK
Run Code Online (Sandbox Code Playgroud)

此行为没有很好地记录,但选择测试的代码显式检查类名开头的下划线.

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


Dav*_*ick -18

我认为您还需要将装饰器重命名为尚未经过测试的名称。下面的内容仅在我的第二次测试中失败,而第一个测试不会出现在测试套件中。

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
Run Code Online (Sandbox Code Playgroud)

  • 否决票推理基于两件事的组合:(1)答案应该是“在阅读文档(或源代码)时不要实现自己的装饰器,这会告诉您它已经可用”并且(2)这个答案仍然被接受。如果这个答案只显示:避免在装饰器名称中使用“test”,那么它可能会_有帮助_。 (10认同)
  • 为什么不删除这篇文章并获得“同侪压力”徽章呢? (9认同)