From c9ac0d06a6b7e6697df31db27a82d02d3c8102dc Mon Sep 17 00:00:00 2001 From: mwwsp Date: Wed, 3 Oct 2018 16:52:52 +0100 Subject: [PATCH 1/4] fix writing results to file method --- axe_selenium_python/axe.py | 2 +- axe_selenium_python/tests/test_axe.py | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/axe_selenium_python/axe.py b/axe_selenium_python/axe.py index 8c74072..8b412f6 100755 --- a/axe_selenium_python/axe.py +++ b/axe_selenium_python/axe.py @@ -100,5 +100,5 @@ def write_results(self, data, name="results.json"): :param name: Name of file to be written to. :param output: JSON object. """ - with open(name, "r", encoding="utf8") as f: + with open(name, "w", encoding="utf8") as f: f.write(json.dumps(data, indent=4)) diff --git a/axe_selenium_python/tests/test_axe.py b/axe_selenium_python/tests/test_axe.py index 4217572..160cdac 100755 --- a/axe_selenium_python/tests/test_axe.py +++ b/axe_selenium_python/tests/test_axe.py @@ -4,7 +4,7 @@ from os import path, getenv -import pytest +import json, pytest from selenium import webdriver from ..axe import Axe @@ -62,3 +62,25 @@ def _perform_axe_run(driver): axe.inject() data = axe.execute() return data + + +@pytest.fixture +def tempdir(): + import tempfile + import shutil + new_dir = tempfile.mkdtemp() + yield new_dir + shutil.rmtree(new_dir) + + +def test_write_results_to_file(firefox_driver, tempdir): + axe = Axe(firefox_driver) + data = json.dumps({"testKey": "testValue"}) + filename = path.join(tempdir, "results.json") + + axe.write_results(data=data, name=filename) + + with open(filename) as f: + actual_file_contents = json.loads(f.read()) + + assert data == actual_file_contents From fe4d041c9f7ccced5edb8e11d20852d881f7cb69 Mon Sep 17 00:00:00 2001 From: mwwsp Date: Wed, 3 Oct 2018 17:10:50 +0100 Subject: [PATCH 2/4] updated file write test with webdriver mock --- axe_selenium_python/tests/test_axe.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/axe_selenium_python/tests/test_axe.py b/axe_selenium_python/tests/test_axe.py index 160cdac..16501a1 100755 --- a/axe_selenium_python/tests/test_axe.py +++ b/axe_selenium_python/tests/test_axe.py @@ -4,7 +4,9 @@ from os import path, getenv -import json, pytest +import json +import pytest +from unittest import mock from selenium import webdriver from ..axe import Axe @@ -73,8 +75,8 @@ def tempdir(): shutil.rmtree(new_dir) -def test_write_results_to_file(firefox_driver, tempdir): - axe = Axe(firefox_driver) +def test_write_results_to_file(tempdir): + axe = Axe(mock.MagicMock()) data = json.dumps({"testKey": "testValue"}) filename = path.join(tempdir, "results.json") From 42fc436e7c781c10d1f9e6e13383a4fd53612153 Mon Sep 17 00:00:00 2001 From: Kimberly Date: Thu, 4 Oct 2018 10:28:54 -0600 Subject: [PATCH 3/4] fixed mock import error, added mock to reqs file, fixed unicode write error --- axe_selenium_python/axe.py | 5 ++++- axe_selenium_python/tests/requirements/tests.txt | 1 + axe_selenium_python/tests/test_axe.py | 13 +++++++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/axe_selenium_python/axe.py b/axe_selenium_python/axe.py index 8b412f6..fe9760b 100755 --- a/axe_selenium_python/axe.py +++ b/axe_selenium_python/axe.py @@ -101,4 +101,7 @@ def write_results(self, data, name="results.json"): :param output: JSON object. """ with open(name, "w", encoding="utf8") as f: - f.write(json.dumps(data, indent=4)) + try: + f.write(unicode(json.dumps(data, indent=4))) + except NameError: + f.write(json.dumps(data, indent=4)) diff --git a/axe_selenium_python/tests/requirements/tests.txt b/axe_selenium_python/tests/requirements/tests.txt index 5f06fce..dd91706 100644 --- a/axe_selenium_python/tests/requirements/tests.txt +++ b/axe_selenium_python/tests/requirements/tests.txt @@ -1,3 +1,4 @@ pytest==3.8.2 selenium==3.14.1 pytest-html==1.19.0 +mock==2.0.0 diff --git a/axe_selenium_python/tests/test_axe.py b/axe_selenium_python/tests/test_axe.py index 16501a1..11c2894 100755 --- a/axe_selenium_python/tests/test_axe.py +++ b/axe_selenium_python/tests/test_axe.py @@ -2,11 +2,10 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at http://mozilla.org/MPL/2.0/. -from os import path, getenv - import json +from os import getenv, path + import pytest -from unittest import mock from selenium import webdriver from ..axe import Axe @@ -70,13 +69,19 @@ def _perform_axe_run(driver): def tempdir(): import tempfile import shutil + new_dir = tempfile.mkdtemp() yield new_dir shutil.rmtree(new_dir) def test_write_results_to_file(tempdir): - axe = Axe(mock.MagicMock()) + try: + from unittest.mock import MagicMock + except ImportError: + from mock import MagicMock + + axe = Axe(MagicMock()) data = json.dumps({"testKey": "testValue"}) filename = path.join(tempdir, "results.json") From 9f9f76a1607ac1bf6a7e5562e8e25dc18acd17ad Mon Sep 17 00:00:00 2001 From: Kimberly Date: Thu, 4 Oct 2018 13:38:51 -0600 Subject: [PATCH 4/4] replaced mock with pytest-mock, using pytest tmpdir fixture --- .../tests/requirements/tests.txt | 2 +- axe_selenium_python/tests/test_axe.py | 25 ++++--------------- tox.ini | 12 +++------ 3 files changed, 9 insertions(+), 30 deletions(-) diff --git a/axe_selenium_python/tests/requirements/tests.txt b/axe_selenium_python/tests/requirements/tests.txt index dd91706..dad738a 100644 --- a/axe_selenium_python/tests/requirements/tests.txt +++ b/axe_selenium_python/tests/requirements/tests.txt @@ -1,4 +1,4 @@ pytest==3.8.2 selenium==3.14.1 pytest-html==1.19.0 -mock==2.0.0 +pytest-mock==1.10.0 diff --git a/axe_selenium_python/tests/test_axe.py b/axe_selenium_python/tests/test_axe.py index 11c2894..ca372e3 100755 --- a/axe_selenium_python/tests/test_axe.py +++ b/axe_selenium_python/tests/test_axe.py @@ -65,27 +65,12 @@ def _perform_axe_run(driver): return data -@pytest.fixture -def tempdir(): - import tempfile - import shutil - - new_dir = tempfile.mkdtemp() - yield new_dir - shutil.rmtree(new_dir) - - -def test_write_results_to_file(tempdir): - try: - from unittest.mock import MagicMock - except ImportError: - from mock import MagicMock - - axe = Axe(MagicMock()) - data = json.dumps({"testKey": "testValue"}) - filename = path.join(tempdir, "results.json") +def test_write_results_to_file(tmpdir, mocker): + axe = Axe(mocker.MagicMock()) + data = {"testKey": "testValue"} + filename = path.join(str(tmpdir), "results.json") - axe.write_results(data=data, name=filename) + axe.write_results(data, filename) with open(filename) as f: actual_file_contents = json.loads(f.read()) diff --git a/tox.ini b/tox.ini index f851245..5f669ba 100644 --- a/tox.ini +++ b/tox.ini @@ -3,16 +3,10 @@ envlist = py27, py36, flake8 skipsdist = true [testenv] -passenv = HOME DISPLAY MOZ_HEADLESS PYTEST_ADDOPTS PYTEST_BASE_URL \ - SAUCELABS_USR SAUCELABS_PSW JENKINS_URL JOB_NAME BUILD_NUMBER +setenv = + MOZ_HEADLESS = 1 deps = -raxe_selenium_python/tests/requirements/tests.txt -commands = pytest - -[testenv:py27] -commands = pytest - -[testenv:py36] -commands = pytest +commands = pytest -rsx --verbose [testenv:flake8] deps = -raxe_selenium_python/tests/requirements/flake8.txt