rubik
rubik

Reputation: 9104

py.test how to run a method before every test

I used unittest and nose for unit-testing in Python but now I'm using py.test.
unittest and nose always call class.setUp before executing every method in the TestCase.

How can I do this with py.test?

EDIT: If I add this:

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')

I get all errors:

_____________________________ 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'

EDIT2: Ok, I'm stupid. I had to put inside the TestCase and not outside. Thank you.

Upvotes: 3

Views: 1103

Answers (1)

Kyle Wild
Kyle Wild

Reputation: 8915

Sounds like you need to define setup_method on the test class:

https://codespeak.net/py/0.9.2/test.html#managing-test-state-across-test-modules-classes-and-methods

Upvotes: 1

Related Questions