Skip to content

Commit

Permalink
Better error handling for pytest teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmintz committed May 18, 2017
1 parent 5f31a37 commit 68c6ff9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
2 changes: 2 additions & 0 deletions seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -1455,9 +1455,11 @@ def tearDown(self):
pass
except:
print("No driver to quit.")
self.driver = None
if self.headless:
if self.headless_active:
self.display.stop()
self.display = None
if self.with_db_reporting:
if sys.exc_info()[1] is not None:
self.__insert_test_result(constants.State.ERROR, True)
Expand Down
39 changes: 13 additions & 26 deletions seleniumbase/plugins/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import os
import pytest
import shutil
import sys
import time
from contextlib import contextmanager
from seleniumbase.config import settings
from seleniumbase.fixtures import constants

Expand Down Expand Up @@ -133,34 +131,23 @@ def pytest_runtest_setup():
def pytest_runtest_teardown(item):
""" This runs after every test with pytest """

# Make sure the web driver has exited properly
with suppress_stdout():
# Make sure webdriver has exited properly and any headless display
try:
self = item._testcase
try:
self = item._testcase
try:
if hasattr(self, 'driver') and self.driver:
self.driver.quit()
except:
pass
try:
if hasattr(self, 'headless') and self.headless:
if self.headless_active:
self.display.stop()
except:
pass
if hasattr(self, 'driver') and self.driver:
self.driver.quit()
except:
pass


@contextmanager
def suppress_stdout():
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout
if hasattr(self, 'headless') and self.headless:
if self.headless_active:
if hasattr(self, 'display') and self.display:
self.display.stop()
except:
pass
except:
pass


@pytest.mark.hookwrapper
Expand Down

0 comments on commit 68c6ff9

Please sign in to comment.