def test_should_return_true_when_command_is_executable(self): when(execution).check_call(ANY_ARGUMENTS).then_return(None) actual_result = check_if_is_executable('command', '--version', '--quiet') self.assertTrue(actual_result) verify(execution).check_call(['command', '--version', '--quiet'])
def test_should_return_false_when_command_is_not_executable(self): when(execution).check_call(ANY_ARGUMENTS).then_raise(subprocess.CalledProcessError(127, 'command')) actual_result = check_if_is_executable('command', '--version', '--quiet') self.assertFalse(actual_result) verify(execution).check_call(['command', '--version', '--quiet'])
def test_should_return_value_of_check(self): when(git).check_if_is_executable(ANY_ARGUMENTS).then_return('value from check') actual_return_value = self.git_client.is_executable() self.assertEqual('value from check', actual_return_value) verify(git).check_if_is_executable('git', '--version')
def test_return_true_if_dot_git_directory_exists(self): when(git.path).isdir(ANY_ARGUMENTS).then_return(True) actual_return_value = self.git_client.detect() self.assertEqual(True, actual_return_value) verify(git.path).isdir('.git')
def test_should_prepend_git_to_given_arguments(self): when(self.git_client)._git(ANY_ARGUMENTS).then_return(None) self.git_client.commit('This is a commit message.') verify(self.git_client)._git('commit', '-a', '-m', 'This is a commit message.') verify(self.git_client)._git('push')
def test_should_return_false_when_trying_to_execute_command_fails(self): when(execution).check_call(ANY_ARGUMENTS).then_raise(OSError()) actual_result = check_if_is_executable('command', '--version', '--quiet') self.assertFalse(actual_result) verify(execution).check_call(['command', '--version', '--quiet'])
def test_should_return_value_of_check(self): when(subversion).check_if_is_executable('svn', '--version', '--quiet').then_return('value from check') actual_return_value = self.subversion_client.is_executable() self.assertEqual('value from check', actual_return_value) verify(subversion).check_if_is_executable('svn', '--version', '--quiet')
def test_matching_a_keyword_argument(self): when(targetpackage).call(ANY_LIST, ANY_VALUE).then_return(None) call_a_subprocess() verify(targetpackage).call(['pip'], stderr=ANY_VALUE)
def test_should_allow_matcher_in_keyword_argument_of_patched_function(self): when(targetpackage).call(ANY_LIST, ANY_VALUE).then_return('Hello world') call_a_subprocess() verify(targetpackage).call(['pip'], stderr=ANY_VALUE)
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_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_configured_value(self): when(targetpackage).targetfunction(2).then_return(3) self.assertEqual(targetpackage.targetfunction(2), 3) verify(targetpackage).targetfunction(2)
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_return_false_if_dot_svn_directory_does_not_exist(self): when(subversion).isdir('.svn').then_return(False) actual_return_value = self.subversion_client.detect() self.assertEqual(False, actual_return_value) verify(subversion).isdir('.svn')
def test_should_verify_never_called_a_field_of_a_mock_without_any_arguments(self): test_object = Mock(targetpackage.TheClass()) test_object.some_method(1, 2, 3) verify(test_object, NEVER).some_method()
def test_should_verify_a_simple_call(self): when(targetpackage).targetfunction().then_return('123') targetpackage.targetfunction() verify(targetpackage).targetfunction()
def test_should_verify_a_call_to_a_field_of_a_mock_without_any_arguments(self): test_object = Mock(targetpackage.TheClass()) test_object.some_method() verify(test_object).some_method()
def test_should_verify_a_call_to_a_field_of_a_mock_with_arguments_and_keyword_arguments(self): test_object = Mock(targetpackage.TheClass()) test_object.some_method(1, 2, 3, hello='world') verify(test_object).some_method(1, 2, 3, hello='world')
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_value_of_check(self): when(mercurial).check_if_is_executable(ANY_ARGUMENTS).then_return('value from check') actual_return_value = self.mercurial_client.is_executable() self.assertEqual('value from check', actual_return_value) verify(mercurial).check_if_is_executable('hg', '--version', '--quiet')
def test_should_call_pull_and_update(self): when(self.mercurial_client)._hg(ANY_ARGUMENTS).then_return(None) self.mercurial_client.update() verify(self.mercurial_client)._hg('pull') verify(self.mercurial_client)._hg('update')
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_when_addressing_using_strings(self): when('targetpackage').targetfunction().then_return('123') targetpackage.targetfunction() verify('targetpackage').targetfunction()
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_return_true_if_dot_svn_directory_exists(self): when(subversion).isdir('.svn').then_return(True) actual_return_value = self.subversion_client.detect() self.assertEqual(True, actual_return_value) verify(subversion).isdir('.svn')
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_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_error_when_function_patched_and_not_called_without_arguments( self): mock_object = Mock() when(mock_object).warn(1, 2).then_return('123') mock_object.warn('spam', 2, 1, 'eggs', False) mock_object.warn('abc', 123, True) mock_object.warn('eggs', False) exception_raised = False try: verify(mock_object).warn() except VerificationError as error: exception_raised = True assert_that( str(error), equal_to(""" Expected: call mock.Mock.warn() << at least once >> but was: call mock.Mock.warn('spam', 2, 1, 'eggs', False) call mock.Mock.warn('abc', 123, True) call mock.Mock.warn('eggs', False) """)) assert_that(exception_raised)
def test_should_raise_exception_when_trying_to_use_matcher_as_second_keyword_argument_in_native_verification( self): test_object = Mock(targetpackage.TheClass()) test_object.some_method(1, 2, 3, hello='world') exception_raised = False try: verify(test_object).some_method(1, 2, 3, hello='world', world=ANY_VALUE) except VerificationError as error: exception_raised = True assert_that( str(error), ends_with(""" Please configure your mock using fluentmock.when in order to be able to use matchers! """)) assert_that(exception_raised)
def test_should_verify_a_call_to_a_field_of_a_mock_with_arguments(self): test_object = Mock(targetpackage.TheClass()) test_object.some_method(1, 2, 3) verify(test_object).some_method(1, 2, 3)
def test_should_return_project_property_when_property_is_defined(self): project_mock = Mock(Project) project_mock.name = "my name" self.assertEquals("my name", ProjectDictWrapper(project_mock, Mock())["name"]) verify(project_mock, NEVER).get_property("name", "name")
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_never_called_a_field_of_a_mock_without_any_arguments( self): test_object = Mock(targetpackage.TheClass()) test_object.some_method(1, 2, 3) verify(test_object, NEVER).some_method()
def test_should_prepend_hg_to_given_arguments(self): when(self.mercurial_client)._hg(ANY_VALUES).then_return(None) when(self.mercurial_client)._hg(ANY_VALUES).then_return(None) self.mercurial_client.commit('This is a commit message.') verify(self.mercurial_client)._hg('commit', '-m', 'This is a commit message.') verify(self.mercurial_client)._hg('push')
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_not_log_stdout_when_stdout_is_empty_string(self): stdout = '' when(self.process_mock).communicate().then_return((stdout, 'stderr')) when(execution).Popen(ANY_ARGUMENTS).then_return(self.process_mock) execute_command('command', '1', '2', '3') verify(execution.LOGGER, NEVER).info(stdout)
def test_should_delegate_to_project_get_property_when_attribute_is_not_defined(self): project_mock = Project(".") when(project_mock).has_property("spam").then_return(True) when(project_mock).get_property("spam").then_return("eggs") self.assertEquals("eggs", ProjectDictWrapper(project_mock, Mock())["spam"]) verify(project_mock).get_property("spam")
def test_should_verify_a_call_to_a_object_with_multiple_arguments(self): test_object = targetpackage.TheClass() when(test_object).some_method(1, 2).then_return('123') test_object.some_method(1, 2) verify(test_object).some_method(1, 2)
def test_should_verify_a_call_to_a_field_of_a_mock_with_any_argument(self): test_object = Mock(targetpackage.TheClass()) when(test_object).some_method(1).then_return(0) assert_that(test_object.some_method(1), equal_to(0)) verify(test_object).some_method(ANY_VALUE)
def test_should_allow_matcher_in_keyword_argument_of_patched_function( self): when(targetpackage).call(ANY_LIST, ANY_VALUE).then_return('Hello world') call_a_subprocess() verify(targetpackage).call(['pip'], stderr=ANY_VALUE)
def test_should_verify_that_mock_has_been_called_exactly_once(self): test_object = Mock(targetpackage.TheClass()) when(test_object).some_method(1).then_return(0) assert_that(test_object.some_method(1), equal_to(0)) verify(test_object, times=1).some_method(1)
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_delegate_to_project_get_property_when_attribute_is_not_defined( self): project_mock = Project(".") when(project_mock).has_property("spam").then_return(True) when(project_mock).get_property("spam").then_return("eggs") self.assertEquals("eggs", ProjectDictWrapper(project_mock, Mock())["spam"]) verify(project_mock).get_property("spam")
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_warn_when_substitution_is_skipped(self): project_mock = Project(".") logger_mock = Mock() when(project_mock).has_property("n/a").then_return(False) self.assertEquals("${n/a}", ProjectDictWrapper(project_mock, logger_mock)["n/a"]) verify(project_mock, NEVER).get_property("n/a") verify(logger_mock).warn( "Skipping impossible substitution for 'n/a' - there is no matching project attribute or property." )
def test_should_list_actual_calls(self): mock_object = Mock() mock_object.warn('hello world') exception_raised = False try: verify(mock_object).warn() except VerificationError as error: exception_raised = True assert_that( str(error), equal_to(""" Expected: call mock.Mock.warn() << at least once >> but was: call mock.Mock.warn('hello world') """)) assert_that(exception_raised)
def test_should_raise_error_with_a_detailed_message_when_function_patched_and_not_called( self): when(targetpackage).targetfunction().then_return('123') exception_raised = False try: verify(targetpackage).targetfunction() except VerificationError as error: exception_raised = True assert_that( str(error), equal_to(""" Expected: call targetpackage.targetfunction() << at least once >> Reason: No patched function has been called. """)) 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_not_verify_a_call_when_no_function_has_been_called(self): when(targetpackage).targetfunction().then_return('123') exception_raised = False try: verify(targetpackage).targetfunction(1, 2, 3, hello='foobar') except VerificationError as error: exception_raised = True assert_that( str(error), equal_to(""" Expected: call targetpackage.targetfunction(1, 2, 3, hello='foobar') << at least once >> Reason: No patched function has been called. """)) assert_that(exception_raised)