提交 3c4c86ce 编写于 作者: L Lucas Meneghel Rodrigues

Merge pull request #936 from clebergnu/fix-local-import-tests

avocado.core.loader: Append test directory to sys.path [v2]
...@@ -258,8 +258,13 @@ class TestLoaderProxy(object): ...@@ -258,8 +258,13 @@ class TestLoaderProxy(object):
if isinstance(test_class, str): if isinstance(test_class, str):
module_name = os.path.basename(test_path).split('.')[0] module_name = os.path.basename(test_path).split('.')[0]
test_module_dir = os.path.dirname(test_path) test_module_dir = os.path.dirname(test_path)
f, p, d = imp.find_module(module_name, [test_module_dir]) # Tests with local dir imports need this
test_module = imp.load_module(module_name, f, p, d) 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): for _, obj in inspect.getmembers(test_module):
if (inspect.isclass(obj) and obj.__name__ == test_class and if (inspect.isclass(obj) and obj.__name__ == test_class and
inspect.getmodule(obj) == test_module): inspect.getmodule(obj) == test_module):
...@@ -267,6 +272,7 @@ class TestLoaderProxy(object): ...@@ -267,6 +272,7 @@ class TestLoaderProxy(object):
test_class = obj test_class = obj
break break
test_instance = test_class(**test_parameters) test_instance = test_class(**test_parameters)
return test_instance return test_instance
......
...@@ -56,6 +56,20 @@ class HelloWorld(Plugin): ...@@ -56,6 +56,20 @@ class HelloWorld(Plugin):
print('Hello World!') 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): class RunnerOperationTest(unittest.TestCase):
...@@ -84,6 +98,21 @@ class RunnerOperationTest(unittest.TestCase): ...@@ -84,6 +98,21 @@ class RunnerOperationTest(unittest.TestCase):
"examples/tests/passtest.py" % self.tmpdir) "examples/tests/passtest.py" % self.tmpdir)
process.run(cmd_line) 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): def test_runner_tests_fail(self):
os.chdir(basedir) os.chdir(basedir)
cmd_line = './scripts/avocado run --sysinfo=off --job-results-dir %s passtest failtest passtest' % self.tmpdir cmd_line = './scripts/avocado run --sysinfo=off --job-results-dir %s passtest failtest passtest' % self.tmpdir
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册