禁用Python鼻子测试 [英] Disabling Python nosetests
问题描述
在Python上使用鼻子测试时,可以通过将测试函数的__test__
属性设置为false来禁用单元测试.我已经使用以下装饰器实现了这一点:
When using nosetests for Python it is possible to disable a unit test by setting the test function's __test__
attribute to false. I have implemented this using the following decorator:
def unit_test_disabled():
def wrapper(func):
func.__test__ = False
return func
return wrapper
@unit_test_disabled
def test_my_sample_test()
#code here ...
但是,这具有调用包装器作为单元测试的副作用.包装器将始终通过,但将包含在鼻子测试输出中.还有另一种构造装饰器的方法,以使测试不会运行并且不会出现在鼻子测试输出中.
However, this has the side effect of calling wrapper as the unit test. Wrapper will always pass but it is included in nosetests output. Is there another way of structuring the decorator so that the test will not run and does not appear in nosetests output.
推荐答案
我认为您还需要将装饰器重命名为未经过测试的东西.没有出现在测试套件中.
I think you will also need to rename your decorator to something that has not got test in. The below only fails on the second test for me and the first does not show up in the test suite.
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
这篇关于禁用Python鼻子测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!