XZise has uploaded a new change for review.
https://gerrit.wikimedia.org/r/231797
Change subject: [FEAT] tests: Add ability to easily patch parts
......................................................................
[FEAT] tests: Add ability to easily patch parts
This adds the decorator `TestCaseBase.patched()` and the method
`TestCaseBase.patch()` to easily patch something which is unpatched in
`tearDown`. Each method with that `@patched()` decorator will be automatically
patched into the given target. Anything more complex can be done manually with
`patch()`.
Change-Id: Id08a2ebf12edda4eb5d3a4673b9defd1859b4aa4
---
M tests/aspects.py
1 file changed, 33 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/97/231797/1
diff --git a/tests/aspects.py b/tests/aspects.py
index f4016d7..e22b867 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -63,6 +63,14 @@
"""Base class for all tests."""
+ @staticmethod
+ def patched(obj, attr_name):
+ """Apply patching information."""
+ def add_patch(decorated):
+ decorated._patching = (obj, attr_name)
+ return decorated
+ return add_patch
+
if not hasattr(unittest.TestCase, 'assertRaisesRegex'):
def assertRaisesRegex(self, *args, **kwargs):
"""
@@ -245,6 +253,31 @@
assertPagelistTitles = assertPageTitlesEqual
+ def patch(self, obj, attr_name, replacement):
+ """
+ Patch the obj's attribute with the replacement.
+
+ It will be reset after each C{tearDown}.
+ """
+ self._patched += [(obj, attr_name, getattr(obj, attr_name))]
+ setattr(obj, attr_name, replacement)
+
+ def setUp(self):
+ """Set up the test by initializing the patched list."""
+ super(TestCaseBase, self).setUp()
+ self._patched = []
+ for attribute in dir(self):
+ attribute = getattr(self, attribute)
+ if callable(attribute) and hasattr(attribute, '_patching'):
+ self.patch(attribute._patching[0], attribute._patching[1],
+ attribute)
+
+ def tearDown(self):
+ """Tear down the test by unpatching the patched."""
+ for patched in self._patched:
+ setattr(*patched)
+ super(TestCaseBase, self).tearDown()
+
class TestLoggingMixin(TestCaseBase):
--
To view, visit https://gerrit.wikimedia.org/r/231797
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id08a2ebf12edda4eb5d3a4673b9defd1859b4aa4
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits