我正在使用带有 python 的谷歌应用程序引擎,并想使用nosetest运行一些测试。
我希望每个测试都运行相同的设置功能。我已经进行了很多测试,所以我不想遍历所有测试并复制并粘贴相同的功能。我可以在某处定义一个设置函数并且每个测试都会首先运行它吗?
thanks.
您可以编写您的设置函数并使用with_setup
装饰器:
from nose.tools import with_setup
def my_setup():
...
@with_setup(my_setup)
def test_one():
...
@with_setup(my_setup)
def test_two():
...
如果您想对多个测试用例使用相同的设置,您可以使用类似的方法。
首先创建设置函数,然后使用装饰器将其应用到所有测试用例:
def my_setup(self):
#do the setup for the test-case
def apply_setup(setup_func):
def wrap(cls):
cls.setup = setup_func
return cls
return wrap
@apply_setup(my_setup)
class MyTestCaseOne(unittest.TestCase):
def test_one(self):
...
def test_two(self):
...
@apply_setup(my_setup)
class MyTestCaseTwo(unittest.TestCase):
def test_one(self):
...
或者另一种方法是简单地分配您的设置:
class MyTestCaseOne(unittest.TestCase):
setup = my_setup
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)