testing-cabal / testtools

Testtools - tasteful testing for python
https://testtools.readthedocs.io/en/latest/
Other
95 stars 88 forks source link

TestCase.assertRaises masks same function from unittest #275

Open s-t-e-v-e-n-k opened 6 years ago

s-t-e-v-e-n-k commented 6 years ago

This masking made perfect sense when assertRaises was awkward to use in the pre-2.7 days, but with unittest.TestCase.assertRaises() now able to be used a context manager if you want to perform multiple steps, and testtools.TestCase.assertRaises() alway expects one callable function with arguments.

I'm not sure of a good solution here.

s-t-e-v-e-n-k commented 6 years ago

Reflecting on this further, this also means you can't interrogate the raised exception further, for example, to make sure the message matches.

woutervb commented 4 years ago

Just to add, I found that this masking also breaks compatibility when asserRaises() is used as a context manager. In which case it will complain about a missing callableObject.

rbtcollins commented 4 years ago

So, you can use unittest.TestCase.assertRaises(self, ...) to access the masked method.

>>> class Base:
...  def foo(self): print ("1")
...
>>> class Child:
...  def foo(self): print("2")
...
>>> c = Child()
>>> Base.foo(c)
1

Changing this would be an API break, so perhaps get a few such things together and rip the bandaid off, if going to do it, but that said I suspect that some clever code might allow for assertRaises to be used as a context manager too.

However, personally, I prefer the raises matcher family.