jenkins-bot has submitted this change and it was merged.
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, 38 insertions(+), 0 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/aspects.py b/tests/aspects.py
index f4016d7..5dec99c 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -1019,6 +1019,44 @@
return result
+class PatchingTestCase(TestCase):
+
+ """Easily patch and unpatch instances."""
+
+ @staticmethod
+ def patched(obj, attr_name):
+ """Apply patching information."""
+ def add_patch(decorated):
+ decorated._patching = (obj, attr_name)
+ return decorated
+ return add_patch
+
+ 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_instances += [(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_instances = []
+ 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_instances:
+ setattr(*patched)
+ super(TestCaseBase, self).tearDown()
+
+
class SiteAttributeTestCase(TestCase):
"""Add the sites as attributes to the instances."""
--
To view, visit
https://gerrit.wikimedia.org/r/231797
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Id08a2ebf12edda4eb5d3a4673b9defd1859b4aa4
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: jenkins-bot <>