Consider the following pseudocode demonstrating my question:
import pytest
@pytest.fixture
def param1():
# return smth
yield "wilma"
@pytest.fixture
def param2():
# return smth
yield "fred"
@pytest.fixture
def bar(param1, param2):
#do smth
return [Bar(param1, param2), Bar(param1, param2)]
@pytest.fixture
def first_bar(bar):
return bar[0]
class Test_first_bar:
# FIXME: how do I do that?
#def setup_smth???(self, first_bar):
# self.bar = first_bar
def test_first_bar_has_wilma(self):
# some meaningful check number 1
assert self.bar.wilma == "wilma"
def test_first_bar_some_other_check(self):
# some meaningful check number 2
assert self.bar.fred == "fred"
Basically I want to pass first_bar
fixture to my Test_first_bar
class in order to reuse that object in all its test methods. How should I go about dealing with such situation?
Python 3, if that matters.
pytest fixtures offer dramatic improvements over the classic xUnit style of setup/teardown functions: fixtures have explicit names and are activated by declaring their use from test functions, modules, classes or whole projects.
You can pass arguments to fixtures with the params keyword argument in the fixture decorator, and you can also pass arguments to tests with the @pytest. mark. parametrize decorator for individual tests.
To access the fixture function, the tests have to mention the fixture name as input parameter. Pytest while the test is getting executed, will see the fixture name as input parameter. It then executes the fixture function and the returned value is stored to the input parameter, which can be used by the test.
You can use such a simple parametrized fixture for more complex fixtures by combining the param value with some kind of factory. You can also alias the call to parametrize: numbers = pytest.
Here, you can define your fixture as autouse
. Which would be called automatically for all the tests of your class. Here, I cant understand what is [Bar(param1, param2), Bar(param1, param2)]
. Well, that's not the point, if rest of the code is working fine than you can try the below solution. I have replaced the code with static variables to verify if it's working or not and it's working fine at my end.
import pytest
@pytest.fixture
def param1():
# return smth
yield "wilma"
@pytest.fixture
def param2():
# return smth
yield "fred"
@pytest.fixture
def bar(param1, param2):
# do smth
return [Bar(param1, param2), Bar(param1, param2)]
@pytest.fixture(scope='function', autouse=True)
def first_bar(bar, request):
request.instance.bar = bar[0]
class Test_first_bar:
def test_first_bar_has_wilma(self,request):
print request.instance.bar
def test_first_bar_some_other_check(self,request):
print request.instance.bar
And if you do not want to make fixture as autouse
, than you can call it before your test. Like,
import pytest
@pytest.fixture
def param1():
# return smth
yield "wilma"
@pytest.fixture
def param2():
# return smth
yield "fred"
@pytest.fixture
def bar(param1, param2):
# do smth
return [Bar(param1, param2), Bar(param1, param2)]
@pytest.fixture(scope='function')
def first_bar(bar, request):
request.instance.bar = bar[0]
class Test_first_bar:
@pytest.mark.usefixtures("first_bar")
def test_first_bar_has_wilma(self,request):
print request.instance.bar
@pytest.mark.usefixtures("first_bar")
def test_first_bar_some_other_check(self,request):
print request.instance.bar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With