Given you have a particular feature in foo_bar.py that you need to test, you will write a unittest.TestCase subclass and several methods that test the different use cases your feature may have, but you want to patch, say, foo_bar.Foo.foo and foo_bar.Bar.bar for all the use cases. Under this scenario you will probably find yourself repeating @patch decorators over the diverse test methods you wrote.
Avoid repeating patch
It would be nice to have @patch also work as a class decorator so that you can decorate the test class and have @patch stub out the given callables for ALL the test methods:
@patch('foo_bar.Foo.foo')
@patch('foo_bar.Bar.bar')
class FooBarTestCase(unittest.TestCase):
...
BUT you may also find that there are exceptions to the rule where you want the class-level @patch not to be applied for one or more particular test methods in the class because you want to override it with an alternative rvalue or side_effect. @patch should be able receive an optional argument ignore_tests=[] in which case the patch should be ignored when the given tests are running:
Given you have a particular feature in
foo_bar.py
that you need to test, you will write aunittest.TestCase
subclass and several methods that test the different use cases your feature may have, but you want to patch, say,foo_bar.Foo.foo
andfoo_bar.Bar.bar
for all the use cases. Under this scenario you will probably find yourself repeating@patch
decorators over the diverse test methods you wrote.Avoid repeating patch
It would be nice to have
@patch
also work as a class decorator so that you can decorate the test class and have@patch
stub out the given callables for ALL the test methods:instead of
@patch overriding
BUT you may also find that there are exceptions to the rule where you want the class-level
@patch
not to be applied for one or more particular test methods in the class because you want to override it with an alternativervalue
orside_effect
.@patch
should be able receive an optional argumentignore_tests=[]
in which case the patch should be ignored when the given tests are running: