def __init__(self, file_name, klass=None, method=None): self.file_name = file_name # fetch assets from specific test using klass and method self.klass = klass # we need to make sure we cover the setUp method when fetching # assets for a specific test self.methods = [method, 'setUp'] self.asgmts = {} self.calls = [] # hold current class and current method to make sure we have the # correct context for the assignment statement. # at this time, module constants and class attributes are discarded. self.current_klass = None self.current_method = None # check if we have valid instrumented tests # discards disabled tests self.tests = safeloader.find_avocado_tests(self.file_name)[0] # create Abstract Syntax Tree from test source file with open(self.file_name, encoding='utf-8') as source_file: self.tree = ast.parse(source_file.read(), self.file_name) # build list of keyword arguments from calls that match pattern self.visit(self.tree)
def resolve(reference): if ':' in reference: module_path, _ = reference.split(':', 1) else: module_path = reference criteria_check = check_file(module_path, reference) if criteria_check is not True: return criteria_check # disabled tests not needed here class_methods_info, _ = find_avocado_tests(module_path) runnables = [] for klass, methods_tags in class_methods_info.items(): for (method, tags) in methods_tags: uri = "%s:%s.%s" % (module_path, klass, method) runnables.append( Runnable('avocado-instrumented', uri=uri, tags=tags)) if runnables: return ReferenceResolution(reference, ReferenceResolutionResult.SUCCESS, runnables) return ReferenceResolution(reference, ReferenceResolutionResult.NOTFOUND)
def test_non_relative_level0_from_level2(self): path = os.path.join(self.tmpdir.name, 'l1', 'l2', 'l2lib3.py') sys_path = sys.path + [self.tmpdir.name] with unittest.mock.patch('sys.path', sys_path): self.assertEqual( safeloader.find_avocado_tests(path)[0], {'BaseL2': [('test_l3', {}, []), ('test_l0', {}, [])]})
def resolve(reference): module_path, tests_filter = reference_split(reference) if tests_filter is not None: tests_filter = re.compile(tests_filter) criteria_check = check_file(module_path, reference) if criteria_check is not True: return criteria_check # disabled tests not needed here class_methods_info, _ = find_avocado_tests(module_path) runnables = [] for klass, methods_tags_reqs in class_methods_info.items(): for (method, tags, reqs) in methods_tags_reqs: klass_method = "%s.%s" % (klass, method) if tests_filter is not None: if not tests_filter.search(klass_method): continue uri = "%s:%s" % (module_path, klass_method) runnables.append( Runnable('avocado-instrumented', uri=uri, tags=tags, requirements=reqs)) if runnables: return ReferenceResolution(reference, ReferenceResolutionResult.SUCCESS, runnables) return ReferenceResolution(reference, ReferenceResolutionResult.NOTFOUND)
def test_import_not_on_parent(self): avocado_test = script.TemporaryScript('import_not_not_parent_test.py', IMPORT_NOT_NOT_PARENT_TEST) avocado_test.save() expected = ['test_something'] tests = safeloader.find_avocado_tests(avocado_test.path)[0] methods = [method[0] for method in tests['SomeClass']] self.assertEqual(expected, methods) avocado_test.remove()
def test_methods_order(self): avocado_keep_methods_order = script.TemporaryScript( 'keepmethodsorder.py', KEEP_METHODS_ORDER) avocado_keep_methods_order.save() expected_order = ['test2', 'testA', 'test1', 'testZZZ', 'test'] tests = safeloader.find_avocado_tests( avocado_keep_methods_order.path)[0] methods = [method[0] for method in tests['MyClass']] self.assertEqual(expected_order, methods) avocado_keep_methods_order.remove()
def test_methods_order(self): avocado_keep_methods_order = script.TemporaryScript( 'keepmethodsorder.py', KEEP_METHODS_ORDER) avocado_keep_methods_order.save() expected_order = ['test2', 'testA', 'test1', 'testZZZ', 'test'] tests = safeloader.find_avocado_tests(avocado_keep_methods_order.path)[0] methods = [method[0] for method in tests['MyClass']] self.assertEqual(expected_order, methods) avocado_keep_methods_order.remove()
def test_recursive_discovery(self): avocado_recursive_discovery_test1 = script.TemporaryScript( 'recursive_discovery_test1.py', RECURSIVE_DISCOVERY_TEST1) avocado_recursive_discovery_test1.save() avocado_recursive_discovery_test2 = script.TemporaryScript( 'recursive_discovery_test2.py', RECURSIVE_DISCOVERY_TEST2) avocado_recursive_discovery_test2.save() sys.path.append(os.path.dirname( avocado_recursive_discovery_test1.path)) tests = safeloader.find_avocado_tests( avocado_recursive_discovery_test2.path)[0] expected = { 'ThirdChild': [('test_third_child', {}), ('test_second_child', {}), ('test_first_child', {}), ('test_basic', {})] } self.assertEqual(expected, tests)
def test_recursive_discovery(self): avocado_recursive_discovery_test1 = script.TemporaryScript( 'recursive_discovery_test1.py', RECURSIVE_DISCOVERY_TEST1) avocado_recursive_discovery_test1.save() avocado_recursive_discovery_test2 = script.TemporaryScript( 'recursive_discovery_test2.py', RECURSIVE_DISCOVERY_TEST2) avocado_recursive_discovery_test2.save() sys.path.append(os.path.dirname(avocado_recursive_discovery_test1.path)) tests = safeloader.find_avocado_tests(avocado_recursive_discovery_test2.path)[0] expected = {'ThirdChild': [('test_third_child', {}), ('test_second_child', {}), ('test_first_child', {}), ('test_basic', {})]} self.assertEqual(expected, tests)
def _make_python_file_tests(self, test_path, make_broken, subtests_filter, test_name=None): if test_name is None: test_name = test_path try: # Avocado tests avocado_tests, _ = safeloader.find_avocado_tests(test_path) if avocado_tests: test_factories = [] for test_class, info in avocado_tests.items(): if isinstance(test_class, str): for test_method, tags, _ in info: name = test_name + \ ':%s.%s' % (test_class, test_method) if (subtests_filter and not subtests_filter.search(name)): continue tst = (test_class, { 'name': name, 'modulePath': test_path, 'methodName': test_method, 'tags': tags }) test_factories.append(tst) return test_factories else: # Module does not have an avocado test or pyunittest class inside, # but maybe it's a Python executable. return self._make_simple_or_broken_test( test_path, subtests_filter, make_broken) # Since a lot of things can happen here, the broad exception is # justified. The user will get it unadulterated anyway, and avocado # will not crash. Ugly python files can raise any exception except BaseException as details: # pylint: disable=W0703 if isinstance(details, KeyboardInterrupt): raise # Don't ignore ctrl+c else: return self._make_simple_or_broken_test( test_path, subtests_filter, make_broken)
def __init__(self, file_name): self.file_name = file_name self.asgmts = {} self.calls = [] # hold current class and current method to make sure we have the # correct context for the assignment statement. # at this time, module constants and class attributes are discarded. self.current_klass = None self.current_method = None # check if we have valid instrumented tests # discards disabled tests self.tests = safeloader.find_avocado_tests(self.file_name)[0] # create Abstract Syntax Tree from test source file with open(self.file_name) as source_file: self.tree = ast.parse(source_file.read(), self.file_name) # build list of keyword arguments from calls that match pattern self.visit(self.tree)
def _make_python_file_tests( self, test_path, make_broken, subtests_filter, test_name=None ): if test_name is None: test_name = test_path try: # Avocado tests avocado_tests, _ = safeloader.find_avocado_tests(test_path) if avocado_tests: test_factories = [] for test_class, info in avocado_tests.items(): if isinstance(test_class, str): for test_method, tags, _ in info: name = test_name + f":{test_class}.{test_method}" if subtests_filter and not subtests_filter.search(name): continue tst = ( test_class, { "name": name, "modulePath": test_path, "methodName": test_method, "tags": tags, }, ) test_factories.append(tst) return test_factories else: return make_broken(NotATest, test_path, self.NOT_TEST_STR) # Since a lot of things can happen here, the broad exception is # justified. The user will get it unadulterated anyway, and avocado # will not crash. Ugly python files can raise any exception except BaseException as details: # pylint: disable=W0703 if isinstance(details, KeyboardInterrupt): raise # Don't ignore ctrl+c else: return make_broken(NotATest, test_path, self.NOT_TEST_STR)
def test_relative_level0_from_level2(self): path = os.path.join(self.tmpdir.name, 'l1', 'l2', 'l2lib2.py') self.assertEqual( safeloader.find_avocado_tests(path)[0], {'BaseL2': [('test_l2', {}, []), ('test_l0', {}, [])]})
def test_base_level0(self): path = os.path.join(self.tmpdir.name, 'l0lib.py') self.assertEqual( safeloader.find_avocado_tests(path)[0], {'BaseL0': [('test_l0', {}, [])]})