def test_timeline_for_user_object(execution_events, user_object): """Construct a new timeline for a test case based on real execution timeline and a user object that needs to be tested. The new timeline in most cases will contain assertions. """ init_call = user_object.get_init_call() external_calls = testable_calls(user_object.get_external_calls()) # If the constructor raised an exception, object creation should be an assertion. if init_call and init_call.raised_exception(): call_return_timestamp = last_call_action_timestamp(init_call) return [ RaisesAssertionLine(init_call.exception, MethodCallContext(init_call, user_object), call_return_timestamp + 0.25) ] timeline = give_context_to_method_calls( compact([init_call]) + flatten([ test_timeline_for_call(execution_events, call) for call in external_calls ]), user_object) if init_call and len(external_calls) == 0: timeline.append( CommentLine("# Make sure it doesn't raise any exceptions.", timeline[-1].timestamp)) return timeline
def test_timeline_for_user_object(execution_events, user_object): """Construct a new timeline for a test case based on real execution timeline and a user object that needs to be tested. The new timeline in most cases will contain assertions. """ init_call = user_object.get_init_call() external_calls = testable_calls(user_object.get_external_calls()) # If the constructor raised an exception, object creation should be an assertion. if init_call and init_call.raised_exception(): call_return_timestamp = last_call_action_timestamp(init_call) return [ RaisesAssertionLine( init_call.exception, MethodCallContext(init_call, user_object), call_return_timestamp + 0.25 ) ] timeline = give_context_to_method_calls( compact([init_call]) + flatten(map(lambda call: test_timeline_for_call(execution_events, call), external_calls)), user_object, ) if init_call and len(external_calls) == 0: timeline.append(CommentLine("# Make sure it doesn't raise any exceptions.", timeline[-1].timestamp)) return timeline
def userobject2testname(user_object): init_call = user_object.get_init_call() external_calls = testable_calls(user_object.get_external_calls()) if len(external_calls) == 0 and init_call: test_name = initcall2testname(init_call) else: if len(external_calls) == 1: call = external_calls[0] test_name = call2testname(call, call.definition.name) # Methods with more than one external call use more brief # descriptions that don't include inputs and outputs. else: methods = [] for method, calls_count in counted( [call.definition.name for call in external_calls]): if calls_count == 1: methods.append(method) else: methods.append("%s_%d_times" % (method, calls_count)) test_name = "test_%s" % '_and_'.join(methods) if init_call and init_call.input: test_name += "_after_creation_with_%s" % arguments_as_string( init_call.input) return test_name
def _generate_test_method_descriptions_for_function(self, function, module): if testable_calls(function.calls): log.debug("Detected %s in function %s." % \ (pluralize("testable call", len(testable_calls(function.calls))), function.name)) # We're calling the function, so we have to make sure it will # be imported in the test self.ensure_import((module.locator, function.name)) # We have at least one call registered, so use it. return self._method_descriptions_from_function(function) else: # No calls were traced, so we'll go for a single test stub. log.debug("Detected _no_ testable calls in function %s." % function.name) name = name2testname(underscore(function.name)) return [TestMethodDescription(name, generate_test_case(function, self.template))]
def userobject2testname(user_object): init_call = user_object.get_init_call() external_calls = testable_calls(user_object.get_external_calls()) if len(external_calls) == 0 and init_call: test_name = initcall2testname(init_call) else: if len(external_calls) == 1: call = external_calls[0] test_name = call2testname(call, call.definition.name) # Methods with more than one external call use more brief # descriptions that don't include inputs and outputs. else: methods = [] for method, calls_count in counted([call.definition.name for call in external_calls]): if calls_count == 1: methods.append(method) else: methods.append("%s_%d_times" % (method, calls_count)) test_name = "test_%s" % '_and_'.join(methods) if init_call and init_call.input: test_name += "_after_creation_with_%s" % arguments_as_string(init_call.input) return test_name
def _method_descriptions_from_function(self, function): for call in testable_calls(function.get_unique_calls()): name = call2testname(call, function.name) yield TestMethodDescription(name, generate_test_case(call, self.template))