How do I deliberately make a specific exception in a unit test?

Asked 2 years ago, Updated 2 years ago, 112 views

We need to do a unit test for the exception Is there a way to deliberately cause specific exceptions such as EOFError and ZeroDivisionError?

unit-testing python exception exception-handling

2022-09-21 21:07

1 Answers

In the unittest module, You must write TestCase.assertRaises().

For example,

import mymod

class MyTestCase(unittest.TestCase):
    def test1(self):
        self.assertRaises(ZeroDivisionError, mymod.myfunc)

Together

If a function receives a factor, self.assertRaises(ZeroDivisionError, mymod.myfunc, arg1, arg2) You can pass the factor as shown in .


2022-09-21 21:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.