py.test 如何在每个测试之前运行一个方法
我之前在Python中使用unittest和nose来进行单元测试,但现在我在使用py.test。
unittest和nose在执行TestCase中的每个方法之前,都会调用class.setUp
。
那我该如何在py.test中做到这一点呢?
编辑:如果我加上这个:
def setup_class(cls):
cls.a = pypol.polynomial('x^3 - 2x^2 + x -5')
cls.b = pypol.polynomial('a^3 - 2x^2 - b + 3')
cls.c = pypol.polynomial('-x + 1')
cls.d = pypol.polynomial('a')
我就会遇到所有的错误:
_____________________________ TestPolynomial.testSetitem ______________________________
self = <test_pypol.TestPolynomial object at 0x97355ec>
def testSetitem(self):
> TestPolynomial.a[2] = (3, {'x': 3, 'y': 4})
E AttributeError: type object 'TestPolynomial' has no attribute 'a'
test_pypol.py:162: AttributeError
_____________________________ TestPolynomial.testDelitem ______________________________
self = <test_pypol.TestPolynomial object at 0x9735eac>
def testDelitem(self):
> del TestPolynomial.a[1:3]
E AttributeError: type object 'TestPolynomial' has no attribute 'a'
编辑2:好吧,我真傻。我应该把它放在TestCase里面,而不是外面。谢谢大家。
1 个回答
1
听起来你需要在测试类里定义一个叫做 setup_method
的方法:
https://codespeak.net/py/0.9.2/test.html#managing-test-state-across-test-modules-classes-and-methods