diff --git a/avocado/core/loader.py b/avocado/core/loader.py index cc0c3cb88e903503a15baadd9049cc2fe13877b0..5771f3fa4c61d933ee403ee5fbaf8d76b0925e14 100644 --- a/avocado/core/loader.py +++ b/avocado/core/loader.py @@ -258,8 +258,13 @@ class TestLoaderProxy(object): if isinstance(test_class, str): module_name = os.path.basename(test_path).split('.')[0] test_module_dir = os.path.dirname(test_path) - f, p, d = imp.find_module(module_name, [test_module_dir]) - test_module = imp.load_module(module_name, f, p, d) + # Tests with local dir imports need this + try: + sys.path.insert(0, test_module_dir) + f, p, d = imp.find_module(module_name, [test_module_dir]) + test_module = imp.load_module(module_name, f, p, d) + finally: + sys.path.pop(0) for _, obj in inspect.getmembers(test_module): if (inspect.isclass(obj) and obj.__name__ == test_class and inspect.getmodule(obj) == test_module): @@ -267,6 +272,7 @@ class TestLoaderProxy(object): test_class = obj break test_instance = test_class(**test_parameters) + return test_instance diff --git a/selftests/functional/test_basic.py b/selftests/functional/test_basic.py index 27d8dd0ca410925b1c07e774cdd16c9657058c07..43cb6c074be70b32d714d68b522350adbccd13f6 100644 --- a/selftests/functional/test_basic.py +++ b/selftests/functional/test_basic.py @@ -56,6 +56,20 @@ class HelloWorld(Plugin): print('Hello World!') """ +HELLO_LIB_CONTENTS = """ +def hello(): + return 'Hello world' +""" + +LOCAL_IMPORT_TEST_CONTENTS = ''' +from avocado import Test +from mylib import hello + +class LocalImportTest(Test): + def test(self): + self.log.info(hello()) +''' + class RunnerOperationTest(unittest.TestCase): @@ -84,6 +98,21 @@ class RunnerOperationTest(unittest.TestCase): "examples/tests/passtest.py" % self.tmpdir) process.run(cmd_line) + def test_runner_test_with_local_imports(self): + mylib = script.TemporaryScript( + 'mylib.py', + HELLO_LIB_CONTENTS, + 'avocado_simpletest_functional') + mylib.save() + mytest = script.Script( + os.path.join(os.path.dirname(mylib.path), 'test_local_imports.py'), + LOCAL_IMPORT_TEST_CONTENTS) + os.chdir(basedir) + mytest.save() + cmd_line = ("./scripts/avocado run --sysinfo=off --job-results-dir %s " + "%s" % (self.tmpdir, mytest)) + process.run(cmd_line) + def test_runner_tests_fail(self): os.chdir(basedir) cmd_line = './scripts/avocado run --sysinfo=off --job-results-dir %s passtest failtest passtest' % self.tmpdir