Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for instantiation of a python class during unit-tests

I for the life of me can not remember how to test for the instination of a python class.

It was something like:

try:
  m = MyModel()
except:
  ...

I can't remember the exception to call or the assert to use.

like image 999
Mike Ramirez Avatar asked Feb 01 '26 07:02

Mike Ramirez


1 Answers

There is no common exception to use for failed instantiation. The constructor (both __init__ and __new__) can raise any exception it wants. The except statement you have without an explicit exception will catch every type of exception, which may be what you want.

like image 58
dappawit Avatar answered Feb 03 '26 20:02

dappawit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!