Beispiel #1
0
 def test_function_list(self):
     """List of test functions.
     Test functions are functions defined inside this test file that start with 'test'
     """
     ast_module_node = parsing_utils.ast_parse_file(self.path)
     local_function_names = parsing_utils.top_level_functions(
         ast_module_node)
     return [f for f in local_function_names if f.startswith('test')]
Beispiel #2
0
 def custom_browsers(self):
     browser_functions = []
     path = os.path.join(self.path, 'browsers.py')
     if os.path.isfile(path):
         try:
             ast_module_node = parsing_utils.ast_parse_file(path)
             browser_functions = parsing_utils.top_level_functions(ast_module_node)
             browser_functions = [f for f in browser_functions if not f.startswith('_')]
         except Exception as e:
             print('There was an error reading browsers.py:\n' + traceback.format_exc())
     return browser_functions
Beispiel #3
0
    def test_top_level_functions(self, dir_function, test_utils):
        path = dir_function.path

        # empty file
        filepath = test_utils.create_file(path, 'test_one.py', content='')
        ast_node = parsing_utils.ast_parse_file(filepath)
        functions = parsing_utils.top_level_functions(ast_node)
        assert functions == []

        # a python module with local functions and imported functions
        content = ('from os import listdir\n'
                   'from sys import *\n'
                   'foo = 2\n'
                   'def f1():\n'
                   '    pass\n'
                   'def f2():\n'
                   '    pass')
        filepath = test_utils.create_file(path, 'test_two.py', content=content)
        ast_node = parsing_utils.ast_parse_file(filepath)
        functions = parsing_utils.top_level_functions(ast_node)
        assert functions == ['f1', 'f2']
Beispiel #4
0
    def test_top_level_assignments(self, dir_function, test_utils):
        path = dir_function.path

        # empty file
        filepath = test_utils.create_file(path, 'test_one.py', content='')
        ast_node = parsing_utils.ast_parse_file(filepath)
        assignments = parsing_utils.top_level_assignments(ast_node)
        assert assignments == []

        # a python module with local functions and imported functions
        content = ('from os import *\n'
                   'from sys import platform\n'
                   'foo = 2\n'
                   'def f1():\n'
                   '    pass\n'
                   'bar = (1, 2, 3)\n'
                   'for n in bar:\n'
                   '    print(x)\n'
                   'if True:\n'
                   '    baz = 3')
        filepath = test_utils.create_file(path, 'test_two.py', content=content)
        ast_node = parsing_utils.ast_parse_file(filepath)
        assignments = parsing_utils.top_level_assignments(ast_node)
        assert assignments == ['foo', 'bar']
Beispiel #5
0
 def test_hook_list(self):
     """List of test hook functions"""
     ast_module_node = parsing_utils.ast_parse_file(self.path)
     local_function_names = parsing_utils.top_level_functions(
         ast_module_node)
     test_hook_names = [
         'setup', 'teardown', 'before_test', 'before_each', 'after_each',
         'after_test'
     ]
     test_hooks = [f for f in local_function_names if f in test_hook_names]
     # TODO: setup / teardown are deprecated
     if 'setup' in test_hooks and 'before_test' in test_hooks:
         test_hooks.remove('setup')
     if 'teardown' in test_hooks and 'after_test' in test_hooks:
         test_hooks.remove('teardown')
     return test_hooks
Beispiel #6
0
 def test_ast_parse_file(self, dir_function, test_utils):
     filepath = test_utils.create_file(dir_function.path, 'test_file.py', content='foo = 2\n')
     ast_node = parsing_utils.ast_parse_file(filepath)
     assert isinstance(ast_node, ast.Module)