diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_plugin/__init__.py b/tests/test_plugin/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_plugin/test_gui.py b/tests/test_plugin/test_gui.py index 35ae4dc..c48f407 100644 --- a/tests/test_plugin/test_gui.py +++ b/tests/test_plugin/test_gui.py @@ -4,6 +4,8 @@ from mock import patch, MagicMock import redis import pytest +from ..utils_test import create_temp_case, reload_2_3 + REDIS_TEST_PORT = status_plugin.REDIS_PORT + 1 mock_htmlPy_module = MagicMock() @@ -41,8 +43,8 @@ def redis_master(request, auto_shutdown=True): def test_redis_fail_1(tmpdir): # load new redis port import pytest_gui_status.status_gui.gui_backend as gui_backend - reload(gui_backend) - reload(status_plugin) + reload_2_3(gui_backend) + reload_2_3(status_plugin) # dont start redis. Also if redis running, stop it redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0) @@ -69,8 +71,8 @@ def test_redis_fail_1(tmpdir): def test_redis_fail_2(tmpdir, redis_master): # load new redis port import pytest_gui_status.status_gui.gui_backend as gui_backend - reload(gui_backend) - reload(status_plugin) + reload_2_3(gui_backend) + reload_2_3(status_plugin) # start Redis redis_master.init(tmpdir.strpath) @@ -100,8 +102,8 @@ def test_redis_fail_2(tmpdir, redis_master): def test_redis_fail_3(tmpdir, redis_master): # load new redis port import pytest_gui_status.status_gui.gui_backend as gui_backend - reload(gui_backend) - reload(status_plugin) + reload_2_3(gui_backend) + reload_2_3(status_plugin) # start Redis redis_master.init(tmpdir.strpath) @@ -130,8 +132,8 @@ def test_redis_fail_3(tmpdir, redis_master): def test_redis_fail_4(tmpdir, redis_master): # load new redis port import pytest_gui_status.status_gui.gui_backend as gui_backend - reload(gui_backend) - reload(status_plugin) + reload_2_3(gui_backend) + reload_2_3(status_plugin) # start Redis redis_master.init(tmpdir.strpath) diff --git a/tests/test_plugin/test_plugin.py b/tests/test_plugin/test_plugin.py index 9cdba3c..5492b5f 100644 --- a/tests/test_plugin/test_plugin.py +++ b/tests/test_plugin/test_plugin.py @@ -4,64 +4,12 @@ from mock import patch import redis import os -import tempfile -import shutil +from ..utils_test import create_temp_case, reload_2_3 REDIS_TEST_PORT = status_plugin.REDIS_PORT + 1 s = status_plugin.s - -class patched_chdir(object): - - """Just like chdir, but can be used in `with` - sets back the old path at exit""" - - def __init__(self, new_path): - self.old_path = os.getcwd() - self.new_path = new_path - - def __enter__(self): - os.chdir(self.new_path) - - def __exit__(self, type, value, traceback): - os.chdir(self.old_path) - - -def create_temp_case(case_name, path_tmpdir=None): - with patched_chdir(os.path.dirname(__file__)): - path_test_cases = os.path.abspath("../testcases/") - path_case = os.path.join(path_test_cases, case_name) - - path_tmpdir = path_tmpdir or tempfile.mkdtemp() - path_tmpdir_case = os.path.join(path_tmpdir, case_name) - shutil.copytree(path_case, path_tmpdir_case) - - return path_tmpdir_case - - -def reload_2_3(module_name): - ''' - reload that works in all versions of Python. - Uses builtin reload in py2, imp.reload for <=py3.3, - importlib.reload for >=3.4 - - The module six has similar functionalities, - but would be too huge a dependency for this simple case. - ''' - - from sys import version_info - major_ver, minor_ver = version_info[:2] - - if major_ver == 2: - reload(module_name) - elif major_ver == 3 and minor_ver <= 3: - import imp - imp.reload(module_name) - elif major_ver == 3 and minor_ver >= 4: - import importlib - importlib.reload(module_name) - else: - raise NotImplementedError("Not sure how to reload in " - "this version of Python, supported upto 3.x") +CASE_BASEDIR = __file__ @patch.dict("os.environ", @@ -72,7 +20,7 @@ def test_whole_1(tmpdir): ''' os.chdir(os.path.dirname(__file__)) tmpdir = str(tmpdir) - path_case = create_temp_case("case_1", tmpdir) + path_case = create_temp_case("case_1", CASE_BASEDIR, tmpdir) os.chdir(path_case) assert(os.environ.get("PYTEST_STATUS_PORT") == str(REDIS_TEST_PORT)) @@ -101,7 +49,7 @@ def test_whole_2(tmpdir): ''' os.chdir(os.path.dirname(__file__)) tmpdir = str(tmpdir) - path_case = create_temp_case("case_2", tmpdir) + path_case = create_temp_case("case_2", CASE_BASEDIR, tmpdir) os.chdir(path_case) popen_pytest = subprocess.Popen(["py.test", "-s"], shell=True) @@ -126,7 +74,7 @@ def test_intermediate_1(tmpdir): ''' os.chdir(os.path.dirname(__file__)) tmpdir = str(tmpdir) - path_case = create_temp_case("case_3", tmpdir) + path_case = create_temp_case("case_3", CASE_BASEDIR, tmpdir) os.chdir(path_case) popen_pytest = subprocess.Popen(["py.test", "-s"], shell=True) diff --git a/tests/utils_test.py b/tests/utils_test.py new file mode 100644 index 0000000..a031afc --- /dev/null +++ b/tests/utils_test.py @@ -0,0 +1,57 @@ +import os +import tempfile +import shutil + + +class patched_chdir(object): + + """Just like chdir, but can be used in `with` - sets back the old path at exit""" + + def __init__(self, new_path): + self.old_path = os.getcwd() + self.new_path = new_path + + def __enter__(self): + os.chdir(self.new_path) + + def __exit__(self, type, value, traceback): + os.chdir(self.old_path) + + +def create_temp_case(case_name, case_basedir=None, path_tmpdir=None): + case_basedir = case_basedir or __file__ + with patched_chdir(os.path.dirname(case_basedir)): + path_test_cases = os.path.abspath("../testcases/") + path_case = os.path.join(path_test_cases, case_name) + + path_tmpdir = path_tmpdir or tempfile.mkdtemp() + path_tmpdir_case = os.path.join(path_tmpdir, case_name) + shutil.copytree(path_case, path_tmpdir_case) + + return path_tmpdir_case + + +def reload_2_3(module_name): + ''' + reload that works in all versions of Python. + Uses builtin reload in py2, imp.reload for <=py3.3, + importlib.reload for >=3.4 + + The module six has similar functionalities, + but would be too huge a dependency for this simple case. + ''' + + from sys import version_info + major_ver, minor_ver = version_info[:2] + + if major_ver == 2: + reload(module_name) + elif major_ver == 3 and minor_ver <= 3: + import imp + imp.reload(module_name) + elif major_ver == 3 and minor_ver >= 4: + import importlib + importlib.reload(module_name) + else: + raise NotImplementedError("Not sure how to reload in " + "this version of Python, supported upto 3.x")