From 6b51f3a21ebc10ce49ae049e55c859a348dbab3b Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Fri, 17 Aug 2018 16:32:17 +0300 Subject: [PATCH 1/4] Cache selenium fixture at the module level --- test/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/conftest.py b/test/conftest.py index d5bf40318..1723a55fc 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -103,7 +103,7 @@ class ChromeWrapper(SeleniumWrapper): if pytest is not None: - @pytest.fixture(params=['firefox', 'chrome']) + @pytest.fixture(params=['firefox', 'chrome'], scope='module') def selenium(request): if request.param == 'firefox': cls = FirefoxWrapper From 9245f6f2464377c3900e233a7f1e611a8d2d779d Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Mon, 20 Aug 2018 20:02:12 +0300 Subject: [PATCH 2/4] More generic selenium fixture caching mechanism --- test/conftest.py | 28 ++++++++++++++++++++++++++-- test/test_common.py | 15 ++++++++------- test/test_python.py | 6 +++--- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 1723a55fc..34dc54799 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -103,8 +103,8 @@ class ChromeWrapper(SeleniumWrapper): if pytest is not None: - @pytest.fixture(params=['firefox', 'chrome'], scope='module') - def selenium(request): + @pytest.fixture(params=['firefox', 'chrome']) + def selenium_standalone(request): if request.param == 'firefox': cls = FirefoxWrapper elif request.param == 'chrome': @@ -116,6 +116,30 @@ if pytest is not None: print('\n'.join(str(x) for x in selenium.logs)) selenium.driver.quit() + @pytest.fixture(params=['firefox', 'chrome'], scope='module') + def _selenium_cached(request): + # intermediary cached selenium instance, this is a copy + # of the selenium_standalone to avoid fixture scope issues + if request.param == 'firefox': + cls = FirefoxWrapper + elif request.param == 'chrome': + cls = ChromeWrapper + selenium = cls() + try: + yield selenium + finally: + selenium.driver.quit() + + @pytest.fixture + def selenium(_selenium_cached): + # this is selenium instance cached at the module level + try: + # for each test run, we clean selenium logs + _selenium_cached.driver.execute_script("window.logs = []") + yield _selenium_cached + finally: + print('\n'.join(str(x) for x in _selenium_cached.logs)) + PORT = 0 diff --git a/test/test_common.py b/test/test_common.py index 371023ab2..15344cb25 100644 --- a/test/test_common.py +++ b/test/test_common.py @@ -32,16 +32,17 @@ UNSUPPORTED_PACKAGES = {'ChromeWrapper': ['pandas'], @pytest.mark.parametrize('name', registered_packages()) -def test_import(name, selenium): +def test_import(name, selenium_standalone): # check that we can parse the meta.yaml meta = common.parse_package(PKG_DIR / name / 'meta.yaml') - if name in UNSUPPORTED_PACKAGES[selenium.__class__.__name__]: + if name in UNSUPPORTED_PACKAGES[selenium_standalone.__class__.__name__]: pytest.xfail( - '{} fails to load and is not supported on {}.' - .format(name, - selenium.__class__.__name__.replace('Wrapper', ''))) + '{} fails to load and is not supported on {}.' + .format(name, + selenium_standalone.__class__.__name__ + .replace('Wrapper', ''))) for import_name in meta.get('test', {}).get('imports', []): - selenium.load_package(name) - selenium.run('import %s' % import_name) + selenium_standalone.load_package(name) + selenium_standalone.run('import %s' % import_name) diff --git a/test/test_python.py b/test/test_python.py index 98d4324fb..5b2c6be0e 100644 --- a/test/test_python.py +++ b/test/test_python.py @@ -6,9 +6,9 @@ import time import pytest -def test_init(selenium): - assert 'Python initialization complete' in selenium.logs - assert len(selenium.driver.window_handles) == 1 +def test_init(selenium_standalone): + assert 'Python initialization complete' in selenium_standalone.logs + assert len(selenium_standalone.driver.window_handles) == 1 def test_webbrowser(selenium): From ce15a29157f657019c7b9500895260b4d6e9a553 Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Mon, 20 Aug 2018 20:10:32 +0300 Subject: [PATCH 3/4] Fix matplotlib tests --- test/conftest.py | 8 ++++---- test/test_matplotlib.py | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 34dc54799..ec30d02c5 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -118,8 +118,8 @@ if pytest is not None: @pytest.fixture(params=['firefox', 'chrome'], scope='module') def _selenium_cached(request): - # intermediary cached selenium instance, this is a copy - # of the selenium_standalone to avoid fixture scope issues + # Cached selenium instance. This is a copy-paste of + # selenium_standalone to avoid fixture scope issues if request.param == 'firefox': cls = FirefoxWrapper elif request.param == 'chrome': @@ -132,9 +132,9 @@ if pytest is not None: @pytest.fixture def selenium(_selenium_cached): - # this is selenium instance cached at the module level + # selenium instance cached at the module level try: - # for each test run, we clean selenium logs + # clean selenium logs for each test run _selenium_cached.driver.execute_script("window.logs = []") yield _selenium_cached finally: diff --git a/test/test_matplotlib.py b/test/test_matplotlib.py index 9530d7670..65899411d 100644 --- a/test/test_matplotlib.py +++ b/test/test_matplotlib.py @@ -1,12 +1,15 @@ def test_matplotlib(selenium): selenium.load_package("matplotlib") selenium.run("from matplotlib import pyplot as plt") + selenium.run("plt.figure()") selenium.run("x = plt.plot([1,2,3])") + selenium.run("plt.destroy_all()") def test_svg(selenium): selenium.load_package("matplotlib") selenium.run("from matplotlib import pyplot as plt") + selenium.run("plt.figure()") selenium.run("x = plt.plot([1,2,3])") selenium.run("import io") selenium.run("fd = io.BytesIO()") @@ -14,3 +17,4 @@ def test_svg(selenium): content = selenium.run("fd.getvalue().decode('utf8')") assert len(content) == 15752 assert content.startswith(" Date: Mon, 20 Aug 2018 23:29:26 +0300 Subject: [PATCH 4/4] Fix matplotlib and mark test_rlcompleter as crashing --- test/python_tests.txt | 2 +- test/test_matplotlib.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/test/python_tests.txt b/test/python_tests.txt index a182b4485..b760c9519 100644 --- a/test/python_tests.txt +++ b/test/python_tests.txt @@ -368,7 +368,7 @@ test_repl subprocess test_reprlib test_resource test_richcmp -test_rlcompleter +test_rlcompleter crash test_robotparser test_runpy crash test_sax diff --git a/test/test_matplotlib.py b/test/test_matplotlib.py index 65899411d..bd492c6ca 100644 --- a/test/test_matplotlib.py +++ b/test/test_matplotlib.py @@ -3,7 +3,6 @@ def test_matplotlib(selenium): selenium.run("from matplotlib import pyplot as plt") selenium.run("plt.figure()") selenium.run("x = plt.plot([1,2,3])") - selenium.run("plt.destroy_all()") def test_svg(selenium): @@ -17,4 +16,3 @@ def test_svg(selenium): content = selenium.run("fd.getvalue().decode('utf8')") assert len(content) == 15752 assert content.startswith("