Skip to content

Commit

Permalink
Do not use deprecated/removed APIs for session_tmpdir (#116)
Browse files Browse the repository at this point in the history
* Attempt to fix session_tmpdir using deprecated pytest api

* Use new-style warnings

* Do not call fixtures directly

* Use most up-to-date versions of pylama and pylint.

* Fixed linter configuration, to our needs. Previously those tox.ini entries were not necessary because they were provided by pytest_pylama.
  • Loading branch information
youtux committed Feb 11, 2019
1 parent 87887fc commit 8d730d3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 47 deletions.
19 changes: 12 additions & 7 deletions pytest_splinter/plugin.py
Expand Up @@ -5,6 +5,8 @@
"""
import codecs
import functools # pragma: no cover
import warnings

try:
from httplib import HTTPException
except ImportError:
Expand Down Expand Up @@ -274,10 +276,9 @@ def browser_patches():


@pytest.fixture(scope='session')
def session_tmpdir(request, tmpdir_factory):
def session_tmpdir(tmpdir_factory):
"""pytest tmpdir which is session-scoped."""
from _pytest.tmpdir import tmpdir
return tmpdir(request, tmpdir_factory)
return tmpdir_factory.mktemp('pytest-splinter')


@pytest.fixture(scope='session')
Expand Down Expand Up @@ -418,7 +419,7 @@ def _take_screenshot(
}]
})
except Exception as e: # NOQA
request.config.warn('SPL504', "Could not save screenshot: {0}".format(e))
warnings.warn(pytest.PytestWarning("Could not save screenshot: {0}".format(e)))


@pytest.yield_fixture(autouse=True)
Expand Down Expand Up @@ -616,11 +617,14 @@ class SplinterXdistPlugin(object):

"""Plugin class to defer pytest-xdist hook handler."""

def __init__(self, screenshot_dir):
"""Initialize the SplinterXdistPlugin with the required configuration."""
self.screenshot_dir = screenshot_dir

def pytest_testnodedown(self, node, error):
"""Copy screenshots back from remote nodes to have them on the master."""
config_screenshot_dir = splinter_screenshot_dir(node)
for screenshot in getattr(node, 'slaveoutput', {}).get('screenshots', []):
screenshot_dir = os.path.join(config_screenshot_dir, screenshot['class_name'])
screenshot_dir = os.path.join(self.screenshot_dir, screenshot['class_name'])
if not os.path.exists(screenshot_dir):
os.makedirs(screenshot_dir)
for fil in screenshot['files']:
Expand All @@ -633,7 +637,8 @@ def pytest_testnodedown(self, node, error):
def pytest_configure(config):
"""Register pytest-splinter's deferred plugin."""
if config.pluginmanager.getplugin('xdist'):
config.pluginmanager.register(SplinterXdistPlugin())
screenshot_dir = os.path.abspath(config.option.splinter_screenshot_dir)
config.pluginmanager.register(SplinterXdistPlugin(screenshot_dir=screenshot_dir))


def pytest_addoption(parser): # pragma: no cover
Expand Down
4 changes: 2 additions & 2 deletions requirements-testing.txt
@@ -1,8 +1,8 @@
mock
pytest-cov
pytest-localserver
pylama
pylama_pylint
pylama~=7.6.6
pylint~=1.9.4
astroid>=1.4.5
selenium
splinter>=0.9.0
13 changes: 6 additions & 7 deletions tests/mocked_browser/test_screenshot.py
Expand Up @@ -3,8 +3,6 @@

import mock

from _pytest.config import Config


def test_browser_screenshot_normal(mocked_browser, testdir):
"""Test making screenshots on test failure.
Expand All @@ -20,17 +18,18 @@ def test_screenshot(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):
def test_browser_screenshot_error(mocked_browser, testdir):
"""Test warning with error during taking screenshots on test failure."""
mocked_browser.return_value.driver.save_screenshot.side_effect = Exception('Failed')
mocked_browser.return_value.driver_name = 'firefox'
mocked_browser.return_value.html = u'<html>'
testdir.inline_runsource("""

testdir.makepyfile("""
def test_screenshot(browser):
assert False
""", "-v", "-r w", "--splinter-session-scoped-browser=false")
mocked_warn.assert_called_with(mock.ANY, 'SPL504', 'Could not save screenshot: Failed')
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines('*Warning: Could not save screenshot: Failed')


@pytest.mark.skipif('not config.pluginmanager.getplugin("xdist")', reason='pytest-xdist is not installed')
Expand Down
63 changes: 33 additions & 30 deletions tests/test_plugin.py
Expand Up @@ -185,17 +185,18 @@ def test_browser_screenshot_normal(testdir, simple_page_content):
Normal test run.
"""
testdir.inline_runsource("""
import pytest
testdir.inline_runsource(
"""
import pytest
@pytest.fixture
def simple_page(httpserver, browser):
httpserver.serve_content(
'''{0}''', code=200, headers={{'Content-Type': 'text/html'}})
browser.visit(httpserver.url)
@pytest.fixture
def simple_page(httpserver, browser):
httpserver.serve_content(
'''{0}''', code=200, headers={{'Content-Type': 'text/html'}})
browser.visit(httpserver.url)
def test_screenshot(simple_page, browser):
assert False
def test_screenshot(simple_page, browser):
assert False
""".format(simple_page_content), "-vl", "-r w")

assert testdir.tmpdir.join('test_browser_screenshot_normal', 'test_screenshot-browser.png')
Expand All @@ -208,17 +209,18 @@ def test_browser_screenshot_function_scoped_browser(testdir, simple_page_content
Normal test run.
"""
testdir.inline_runsource("""
import pytest
testdir.inline_runsource(
"""
import pytest
@pytest.fixture
def simple_page(httpserver, browser):
httpserver.serve_content(
'''{0}''', code=200, headers={{'Content-Type': 'text/html'}})
browser.visit(httpserver.url)
@pytest.fixture
def simple_page(httpserver, browser):
httpserver.serve_content(
'''{0}''', code=200, headers={{'Content-Type': 'text/html'}})
browser.visit(httpserver.url)
def test_screenshot(simple_page, browser):
assert False
def test_screenshot(simple_page, browser):
assert False
""".format(simple_page_content), "-vl", "-r w", '--splinter-session-scoped-browser=false')

content = testdir.tmpdir.join(
Expand All @@ -235,18 +237,19 @@ def test_browser_screenshot_escaped(testdir, simple_page_content):
Normal test run.
"""
testdir.inline_runsource("""
import pytest
@pytest.fixture
def simple_page(httpserver, browser):
httpserver.serve_content(
'''{0}''', code=200, headers={{'Content-Type': 'text/html'}})
browser.visit(httpserver.url)
@pytest.mark.parametrize('param', ['escaped/param'])
def test_screenshot(simple_page, browser, param):
assert False
testdir.inline_runsource(
"""
import pytest
@pytest.fixture
def simple_page(httpserver, browser):
httpserver.serve_content(
'''{0}''', code=200, headers={{'Content-Type': 'text/html'}})
browser.visit(httpserver.url)
@pytest.mark.parametrize('param', ['escaped/param'])
def test_screenshot(simple_page, browser, param):
assert False
""".format(simple_page_content), "-vl", "-r w")

content = testdir.tmpdir.join(
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -46,7 +46,7 @@ addopts = -vvl -r w
format = pep8
skip = */.tox/*,*/.env/*
linters = pylint,mccabe,pep8,pep257
ignore = F0401,C0111,E731,D100,W0621,W0108,R0201,W0401,W0614,W0212,C901,R0914,D211,D213,D403,R0912
ignore = F0401,C0103,C0111,E731,D100,W0621,W0108,R0201,W0401,W0613,W0614,W0212,C901,R0914,D211,D213,D403,R0903,R0912,R0913

[pylama:pep8]
max_line_length = 120
Expand Down

0 comments on commit 8d730d3

Please sign in to comment.