I have a suite of selenium end to end tests. I instantiate one webdriver.ChromeDriver instance that I pass as a fixture to every test in the suite.
'''python3
@pytest.fixture(scope="session")
def driver(request):
return d(request)
'''
After the last test runs I want to close the webdriver instance with driver.quit().
I can accomplish this with a fixture
@pytest.hookimpl(trylast=True)
def close_after_test_suite(driver, requests):
if requests:
driver.quit()
However this only runs if tests do not fail during the suite. If every test passes, the browser will close. If a single test fails then the browser will be left open.
How can I use pytest to close my browser at the end of the test suite regardless of whether or not tests fail?