0

I am a newbie at writing unit tests so pardon my lack of knowledge. I have looked at previous posts, but still not able to get it working.

I have

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

How do I implement the test where mutility.operate was never called? I cannot use '.called' on it because its not available.

sam
  • 1,280
  • 2
  • 11
  • 20
  • 1
    Does this answer your question? [Find Out If a Function has been Called](https://stackoverflow.com/questions/9882280/find-out-if-a-function-has-been-called) – ItsMeNaira Mar 05 '20 at 23:27
  • Is the call to `get_bugs` repeatable/consistent in return value? Any reason not to just call that directly in the test and look for a return value? Otherwise [this](https://stackoverflow.com/q/3829742/10682164) question has a few answers that look promising utilizing mock. – totalhack Mar 06 '20 at 00:09

1 Answers1

0

As far as I know you cannot test if a method has not been called. If you are unit testing you should be writing more than one test, each one testing one expected behavior of the unit being tested. In your case you actually have to test if your operate function is being called when there are bugs to operate on. If there are no bugs you should not be worried about the operate call.

CaMMelo
  • 143
  • 5