#!/usr/bin/env python from __future__ import print_function, division, absolute_import import os import inspect import pytest_helper import set_package_attribute if __name__ == "__main__": #pytest_helper.init(set_package=True) # Finds no tests when this is set! pytest_helper.init() import sys #pytest_helper.script_run(self_test=True) pytest_helper.script_run(self_test="test_in_package_subdir", pyargs=True) pytest_helper.sys_path("../../..") from package_dir.package_subdir import subdir_dummy_module pytest_helper.autoimport() def test_running_test_in_package_subdir(): assert True
from __future__ import print_function, division, absolute_import import pytest_helper from pytest_helper import PytestHelperException, LocalsToGlobalsError pytest_helper.init() # Needed because of testing chdir call inserted below. import os old_cwd = os.getcwd() os.chdir("..") # Causes an error if pytest_helper.init() is not called above. pytest_helper.script_run(self_test=True, pytest_args="-v -s") # More efficient to put below two lines after script_run (won't run twice). pytest_helper.autoimport() # The module to be tested is in_child_dir. # The import below defines test_string="in_child_dir". pytest_helper.sys_path(add_grandparent=True) from package_dir.in_child_dir import * # Note imported as a package module. #pytest_helper.sys_path(add_parent=True) #from in_child_dir import * # Note imported as a regular module, NOT a package module. os.chdir(old_cwd) # Return to prev dir so as not to mess up later tests. @fixture def basic_setup(): teststr = "tree" teststr2 = "rock" # Normally you'd only call locals_to_globals at the end of setup funs, like here. locals_to_globals() def test_basic_stuff(basic_setup): assert test_string == "in_child_dir" # This is set as a global in in_child_dir.
from __future__ import print_function, division, absolute_import import pytest_helper from pytest_helper import PytestHelperException, LocalsToGlobalsError pytest_helper.init() # Needed because of testing chdir call inserted below. import os old_cwd = os.getcwd() os.chdir("..") # Causes an error if pytest_helper.init() is not called above. #pytest_helper.script_run("package_dir.test_in_same_dir", pytest_args="-v -s", pyargs=True) pytest_helper.script_run(self_test=True, pytest_args="-v -s", pyargs=True) #pytest_helper.script_run(self_test=True, pytest_args="-v -s") # More efficient to put below two lines after script_run (won't run twice). pytest_helper.autoimport() pytest_helper.sys_path(add_self=True) # Needs to add own dir when run as test. # The module to be tested is `in_same_dir`. # The module imported below defines `test_string="in_same_dir"`. from .in_same_dir import * # Test restoring the system path. pytest_helper.sys_path("../package_dir/test") pytest_helper.restore_previous_sys_path() with raises(ImportError): import test_in_child_dir # This succeeds without the restore above. os.chdir(old_cwd) # Return to prev dir so as not to mess up later tests. @fixture
from pytest_helper import PytestHelperException, LocalsToGlobalsError pytest_helper.init() # Needed because of testing chdir call inserted below. import os old_cwd = os.getcwd() os.chdir("..") # Causes an error if pytest_helper.init() is not called above. pytest_helper.script_run(self_test=True, pytest_args="-v -s") # More efficient to put below two lines after script_run (won't run twice). pytest_helper.autoimport() # The module to be tested is in_child_dir. # The import below defines test_string="in_child_dir". pytest_helper.sys_path(add_grandparent=True) from package_dir.in_child_dir import * # Note imported as a package module. #pytest_helper.sys_path(add_parent=True) #from in_child_dir import * # Note imported as a regular module, NOT a package module. os.chdir(old_cwd) # Return to prev dir so as not to mess up later tests. @fixture def basic_setup(): teststr = "tree" teststr2 = "rock" # Normally you'd only call locals_to_globals at the end of setup funs, like here. locals_to_globals()
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function, division, absolute_import import pytest_helper """ This is just here to test multiple files being specified. It doesn't do any tests that test_in_child_dir doesn't do (and it does fewer). """ pytest_helper.script_run(self_test=True) pytest_helper.sys_path("../package_dir") pytest_helper.autoimport() from in_sibling_dir import * def test_basic_stuff(): assert test_string == "in_sibling_dir" def test_autoimports(): teststr = "water" with pytest.raises(KeyError): raise KeyError locals_to_globals()
# Below commented-out line FAILS and even set_package can't help it, because # set_package imports the module under its full package name, which pytest then # tries to also import, but it refuses to reload it and gives an "import file # mismatch" error. So don't do this, put script_run near top or use absolute # imports. #from . import dummy_module import os old_cwd = os.getcwd() os.chdir("..") # Causes an error if pytest_helper.init() is not called above. pytest_helper.script_run(self_test=True, pytest_args="-v -s") # Put below two lines after script_run (won't be run twice). pytest_helper.sys_path(["../package_dir"]) pytest_helper.autoimport() os.chdir(old_cwd) # Return to prev dir so as not to mess up later tests. # This DOES work, because pytest invocation runs it (works even with the # self_test option passing it a filename rather than a package name). from . import dummy_module @fixture def basic_setup(): teststr = "tree" teststr2 = "rock" # Normally you'd only call locals_to_globals at the end of setup funs, like here. locals_to_globals()
from __future__ import print_function, division, absolute_import import pytest_helper from pytest_helper import PytestHelperException, LocalsToGlobalsError pytest_helper.init() # Needed because of testing chdir call inserted below. import os old_cwd = os.getcwd() os.chdir("..") # Causes an error if pytest_helper.init() is not called above. #pytest_helper.script_run("package_dir.test_in_same_dir", pytest_args="-v -s", pyargs=True) pytest_helper.script_run(self_test=True, pytest_args="-v -s", pyargs=True) #pytest_helper.script_run(self_test=True, pytest_args="-v -s") # More efficient to put below two lines after script_run (won't run twice). pytest_helper.autoimport() pytest_helper.sys_path(add_self=True) # Needs to add own dir when run as test. # The module to be tested is `in_same_dir`. # The module imported below defines `test_string="in_same_dir"`. from .in_same_dir import * # Test restoring the system path. pytest_helper.sys_path("../package_dir/test") pytest_helper.restore_previous_sys_path() with raises(ImportError): import test_in_child_dir # This succeeds without the restore above. os.chdir(old_cwd) # Return to prev dir so as not to mess up later tests. @fixture def basic_setup():
break things. """ from __future__ import print_function, division, absolute_import import pytest_helper from pytest_helper import PytestHelperException, LocalsToGlobalsError pytest_helper.init() import os old_cwd = os.getcwd() os.chdir("..") # Causes an error if init() is not called above. pytest_helper.script_run(self_test=True, pytest_args="-v -s") # More efficient to put below two lines after script_run (won't run twice). pytest_helper.sys_path("../../../") # contains package_dir, to import it pytest_helper.autoimport() os.chdir(old_cwd) # Return to prev dir so as not to mess up later tests. # Test the pytest_helper routine get_calling_module_info #from define_check_calling import check_calling_get_module_info #assert check_calling_get_module_info(level=2) == "test_import_package" def test_basic_stuff(): # This string is set in the package's __init__ two dirs up. # assert package_dir.test_string == "package_init" var = 5 locals_to_globals(clear=True)
break things. """ from __future__ import print_function, division, absolute_import import pytest_helper from pytest_helper import PytestHelperException, LocalsToGlobalsError pytest_helper.init() import os old_cwd = os.getcwd() os.chdir("..") # Causes an error if init() is not called above. pytest_helper.script_run(self_test=True, pytest_args="-v -s") # More efficient to put below two lines after script_run (won't run twice). pytest_helper.sys_path("../../../") # contains package_dir, to import it pytest_helper.autoimport() os.chdir(old_cwd) # Return to prev dir so as not to mess up later tests. # Test the pytest_helper routine get_calling_module_info #from define_check_calling import check_calling_get_module_info #assert check_calling_get_module_info(level=2) == "test_import_package" def test_basic_stuff(): # This string is set in the package's __init__ two dirs up. # assert package_dir.test_string == "package_init" var = 5 locals_to_globals(clear=True) def test_look_at_global():