0

我是编写单元测试的新手,所以请原谅我缺乏知识。我看过以前的帖子,但仍然无法使其正常工作。

我有

def get_bugs():
  bugs = []
  if ...:
    bugs.append(123)

  # can be empty
  return bugs

def operate(bugs):
  for bug in bugs:
    do something

def main():
  bugs = get_bugs()

  if bugs:
    operate(bugs)
    .... # other methods

-------------------------

# in my test

@mock.patch.object(myutility, "get_bugs", autospec=True, return_value=[])
def test_nobugstooperate():
   # logic to ensure myutility.operate was not called because there are no bugs

如何实现从未调用过 mutility.operate 的测试?我不能在上面使用“.call”,因为它不可用。

4

1 回答 1

0

据我所知,您无法测试是否尚未调用方法。如果您正在进行单元测试,您应该编写多个测试,每个测试都测试被测试单元的一种预期行为。在您的情况下,您实际上必须测试是否operate在存在要操作的错误时调用您的函数。如果没有错误,您不应该担心operate调用。

于 2021-04-08T00:08:07.363 回答