问题
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