pytest AttributeError when using fixture with yield

随声附和 提交于 2021-01-08 15:30:24

问题


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'


回答1:


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.



来源:https://stackoverflow.com/questions/47108331/pytest-attributeerror-when-using-fixture-with-yield

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!