diff --git a/pytest_splinter/plugin.py b/pytest_splinter/plugin.py index 597e3ae..7c39a40 100644 --- a/pytest_splinter/plugin.py +++ b/pytest_splinter/plugin.py @@ -311,10 +311,7 @@ def prepare_browser(parent): prepare_browser(parent) def make_screenshot_on_failure(): - if not splinter_make_screenshot_on_failure or not request.node.splinter_failure: - return - - try: + if splinter_make_screenshot_on_failure and request.node.splinter_failure: slaveoutput = getattr(request.config, 'slaveoutput', None) names = junitxml.mangle_testnames(request.node.nodeid.split("::")) classname = '.'.join(names[:-1]) @@ -328,17 +325,17 @@ def make_screenshot_on_failure(): screenshot_dir = tmpdir.mkdir('screenshots').strpath screenshot_path = os.path.join(screenshot_dir, screenshot_file_name) LOGGER.info('Saving screenshot to {0}'.format(screenshot_path)) - browser.driver.save_screenshot(screenshot_path) - with open(screenshot_path) as fd: - if slaveoutput is not None: - slaveoutput.setdefault('screenshots', []).append({ - 'class_name': classname, - 'file_name': screenshot_file_name, - 'content': fd.read() - }) - except Exception as e: - request.config.warn('splinter', "Could not save screenshot: {0}".format(e)) - pass + try: + browser.driver.save_screenshot(screenshot_path) + with open(screenshot_path) as fd: + if slaveoutput is not None: + slaveoutput.setdefault('screenshots', []).append({ + 'class_name': classname, + 'file_name': screenshot_file_name, + 'content': fd.read() + }) + except Exception as e: + request.config.warn('SPL504', "Could not save screenshot: {0}".format(e)) request.addfinalizer(make_screenshot_on_failure) return browser diff --git a/tests/conftest.py b/tests/conftest.py index ac1f68a..9fd82f7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,32 +1,2 @@ """Configuration for pytest runner.""" -import mock - -import pytest - pytest_plugins = 'pytester' - - -@pytest.fixture -def mocked_browser(browser_pool, request): - """Mock splinter browser.""" - # to avoid re-using of cached browser from other tests - for browser in browser_pool.values(): - browser.quit() - browser_pool.clear() - - def mocked_browser(*args, **kwargs): - mocked_browser = mock.MagicMock() - mocked_browser.driver = mock.MagicMock() - mocked_browser.driver.profile = mock.MagicMock() - - def save_screenshot(path): - with open(path, 'w'): - pass - - mocked_browser.driver.save_screenshot = save_screenshot - return mocked_browser - - patcher = mock.patch('pytest_splinter.plugin.splinter.Browser', mocked_browser) - request.addfinalizer(patcher.stop) - patcher.start() - return mocked_browser diff --git a/tests/mocked_browser/conftest.py b/tests/mocked_browser/conftest.py index 8726dd4..926856f 100644 --- a/tests/mocked_browser/conftest.py +++ b/tests/mocked_browser/conftest.py @@ -1,6 +1,5 @@ """Configuration for pytest runner.""" - -pytest_plugins = 'pytester' +import mock import pytest @@ -11,7 +10,26 @@ def splinter_session_scoped_browser(): return False -@pytest.fixture(autouse=True) -def mocked_browser(mocked_browser): - """Make mocked browser fixture autoused.""" - return mocked_browser +@pytest.yield_fixture(autouse=True) +def mocked_browser(browser_pool, request): + """Mock splinter browser.""" + # to avoid re-using of cached browser from other tests + for browser in browser_pool.values(): + browser.quit() + browser_pool.clear() + + def mocked_browser(*args, **kwargs): + mocked_browser = mock.MagicMock() + mocked_browser.driver = mock.MagicMock() + mocked_browser.driver.profile = mock.MagicMock() + + def save_screenshot(path): + with open(path, 'w'): + pass + + mocked_browser.driver.save_screenshot = save_screenshot + return mocked_browser + + patcher = mock.patch('pytest_splinter.plugin.splinter.Browser', mocked_browser) + yield patcher.start() + patcher.stop() diff --git a/tests/test_screenshot.py b/tests/mocked_browser/test_screenshot.py similarity index 56% rename from tests/test_screenshot.py rename to tests/mocked_browser/test_screenshot.py index 62facbc..cfe6a67 100644 --- a/tests/test_screenshot.py +++ b/tests/mocked_browser/test_screenshot.py @@ -1,11 +1,13 @@ """Browser screenshot tests.""" import pytest -from mock import patch +import mock +from _pytest.config import Config -def test_browser_screenshot_normal(testdir, mocked_browser): - """Test making screenshots on test failure if the commandline option is passed. + +def test_browser_screenshot_normal(testdir): + """Test making screenshots on test failure. Normal test run. """ @@ -17,23 +19,21 @@ def test_screenshot(browser): assert testdir.tmpdir.join('test_browser_screenshot_normal', 'test_screenshot-browser.png').isfile() -@patch('_pytest.config.Config.warn') -def test_browser_screenshot_error(testdir, mocked_browser): +@mock.patch('pytest_splinter.plugin.splinter.Browser') +@mock.patch.object(Config, 'warn', autospec=True) +def test_browser_screenshot_error(mocked_warn, mocked_browser, testdir): """Test warning with error during taking screenshots on test failure.""" + mocked_browser.return_value.driver.save_screenshot.side_effect = Exception('Failed') testdir.inline_runsource(""" def test_screenshot(browser): - # Create a file here, so makedirs in make_screenshot_on_failure will fail. - open('isafile', 'w').close() assert False - - def test_warn_called(request): - assert request.config.warn.call_count == 1 - """, "-vl", "--splinter-session-scoped-browser=false") + """, "-vvl", "-r w", "--splinter-session-scoped-browser=false") + mocked_warn.assert_called_with(mock.ANY, 'SPL504', 'Could not save screenshot: Failed') @pytest.mark.skipif('not config.pluginmanager.getplugin("xdist")', reason='pytest-xdist is not installed') -def test_browser_screenshot_xdist(testdir, mocked_browser): - """Test making screenshots on test failure if the commandline option is passed. +def test_browser_screenshot_xdist(testdir): + """Test making screenshots on test failure in distributed mode (xdist). Distributed test run. """ diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 44f1790..a4b4052 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -1,4 +1,4 @@ -"""Tests for pytest-bdd-splinter subplugin.""" +"""Tests for pytest-splinter plugin.""" import os.path import time