3

I'm trying to use PyTest and I cannot obtain how to set fixtures. I've tried the following code:

import pytest
import random

@pytest.fixture()
def setup():
    a = random.randint(0, 10)

def test(setup):
    assert 3 > a
if __name__ == '__main__':
    pytest.main()

And I am getting "NameError: name 'a' is not defined".

Also the example from the official documentation doesn't work. What's wrong? I need functionality similar to setUp/tearDown. But I don't want to use unittest. Can someone provide me an example with working fixtures (both setUp type and tearDown type)?

I want to write some test as functions and some test as methods inside classes. Therefore me second question is for an working example of using fixture with classes/methods. I just need to see working examples of fixtures in Python.

Is there a different Python 3 unit testing framework with assertions as simple as in PyTest?

2 Answers 2

8

Fixtures don’t work like this. They cannot magically transfer the name a from one function’s (setup) local scope to another’s (test). Instead, your setup function must explicitly return the object that will be passed as the setup argument to your test function. For example:

import pytest
import random

class TestSetup:
    def __init__(self):
        self.a = random.randint(0, 10)

@pytest.fixture()
def setup():
    return TestSetup()

def test(setup):
    assert 0 <= setup.a <= 10

if __name__ == '__main__':
    pytest.main()
6
  • Thanks Vasiliy. Is there a easier way to setUp/tearDown with pytest? By easier I mean less typing. I'v tried to define variable in fixture and return it, but it doesn't work. I really don't want to define additional class for this. Any advice would be helpful. And again, is there another unit testing framework with assertions defined with statements as in pytest? I want to write as little as possible:) Thanks again.
    – master.py
    Sep 13, 2014 at 16:20
  • @master.py If you only need one variable (a in this case), just return it from setup(). Then, inside test(), the value of the setup parameter will be the value you returned, so you can write: assert 0 <= setup <= 10. Sep 13, 2014 at 16:22
  • Vasiliy! it works. And it is simple. I was confused by documentation. I have two more favors to ask. Can you show me how to set tearDown in similar way - I've read docs and I have no idea :( And how to set default setUp and tearDown for all tests without explicitly putting something like 'def test(setup): '
    – master.py
    Sep 13, 2014 at 16:31
  • @master.py The docs are really clear, see fixture finalization (request.addfinalizer) and autouse fixtures (autouse=True). Sep 13, 2014 at 16:43
  • Vasiliy it doesn't work for me, but I can workaround this issue. Please tell me one last thing how to set fixture (setUp type) for many tests. I'm trying to avoid things like def testa(setup):, def testb(setup): and so on I'm studying docs and nothing - perhaps I'm stupid, perhaps I need more examples.
    – master.py
    Sep 13, 2014 at 19:34
1

More typically you'd do something like:

/usr/bin/env python
import pytest
import random

@pytest.fixture()
def my_rand_int():
    return random.randint(0,10)

def test(my_rand_int):
    assert 3 > my_rand_int

if __name__ == '__main__':
    pytest.main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.