def test_should_verify_that_function_has_not_been_called_when_function_has_been_called_with_other_arguments(self): when(targetpackage).targetfunction().then_return('123') targetpackage.targetfunction(1) verify(targetpackage, NEVER).targetfunction()
def test_should_verify_a_simple_call_when_addressing_using_strings(self): when('targetpackage').targetfunction().then_return('123') targetpackage.targetfunction() verify('targetpackage').targetfunction()
def test_should_verify_a_simple_call_using_a_keyword_argument(self): when(targetpackage).targetfunction(keyword_argument='foobar').then_return('123') targetpackage.targetfunction(keyword_argument='foobar') verify(targetpackage).targetfunction(keyword_argument='foobar')
def test_should_verify_that_target_has_been_called_exactly_once(self): when(targetpackage).targetfunction(ANY_VALUES).then_return(123) targetpackage.targetfunction("abc") verify(targetpackage, times=1).targetfunction("abc")
def test_should_return_specific_values_with_diff_args_and_same_kwargs(self): when(targetpackage).targetfunction(1, foo='bar').then_return('boo') when(targetpackage).targetfunction(2, foo='bar').then_return('zoo') assert_that(targetpackage.targetfunction(1, foo='bar'), equal_to('boo')) assert_that(targetpackage.targetfunction(2, foo='bar'), equal_to('zoo'))
def test_should_verify_a_call_with_multiple_arguments(self): when(targetpackage).targetfunction(1, 2).then_return('123') targetpackage.targetfunction(1, 2) verify(targetpackage).targetfunction(1, 2)
def test_should_verify_a_simple_call_with_a_argument(self): when(targetpackage).targetfunction(1).then_return('123') targetpackage.targetfunction(1) verify(targetpackage).targetfunction(1)
def test_should_verify_any_argument_twice(self): when(targetpackage).targetfunction(ANY_VALUES).then_return(1) targetpackage.targetfunction(2, 'abc') verify(targetpackage).targetfunction(ANY_VALUE, ANY_VALUE)
def test_should_verify_any_argument(self): when(targetpackage).targetfunction(ANY_VALUE).then_return(1) targetpackage.targetfunction(2) verify(targetpackage).targetfunction(ANY_VALUE)
def test_should_verify_a_simple_call(self): when(targetpackage).targetfunction().then_return('123') targetpackage.targetfunction() verify(targetpackage).targetfunction()
def test_should_return_two_as_second_answer_when_two_answers_are_configured(self): when(targetpackage).targetfunction().then_return(1).then_return(2) targetpackage.targetfunction() actual_value = targetpackage.targetfunction() assert_that(actual_value, equal_to(2))
def test_should_verify_a_simple_call_using_a_keyword_argument(self): when(targetpackage).targetfunction( keyword_argument='foobar').then_return('123') targetpackage.targetfunction(keyword_argument='foobar') verify(targetpackage).targetfunction(keyword_argument='foobar')
def test_should_return_zero_again_when_answer_zero_is_given(self): when(targetpackage).targetfunction().then_return(0) targetpackage.targetfunction() actual_value = targetpackage.targetfunction() assert_that(actual_value, equal_to(0))
def test_should_always_return_the_same_answer_for_any_argument(self): when(targetpackage).targetfunction(ANY_VALUES).then_return('foobar') assert_that(targetpackage.targetfunction(0), equal_to('foobar')) assert_that(targetpackage.targetfunction(1, 2, 3), equal_to('foobar')) assert_that(targetpackage.targetfunction(), equal_to('foobar')) assert_that(targetpackage.targetfunction('hello', 1), equal_to('foobar'))
def test_should_remove_predefined_answer_when_multiple_new_answers_given(self): when(targetpackage).targetfunction(1).then_return('Nope!') when(targetpackage).targetfunction(1).then_return('Yes.').then_return('Maybe!').then_return('No.') assert_that(targetpackage.targetfunction(1), equal_to('Yes.')) assert_that(targetpackage.targetfunction(1), equal_to('Maybe!')) assert_that(targetpackage.targetfunction(1), equal_to('No.'))
def test_should_verify_that_function_has_not_been_called_when_function_has_been_called_with_other_arguments( self): when(targetpackage).targetfunction().then_return('123') targetpackage.targetfunction(1) verify(targetpackage, NEVER).targetfunction()
def test_matching_any_string_that_contains_a_given_substring(self): when(targetpackage).targetfunction(contains('foo')).then_return('string contained "foo"') assert_that(targetpackage.targetfunction('foo bar'), equal_to('string contained "foo"')) assert_that(targetpackage.targetfunction('spam foo bar'), equal_to('string contained "foo"')) assert_that(targetpackage.targetfunction('superfoo'), equal_to('string contained "foo"')) assert_that(targetpackage.targetfunction('Hello'), equal_to(None)) assert_that(targetpackage.targetfunction('World'), equal_to(None))
def test_should_return_two_as_second_answer_when_two_answers_are_configured( self): when(targetpackage).targetfunction().then_return(1).then_return(2) targetpackage.targetfunction() actual_value = targetpackage.targetfunction() assert_that(actual_value, equal_to(2))
def test_should_return_configured_values_in_given_order(self): when(targetpackage).targetfunction(2).then_return(1).then_return(2).then_return(3) assert_that(targetpackage.targetfunction(2), equal_to(1)) assert_that(targetpackage.targetfunction(2), equal_to(2)) assert_that(targetpackage.targetfunction(2), equal_to(3)) verify(targetpackage).targetfunction(2)
def test_should_return_specific_values_with_diff_args_and_same_kwargs( self): when(targetpackage).targetfunction(1, foo='bar').then_return('boo') when(targetpackage).targetfunction(2, foo='bar').then_return('zoo') assert_that(targetpackage.targetfunction(1, foo='bar'), equal_to('boo')) assert_that(targetpackage.targetfunction(2, foo='bar'), equal_to('zoo'))
def test_should_return_configured_values_in_given_order(self): when(targetpackage).targetfunction(2).then_return(1).then_return( 2).then_return(3) assert_that(targetpackage.targetfunction(2), equal_to(1)) assert_that(targetpackage.targetfunction(2), equal_to(2)) assert_that(targetpackage.targetfunction(2), equal_to(3)) verify(targetpackage).targetfunction(2)
def test_should_return_specific_value_when_argument_fits_and_several_configurations_are_given(self): when(targetpackage).targetfunction(1).then_return(1) when(targetpackage).targetfunction(2).then_return(2) when(targetpackage).targetfunction(3).then_return(3) assert_that(targetpackage.targetfunction(3), equal_to(3)) assert_that(targetpackage.targetfunction(2), equal_to(2)) assert_that(targetpackage.targetfunction(1), equal_to(1)) assert_that(targetpackage.targetfunction(0), equal_to(None))
def test_should_return_specific_values_when_arguments_fit(self): when(targetpackage).targetfunction(2).then_return(2) when(targetpackage).targetfunction(1, 'spam').then_return(1) when(targetpackage).targetfunction(3, 'foo', True).then_return('bar') assert_that(targetpackage.targetfunction(1, 'spam'), equal_to(1)) assert_that(targetpackage.targetfunction(3, 'foo', True), equal_to('bar')) assert_that(targetpackage.targetfunction(2), equal_to(2)) assert_that(targetpackage.targetfunction(0), equal_to(None))
def test_should_remove_predefined_answer_when_multiple_new_answers_given( self): when(targetpackage).targetfunction(1).then_return('Nope!') when(targetpackage).targetfunction(1).then_return('Yes.').then_return( 'Maybe!').then_return('No.') assert_that(targetpackage.targetfunction(1), equal_to('Yes.')) assert_that(targetpackage.targetfunction(1), equal_to('Maybe!')) assert_that(targetpackage.targetfunction(1), equal_to('No.'))
def test_should_repeatedly_return_last_configured_value(self): when(targetpackage).targetfunction(2).then_return(1).then_return(5) targetpackage.targetfunction(2) assert_that(targetpackage.targetfunction(2), equal_to(5)) assert_that(targetpackage.targetfunction(2), equal_to(5)) assert_that(targetpackage.targetfunction(2), equal_to(5)) verify(targetpackage).targetfunction(2)
def test_should_return_specific_value_when_argument_fits_and_several_configurations_are_given( self): when(targetpackage).targetfunction(1).then_return(1) when(targetpackage).targetfunction(2).then_return(2) when(targetpackage).targetfunction(3).then_return(3) assert_that(targetpackage.targetfunction(3), equal_to(3)) assert_that(targetpackage.targetfunction(2), equal_to(2)) assert_that(targetpackage.targetfunction(1), equal_to(1)) assert_that(targetpackage.targetfunction(0), equal_to(None))
def test_should_raise_exception_when_configured_to_raise_exception(self): when(targetpackage).targetfunction(ANY_VALUES).then_raise(Exception('foobar')) exception_raised = False try: targetpackage.targetfunction() except Exception as exception: exception_raised = True assert_that(str(exception), equal_to('foobar')) assert_that(exception_raised)
def test_matching_any_string_that_contains_a_given_substring(self): when(targetpackage).targetfunction( contains('foo')).then_return('string contained "foo"') assert_that(targetpackage.targetfunction('foo bar'), equal_to('string contained "foo"')) assert_that(targetpackage.targetfunction('spam foo bar'), equal_to('string contained "foo"')) assert_that(targetpackage.targetfunction('superfoo'), equal_to('string contained "foo"')) assert_that(targetpackage.targetfunction('Hello'), equal_to(None)) assert_that(targetpackage.targetfunction('World'), equal_to(None))
def test_should_raise_exception_when_configured_to_raise_exception(self): when(targetpackage).targetfunction(ANY_VALUES).then_raise( Exception('foobar')) exception_raised = False try: targetpackage.targetfunction() except Exception as exception: exception_raised = True assert_that(str(exception), equal_to('foobar')) assert_that(exception_raised)
def test_should_raise_exception_when_times_is_not_instance_of_fluentmatcher_or_integer(self): when(targetpackage).targetfunction(ANY_VALUES).then_return(123) targetpackage.targetfunction("abc") exception_raised = False try: verify(targetpackage, times='123').targetfunction('abc') except ValueError as error: exception_raised = True assert_that(str(error), equal_to('Argument times has to be a instance of FluentMatcher')) assert_that(exception_raised)
def test_should_raise_error_when_function_not_called_with_expected_arguments_but_in_many_other_ways( self): when(targetpackage).targetfunction(1, 2).then_return('123') targetpackage.targetfunction('abc', 123, True) targetpackage.targetfunction('spam', 2, 1, 'eggs', False) targetpackage.targetfunction('eggs', False) targetpackage.targetfunction() exception_raised = False try: verify(targetpackage).targetfunction(1, 2) except VerificationError as error: exception_raised = True assert_that( str(error), equal_to(""" Expected: call targetpackage.targetfunction(1, 2) << at least once >> but was: call targetpackage.targetfunction('abc', 123, True) call targetpackage.targetfunction('spam', 2, 1, 'eggs', False) call targetpackage.targetfunction('eggs', False) call targetpackage.targetfunction() """)) assert_that(exception_raised)
def test_should_raise_exception_when_target_does_not_have_attribute(self): when(targetpackage).targetfunction().then_return('123') targetpackage.targetfunction() exception_raised = False try: verify(targetpackage).spameggs except InvalidAttributeError as error: exception_raised = True assert_that(str(error), equal_to('The target "targetpackage" has no attribute called "spameggs".')) assert_that(exception_raised)
def test_should_return_four_as_fifth_answer_when_four_answers_are_configured(self): when(targetpackage).targetfunction().then_return(1).then_return(2).then_return(3).then_return(4) targetpackage.targetfunction() targetpackage.targetfunction() targetpackage.targetfunction() targetpackage.targetfunction() actual_value = targetpackage.targetfunction() assert_that(actual_value, equal_to(4))
def test_should_return_configured_answer_when_addressing_target_using_string( self): when('targetpackage').targetfunction(ANY_VALUES).then_return('foobar') assert_that(targetpackage.targetfunction(keyword_argument='abc'), equal_to('foobar'))
def test_should_return_None_when_no_answer_is_configured(self): when(targetpackage).targetfunction() actual_value = targetpackage.targetfunction() assert_that(actual_value, equal_to(None))
def test_should_return_configured_value(self): when(targetpackage).targetfunction(2).then_return(3) self.assertEqual(targetpackage.targetfunction(2), 3) verify(targetpackage).targetfunction(2)
def test_should_raise_error_with_a_detailed_message_when_any_argument_does_not_match(self): when(targetpackage).targetfunction(ANY_VALUES).then_return('123') targetpackage.targetfunction(1, 2, 3) exception_raised = False try: verify(targetpackage).targetfunction(1, ANY_VALUE, 'c') except VerificationError as error: exception_raised = True assert_that(str(error), equal_to(""" Expected: call targetpackage.targetfunction(1, << ANY_VALUE >>, 'c') << at least once >> but was: call targetpackage.targetfunction(1, 2, 3) """)) assert_that(exception_raised)
def test_should_return_configured_answer_when_keyword_argument_given(self): when(targetpackage).targetfunction( keyword_argument='abc').then_return('foobar') assert_that(targetpackage.targetfunction(keyword_argument='abc'), equal_to('foobar'))
def test_should_return_none_when_configured_for_keyword_argument_but_not_called_with_it( self): when(targetpackage).targetfunction( keyword_argument='abc').then_return('foobar') assert_that(targetpackage.targetfunction(), equal_to(None))
def test_should_return_four_as_fifth_answer_when_four_answers_are_configured( self): when(targetpackage).targetfunction().then_return(1).then_return( 2).then_return(3).then_return(4) targetpackage.targetfunction() targetpackage.targetfunction() targetpackage.targetfunction() targetpackage.targetfunction() actual_value = targetpackage.targetfunction() assert_that(actual_value, equal_to(4))
def test_should_raise_exception_when_any_arguments_in_arguments_but_not_first(self): when(targetpackage).targetfunction(1).then_return('123') targetpackage.targetfunction(1) exception_raised = False try: verify(targetpackage).targetfunction(1, 2, 3, ANY_VALUES) except InvalidUseOfAnyValuesError as error: exception_raised = True assert_that(str(error), equal_to("""Do not use ANY_VALUES together with other arguments! Use ANY_VALUE as a wildcard for single arguments.""")) assert_that(exception_raised)
def test_should_raise_exception_when_called_once_but_expected_twice_with_any_arguments(self): when(targetpackage).targetfunction(ANY_VALUES).then_return(123) targetpackage.targetfunction("abc") exception_raised = False try: verify(targetpackage, times=2).targetfunction(ANY_VALUES) except VerificationError as error: exception_raised = True assert_that(str(error), equal_to(""" Expected: call targetpackage.targetfunction(<< ANY_VALUES >>) << exactly 2 times >> but was: call targetpackage.targetfunction('abc') """)) assert_that(exception_raised)
def test_should_raise_error_when_function_patched_and_not_called_with_expected_argument(self): when(targetpackage).targetfunction(1).then_return('123') targetpackage.targetfunction(2) exception_raised = False try: verify(targetpackage).targetfunction(1) except VerificationError as error: exception_raised = True assert_that(str(error), equal_to(""" Expected: call targetpackage.targetfunction(1) << at least once >> but was: call targetpackage.targetfunction(2) """)) assert_that(exception_raised)
def test_should_raise_error_when_function_called_but_it_should_not_have_been_called(self): when(targetpackage).targetfunction(1, 2, 3, test=1).then_return('123') targetpackage.targetfunction(1, 2, 3, test=1) exception_raised = False try: verify(targetpackage, NEVER).targetfunction(1, 2, 3, test=1) except VerificationError as error: exception_raised = True assert_that(str(error), equal_to(""" Expected: call targetpackage.targetfunction(1, 2, 3, test=1) << should never be called >> but was: call targetpackage.targetfunction(1, 2, 3, test=1) """)) assert_that(exception_raised)
def test_should_raise_exception_when_target_does_not_have_attribute(self): when(targetpackage).targetfunction().then_return('123') targetpackage.targetfunction() exception_raised = False try: verify(targetpackage).spameggs except InvalidAttributeError as error: exception_raised = True assert_that( str(error), equal_to( 'The target "targetpackage" has no attribute called "spameggs".' )) assert_that(exception_raised)
def test_should_raise_exception_when_times_is_not_instance_of_fluentmatcher_or_integer( self): when(targetpackage).targetfunction(ANY_VALUES).then_return(123) targetpackage.targetfunction("abc") exception_raised = False try: verify(targetpackage, times='123').targetfunction('abc') except ValueError as error: exception_raised = True assert_that( str(error), equal_to( 'Argument times has to be a instance of FluentMatcher')) assert_that(exception_raised)
def test_should_raise_error_with_a_detailed_message_when_any_argument_does_not_match( self): when(targetpackage).targetfunction(ANY_VALUES).then_return('123') targetpackage.targetfunction(1, 2, 3) exception_raised = False try: verify(targetpackage).targetfunction(1, ANY_VALUE, 'c') except VerificationError as error: exception_raised = True assert_that( str(error), equal_to(""" Expected: call targetpackage.targetfunction(1, << ANY_VALUE >>, 'c') << at least once >> but was: call targetpackage.targetfunction(1, 2, 3) """)) assert_that(exception_raised)