def test_should_return_one_as_first_answer_when_two_answers_are_configured(self): when(targetpackage).targetfunction().then_return(1).then_return(2) actual_value = targetpackage.targetfunction() assert_that(actual_value, equal_to(1))
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_zero_when_answer_zero_is_given(self): when(targetpackage).targetfunction().then_return(0) actual_value = targetpackage.targetfunction() assert_that(actual_value, equal_to(0))
def test_should_match_argument_when_using_any_argument(self): when(targetpackage.subpackage).subtargetfunction(ANY_VALUE).then_return('specific') assert_that(targetpackage.subpackage.subtargetfunction(1), equal_to('specific')) assert_that(targetpackage.subpackage.subtargetfunction(2), equal_to('specific')) assert_that(targetpackage.subpackage.subtargetfunction(3), equal_to('specific'))
def test_should_patch_away_method_of_mock(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))
def test_should_patch_away_class_method_for_old_style_classes(self): when(targetpackage.TheOldStyleClass).some_class_method(1).then_return(10) when(targetpackage.TheOldStyleClass).some_class_method(2).then_return(11) assert_that(targetpackage.TheOldStyleClass.some_class_method(1), equal_to(10)) assert_that(targetpackage.TheOldStyleClass.some_class_method(2), equal_to(11))
def test_should_undo_patching_of_function(self): when(targetpackage).patch_test_1().then_return('patched!') undo_patches() assert_that(targetpackage.patch_test_1(), equal_to('not patched 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_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_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(self): when(targetpackage).targetfunction(ANY_VALUE).then_return(1) targetpackage.targetfunction(2) verify(targetpackage).targetfunction(ANY_VALUE)
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_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_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_return_true_if_dot_hg_directory_exists(self): when(mercurial.path).isdir(ANY_ARGUMENTS).then_return(True) actual_return_value = self.mercurial_client.detect() self.assertEqual(True, actual_return_value) when(mercurial.path).isdir('.hg')
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_return_false_if_dot_hg_directory_does_not_exist(self): when(mercurial).isdir(ANY_VALUES).then_return(False) actual_return_value = self.mercurial_client.detect() self.assertEqual(False, actual_return_value) when(mercurial).isdir('.hg')
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 set_up(self): when(execution.LOGGER).info(ANY_ARGUMENTS).then_return(None) when(execution.LOGGER).error(ANY_ARGUMENTS).then_return(None) process_mock = Mock() process_mock.returncode = 0 self.process_mock = process_mock
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_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_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_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_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_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_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(self): when(targetpackage).targetfunction().then_return('123') targetpackage.targetfunction() verify(targetpackage).targetfunction()
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_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_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_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_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_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 set_up(self): def __eq__(self, other): return False def __ne__(self, other): return not self.__eq__(other) def __lt__(self, other): return False self.mock_reactor = Mock() self.mock_reactor.project.name = "any-project-name" self.task_1 = Mock() self.task_1.__eq__ = __eq__ self.task_1.__ne__ = __ne__ self.task_1.__lt__ = __lt__ self.task_1.name = "task-1" self.task_1.description = "" self.task_1.dependencies = [] self.task_2 = Mock() self.task_2.__eq__ = __eq__ self.task_2.__ne__ = __ne__ self.task_2.__lt__ = __lt__ self.task_2.name = "task-2" self.task_2.description = "" self.task_2.dependencies = [] when(self.mock_reactor).get_tasks().then_return([self.task_1, self.task_2]) when(pybuilder.cli).print_text_line(ANY_STRING).then_return(None)
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 set_up(self): def __eq__(self, other): return False def __ne__(self, other): return not self.__eq__(other) def __lt__(self, other): return False self.mock_reactor = Mock() self.mock_reactor.project.name = "any-project-name" self.task_1 = Mock() self.task_1.__eq__ = __eq__ self.task_1.__ne__ = __ne__ self.task_1.__lt__ = __lt__ self.task_1.name = "task-1" self.task_1.description = "" self.task_1.dependencies = [] self.task_2 = Mock() self.task_2.__eq__ = __eq__ self.task_2.__ne__ = __ne__ self.task_2.__lt__ = __lt__ self.task_2.name = "task-2" self.task_2.description = "" self.task_2.dependencies = [] when(self.mock_reactor).get_tasks().then_return( [self.task_1, self.task_2]) when(pybuilder.cli).print_text_line(ANY_STRING).then_return(None)
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_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_match_number_of_arguments_when_using_any_argument(self): when(targetpackage.subpackage).subtargetfunction( ANY_VALUE).then_return('specific') assert_that(targetpackage.subpackage.subtargetfunction(0, 1), equal_to(None))
def test_should_patch_away_class_method_for_new_style_classes(self): when(targetpackage.TheClass).some_class_method(1).then_return(10) when(targetpackage.TheClass).some_class_method(2).then_return(11) assert_that(targetpackage.TheClass.some_class_method(1), equal_to(10)) assert_that(targetpackage.TheClass.some_class_method(2), equal_to(11))
def test_should_not_match_when_using_any_argument_on_second_argument_and_first_argument_different( self): when(targetpackage.subpackage).subtargetfunction( 1, ANY_VALUE).then_return('specific') assert_that(targetpackage.subpackage.subtargetfunction(0, 2), equal_to(None))
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_reset_list_of_patches(self): when(targetpackage).patch_test_1().then_return('patched call! 1') when(targetpackage).patch_test_2().then_return('patched call! 2') undo_patches() assert_that(get_patches(), equal_to([]))
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_one_as_first_answer_when_two_answers_are_configured( self): when(targetpackage).targetfunction().then_return(1).then_return(2) actual_value = targetpackage.targetfunction() assert_that(actual_value, equal_to(1))
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_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_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_not_match_when_using_any_argument_on_second_argument_and_keyword_argument_does_not_match( self): when(targetpackage.subpackage).subtargetfunction( 1, ANY_VALUE, foo='spam').then_return('specific') assert_that( targetpackage.subpackage.subtargetfunction(1, 2, foo='eggs'), equal_to(None))