Vadim  Kovrizhkin
Vadim Kovrizhkin

Reputation: 1781

pytest AttributeError when using fixture with yield

I am using pytest fixture with yield. But receive AttributeError when trying to get value that yield returns

conftest.py

@pytest.fixture()
def driver_setup():
    driver = webdriver.Firefox()
    yield driver
    driver.quit()

basetest.py

@pytest.mark.usefixtures("driver_setup")
class BaseTest:
    pass

test_example.py

class TestExample(BaseTest):

    def test_example(self):
        self.driver.get(url)
        pass

Output: AttributeError: 'TestExample' object has no attribute 'driver'

Upvotes: 0

Views: 872

Answers (1)

Dušan Maďar
Dušan Maďar

Reputation: 9849

You need to update driver_setup fixture as below if you want access to driver in tests.

@pytest.fixture()
def driver_setup(request):
    driver = webdriver
    request.cls.driver = driver
    yield
    driver.quit()

For more details refer to http://computableverse.com/blog/pytest-sharing-class-fixtures.

Upvotes: 3

Related Questions