#!/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
import pytest_helper # Regular program code goes here. testing_var = "foo" # Test running below, but only when the module is invoked as a script. if __name__ == "__main__": # This guard conditional is optional. pytest_helper.script_run(self_test=True, pytest_args="-v") pytest_helper.autoimport() # Do some basic imports automatically. def my_setup(): # Could be a pytest fixture instead of a regular function. setup_var = "bar" locals_to_globals( clear=True) # Copies setup_var to the module's global namespace. def test_var_values(): my_setup() assert testing_var == "foo" # Set in the regular code above. assert setup_var == "bar" # Read from the global namespace. test_dict = {} with raises(KeyError): # Pytest function raises was autoimported. test_dict[5]
""" """ from __future__ import print_function, division, absolute_import import pytest_helper testing_var = "foo" if __name__ == "__main__": pytest_helper.script_run(self_test=True, pytest_args="-v") pytest_helper.autoimport() def test_config_values(): assert testing_var == "foo" locals_to_globals() with raises(NameError): clear_locals_from_globals() # This is on the autoimport skip list in ini file.
import os import inspect import pytest_helper pytest_helper.init(set_package=True) # This import *needs* the init with the set_package above in order to work. # Would NOT be needed, though, with script_run run from top of the file # as recommended instead of after the import. from . import dummy_module # This line is all this module really does; obviously a real, non-test module # would do more. test_string = "in_same_dir" ## ## Run tests. ## # Below imports both work, but commented out to not confound tests. #import package_dir.test_in_same_dir #from . import test_in_same_dir pytest_helper.script_run("test_in_same_dir", pytest_args="-v", exit=False) pytest_helper.script_run("test_in_same_dir.py", pytest_args="-v", exit=False) # This sys.path call is needed to find the package root. #pytest_helper.sys_path("..") pytest_helper.script_run("package_dir.test_in_same_dir", pytest_args="-v", pyargs=True)
## ## Run tests. ## # Everything below can optionally be placed in an "if __name__ == '__main__'" # conditional. The call to script_run can optionally be placed near the top # of the file, to avoid running the module initialization twice (for modules # where that really matters). if __name__ == "__main__": # Get this file's absolute dir path to test passing absolute pathnames in calls # to pytest_helper. this_files_dir = os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda: 0))) path_to_second_test = os.path.join(this_files_dir, "test/test_second_in_child.py") # Use both relative and absolute pathnames in specifying the test file paths. # Also include the full directory in the list to check that that works. pytest_helper.script_run( testfile_paths=["test/test_in_child_dir.py", path_to_second_test, "test"], pytest_args="-v", exit=False ) # Try running as a package module. Note it can start at package_subdir. # Doesn't need the path modification. # pytest_helper.sys_path("..") pytest_helper.script_run(testfile_paths="package_subdir.test_in_package_subdir", pytest_args="-v", pyargs=True) # Import works fine here; only called when imported. from .dummy_module import test_string as dummy_test_string
from __future__ import print_function, division, absolute_import import os import inspect import pytest_helper pytest_helper.init(set_package=True) # This import *needs* the init with the set_package above in order to work. # Would NOT be needed, though, with script_run run from top of the file # as recommended instead of after the import. from . import dummy_module # This line is all this module really does; obviously a real, non-test module # would do more. test_string = "in_same_dir" ## ## Run tests. ## # Below imports both work, but commented out to not confound tests. #import package_dir.test_in_same_dir #from . import test_in_same_dir pytest_helper.script_run("test_in_same_dir", pytest_args="-v", exit=False) pytest_helper.script_run("test_in_same_dir.py", pytest_args="-v", exit=False) # This sys.path call is needed to find the package root. #pytest_helper.sys_path("..") pytest_helper.script_run("package_dir.test_in_same_dir", pytest_args="-v", pyargs=True)
# This line is all this module really does; obviously a real, non-test module # would do more. test_string = "in_child_dir" #import set_package_attribute # Test setting the package attribute for scripts. from . import subdir_dummy_module # Works! now this doesn't give an error. from .. import dummy_module # Works! now this doesn't give an error. import sys sys.exit(0) # TODO currently only testing relative imports... ## ## Run tests. ## # Everything below can optionally be placed in an "if __name__ == '__main__'" # conditional. The call to script_run can optionally be placed near the top # of the file, to avoid running the module initialization twice (for modules # where that really matters). # Get this file's absolute dir path to test passing absolute pathnames in calls # to pytest_helper. this_files_dir = os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda:0))) path_to_second_test = os.path.join(this_files_dir, "test/second_test.py") # Use both relative and absolute pathnames in specifying the test file paths. # Also include the full directory in the list to check that that works. pytest_helper.script_run(testfile_paths=["test/test_in_child_dir.py", path_to_second_test, "test"], pytest_args="-v")
# -*- coding: utf-8 -*- """ These are tests of `locals_to_globals` and older tests of the techniques used in pytest helper. There are also a couple of early experimental approaches. Mostly stuff related to locals_to_globals and alternatives. """ from __future__ import print_function, division, absolute_import import sys import pytest_helper pytest_helper.script_run(self_test=True) pytest_helper.autoimport() #from py.test import raises, fail, fixture, skip # Done by autoimport. def dummy_fun(): pass # just to test function objects @fixture def set_up_test_copy_to_globals(request): """ """ xx = 100 x_str = "string in setup" monkey = 900 df = dummy_fun
#!/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()
#!/usr/bin/env python # -*- coding: utf-8 -*- 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.
""" from __future__ import print_function, division, absolute_import import os import inspect import pytest_helper # This line is all this module really does; obviously a real, non-test module # would do more. test_string = "in_sibling_dir" ## ## Run tests. ## # Everything below can optionally be placed in an "if __name__ == '__main__'" # conditional. The call to script_run can optionally be placed near the top # of the file, to avoid running the module initialization twice (for modules # where that really matters). # Get this file's absolute dir path to test passing absolute pathnames in calls # to pytest_helper. this_files_dir = os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda:0))) path_to_second_test = os.path.join(this_files_dir, "../test/test_second_in_sibling.py") # Use both relative and absolute pathnames in specifying the test file paths. # Also include the full directory in the list to check that that works. pytest_helper.script_run(testfile_paths=["../test", "../test/test_in_sibling_dir.py", path_to_second_test, "../test"], pytest_args="-v")
from __future__ import print_function, absolute_import, division if __name__ == "__main__": import pytest_helper pytest_helper.script_run("test_intro_example.py", pytest_args="-v")
# conditional. The call to script_run can optionally be placed near the top # of the file, to avoid running the module initialization twice (for modules # where that really matters). if __name__ == "__main__": # Get this file's absolute dir path to test passing absolute pathnames in calls # to pytest_helper. this_files_dir = os.path.dirname( os.path.abspath(inspect.getsourcefile(lambda: 0))) path_to_second_test = os.path.join(this_files_dir, "test/test_second_in_child.py") # Use both relative and absolute pathnames in specifying the test file paths. # Also include the full directory in the list to check that that works. pytest_helper.script_run(testfile_paths=[ "test/test_in_child_dir.py", path_to_second_test, "test" ], pytest_args="-v", exit=False) # Try running as a package module. Note it can start at package_subdir. # Doesn't need the path modification. #pytest_helper.sys_path("..") pytest_helper.script_run( testfile_paths="package_subdir.test_in_package_subdir", pytest_args="-v", pyargs=True) # Import works fine here; only called when imported. from .dummy_module import test_string as dummy_test_string
import pytest_helper if __name__ == "__main__": # This guard conditional is optional. pytest_helper.script_run(".", pytest_args="-v")