如何为所有鼻子测试测试定义一个设置功能? [英] How can I define one setup function for all nosetests tests?
问题描述
我正在将google app引擎与python结合使用,并希望使用鼻子测试运行一些测试. 我希望每个测试都运行相同的设置功能.我已经有很多测试,所以我不想全部通过测试并复制并粘贴相同的功能.我可以在某个地方定义一个设置功能,并且每个测试都会首先运行它吗?
I'm using google app engine with python and want to run some tests using nosetest. I want each test to run the same setup function. I have already a lot of tests, so I don't want to go through them all and copy&paste the same function. can I define somewhere one setup function and each test would run it first?
谢谢.
推荐答案
您可以编写设置函数,并使用with_setup
装饰器将其应用:
You can write your setup function and apply it using the with_setup
decorator:
from nose.tools import with_setup
def my_setup():
...
@with_setup(my_setup)
def test_one():
...
@with_setup(my_setup)
def test_two():
...
如果要对多个测试用例使用相同的设置,则可以使用类似的方法. 首先创建设置功能,然后使用装饰器将其应用于所有TestCases:
If you want to use the same setup for several test-cases you can use a similar method. First you create the setup function, then you apply it to all the TestCases with a decorator:
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):
...
或者另一种方法是简单地分配您的设置:
Or another way could be to simply assign your setup:
class MyTestCaseOne(unittest.TestCase):
setup = my_setup
这篇关于如何为所有鼻子测试测试定义一个设置功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!