0

Given this code:

def main(b):
    try:
        a = 2 / b
        return "Hello World"
    except Exception:
        return "Exception"

How to test the path which handles the exception using patching? Would it possible to make a = 2 / b as a valid target to patch?

Similar questions:

How to use Python Mock to raise an exception - but with Errno set to a given value

Mocking - How do I raise exception on the caller?

Mocking a function to raise an Exception to test an except block

Community
  • 1
  • 1
Shravan
  • 2,553
  • 2
  • 16
  • 19
  • That path can never possibly be reached, since neither of the statements within it will ever cause an exception. And you can't patch either assignment or return, so there is no way to trigger it. Why do you have code like this in the first place? – Daniel Roseman Nov 28 '16 at 16:27
  • Sorry for the poor example. Ideally, I would like to patch `a = 2 / b`, where the function can take an argument like this `def main(b)`. I can just make `b` as zero and trigger an exception. But, I was trying to figure out how to do this with patching. I have edited the question now. – Shravan Nov 28 '16 at 16:55

0 Answers0