def steps(self): steps = { 'setup': [], 'test': [], 'teardown': [] } setup_function = getattr(self.get_module(), 'setup', None) if setup_function: steps['setup'] = test_parser.parse_function_steps(setup_function) else: steps['setup'] = [] test_function = getattr(self.get_module(), 'test', None) if test_function: steps['test'] = test_parser.parse_function_steps(test_function) else: steps['test'] = [] teardown_function = getattr(self.get_module(), 'teardown', None) if teardown_function: steps['teardown'] = test_parser.parse_function_steps(teardown_function) else: steps['teardown'] = [] return steps
def test_parse_function_steps(self): def function(): print('foo') bar = False if bar: print('baz') # comment str('test').replace('s', 'x') steps = test_parser.parse_function_steps(function) expected_steps = [{ 'type': 'function-call', 'code': "print('foo')", 'function_name': 'print', 'parameters': ["'foo'"] }, { 'type': 'code-block', 'code': 'bar = False' }, { 'type': 'code-block', 'code': "if bar:\n print('baz')" }, { 'type': 'code-block', 'code': '# comment' }, { 'type': 'code-block', 'code': "str('test').replace('s', 'x')" }] assert steps == expected_steps
def test_functions(self): """Dictionary of parsed steps of each test function""" tests = {} test_function_list = self.test_function_list for test_function in test_function_list: function = getattr(self.get_module(), test_function) tests[test_function] = test_parser.parse_function_steps(function) return tests
def test_parse_function_pass(self): """Functions with only `pass` in the body return no steps""" def function1(): pass steps = test_parser.parse_function_steps(function1) assert steps == [] def function2(): pass steps = test_parser.parse_function_steps(function2) assert steps == [] def function3(): print('foo') pass steps = test_parser.parse_function_steps(function3) assert len(steps) == 2 assert steps[0]['function_name'] == 'print' assert steps[1]['code'] == 'pass'
def test_hooks(self): """Dictionary of parsed steps of each test hook function""" hooks = {} test_hooks = self.test_hook_list for hook_name in test_hooks: h = getattr(self.get_module(), hook_name) # TODO setup / teardown are deprecated, returned as before_test/after_test if hook_name == 'setup': hook_name = 'before_test' if hook_name == 'teardown': hook_name = 'after_test' hooks[hook_name] = test_parser.parse_function_steps(h) from collections import OrderedDict return OrderedDict(hooks)
def test_parse_function_nested(self): def function1(): print(['foo', 'bar']) bar = ([False, True], 0) steps = test_parser.parse_function_steps(function1) expected_steps = [{ 'type': 'function-call', 'code': "print([\n 'foo',\n 'bar'\n])", 'function_name': 'print', 'parameters': ["[ 'foo', 'bar']"] }, { 'type': 'code-block', 'code': 'bar = (\n [False, True],\n 0)' }] assert steps == expected_steps