UnitTestPython模拟对方法的第一次调用,第二次调用照常进行

UnitTestPython模拟对方法的第一次调用,第二次调用照常进行,python,unit-testing,python-unittest,python-unittest.mock,Python,Unit Testing,Python Unittest,Python Unittest.mock,我在嘲笑一种方法。我想在第一次调用时引发异常,但在异常时,我将使用不同的参数再次调用该方法,因此我希望第二次调用能够正常处理。我需要做什么 代码 试试1 试试2 在第二次调用中,some_method按原样返回,数据不会使用不同的参数进行处理。我希望第二次迭代调用该方法并处理数据side_effect=[Exception,'some_other_Output']语法是什么?什么是some other_Output?您正在模拟一个方法,所以您所能做的就是断言该方法是使用预期参数第二次调用的。如果

我在嘲笑一种方法。我想在第一次调用时引发异常,但在异常时,我将使用不同的参数再次调用该方法,因此我希望第二次调用能够正常处理。我需要做什么

代码 试试1 试试2
在第二次调用中,
some_method
按原样返回,数据不会使用不同的参数进行处理。

我希望第二次迭代调用该方法并处理数据
side_effect=[Exception,'some_other_Output']
语法是什么?什么是
some other_Output
?您正在模拟一个方法,所以您所能做的就是断言该方法是使用预期参数第二次调用的。如果你正在模拟的方法没有经过测试,那么单独测试它。
class Foo(object):
  def Method1(self, arg):
    pass

  def Method2(self, arg):
    if not arg:
      raise
    self.Method1(arg)

  def Method3(self, arg):
    try:
      self.Method2(arg)
    except:
      self.Method2('some default value')

class FooTest(unittest.TestCase):
  def SetUp(self):
    self.helper = Foo()

  def TestFooMethod3(self):
    with mock.patch.object(self.helper, 'Method2', 
                           side_effect=[Exception,self.helper.Method1]
                           ) as mock_object:
      self.helper.Method3('fake_arg')
      mock_object.assert_has_calls([mock.call('fake_arg'),
                                    mock.call('some default value')])
with patch('xblock.runtime.Runtime.construct_xblock_from_class', Mock(side_effect=[Exception, some_method])):
class Foo(object):
  def Method1(self, arg):
    pass

  def Method2(self, arg):
    if not arg:
      raise
    self.Method1(arg)

  def Method3(self, arg):
    try:
      self.Method2(arg)
    except:
      self.Method2('some default value')

class FooTest(unittest.TestCase):
  def SetUp(self):
    self.helper = Foo()

  def TestFooMethod3(self):
    with mock.patch.object(self.helper, 'Method2', 
                           side_effect=[Exception,self.helper.Method1]
                           ) as mock_object:
      self.helper.Method3('fake_arg')
      mock_object.assert_has_calls([mock.call('fake_arg'),
                                    mock.call('some default value')])