I am trying to run selenium with pytest for my Django project and execute the fixture setup/teardown.
I have tried to follow the best practice using yield
but I am getting an error :
--- ERROR at setup of test_browsing_check ---
pytest.fixture functions cannot use ``yield``. Instead write and return an inner function/generator and let the consumer call and iterate over it.:
@pytest.fixture(scope="module")
def browser(request):
selenium = webdriver.Firefox()
selenium .implicitly_wait(3)
yield selenium
selenium.quit()
Do you know why it is not working ?
Then later on I have used another code that works well
@pytest.fixture(scope="module")
def browser(request):
selenium = webdriver.Firefox()
selenium.implicitly_wait(3)
def teardown():
selenium.quit()
request.addfinalizer(teardown)
return selenium
But this method is not recommended:
This method is still fully supported, but yield is recommended from 2.10 onward because it is considered simpler and better describes the natural code flow.
Note about versions:
$ python -V
$ Python 3.5.2 :: Anaconda 4.2.0 (64-bit)
$ django-admin version
$ 1.10.3
$ pip show pytest
$ Name: pytest
$ Version: 2.9.2