Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestCase.assertRaises masks same function from unittest #275

Open
s-t-e-v-e-n-k opened this issue Sep 25, 2018 · 3 comments
Open

TestCase.assertRaises masks same function from unittest #275

s-t-e-v-e-n-k opened this issue Sep 25, 2018 · 3 comments

Comments

@s-t-e-v-e-n-k
Copy link

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
Copy link
Author

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

@woutervb
Copy link

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants