py.test如何在每次测试之前运行一个方法

3

我以前在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

此答案中的链接已失效。 - Mike C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接