jenkins-bot has submitted this change and it was merged.
Change subject: [IMPROV] Provide helper for generated tests
......................................................................
[IMPROV] Provide helper for generated tests
With 4ec75cd7 all tests which were generated using multiple sites are using
`__name__` now. But there are other cases which dynamically create tests and
may not add that property. So `MetaTestCaseClass` now supports `add_method`
which adds the value to the `dct` and sets `__name__` and allows to appends
something to a docstring (or to overwrite the docstring).
Change-Id: I7797ee461d82d764c32e92f773e9c7433fda24bd
---
M tests/aspects.py
M tests/date_tests.py
M tests/l10n_tests.py
M tests/logentry_tests.py
M tests/script_tests.py
M tests/tools_tests.py
6 files changed, 36 insertions(+), 24 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/aspects.py b/tests/aspects.py
index 8e94e38..bdcc0a0 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -848,9 +848,8 @@
# create test methods processed by unittest
for (key, sitedata) in dct['sites'].items():
test_name = test + '_' + key.replace('-', '_')
-
- dct[test_name] = wrap_method(key, sitedata, dct[test])
- dct[test_name].__name__ = str(test_name)
+ cls.add_method(dct, test_name,
+ wrap_method(key, sitedata, dct[test]))
if key in dct.get('expected_failures', []):
dct[test_name] = unittest.expectedFailure(dct[test_name])
@@ -866,6 +865,21 @@
bases = (subclass, ) + bases
return bases
+ @staticmethod
+ def add_method(dct, test_name, method, doc=None, doc_suffix=None):
+ """Add a method to a dictionary and set its name and
documention."""
+ dct[test_name] = method
+ # it's explicitly using str() because __name__ must be str
+ dct[test_name].__name__ = str(test_name)
+ if doc_suffix:
+ if not doc:
+ doc = method.__doc__
+ assert doc[-1] == '.'
+ doc = doc[:-1] + ' ' + doc_suffix + '.'
+
+ if doc:
+ dct[test_name].__doc__ = doc
+
@add_metaclass
class TestCase(TestTimerMixin, TestCaseBase):
diff --git a/tests/date_tests.py b/tests/date_tests.py
index c8fe5ab..c000332 100644
--- a/tests/date_tests.py
+++ b/tests/date_tests.py
@@ -49,11 +49,10 @@
return testMapEntry
for formatname in date.formats:
- # it's explicitly using str() because __name__ must be str
- test_name = str('test_' + formatname)
- dct[test_name] = test_method(formatname)
- dct[test_name].__name__ = test_name
- return type.__new__(cls, name, bases, dct)
+ cls.add_method(dct, 'test_' + formatname, test_method(formatname),
+ doc_suffix='using {0} format'.format(formatname))
+
+ return super(TestDateMeta, cls).__new__(cls, name, bases, dct)
@add_metaclass
diff --git a/tests/l10n_tests.py b/tests/l10n_tests.py
index d662647..cfbac35 100644
--- a/tests/l10n_tests.py
+++ b/tests/l10n_tests.py
@@ -68,9 +68,11 @@
for code in codes:
current_site = pywikibot.Site(code, dct['family'])
test_name = ("test_%s_%s" % (package,
code)).replace('-', '_')
- dct[test_name] = test_method(current_site)
- dct[test_name].__doc__ = 'Test {0} with language {1}'.format(
- package, code)
+ cls.add_method(
+ dct, test_name, test_method(current_site),
+ doc_suffix='{0} and language {1}'.format(
+ package, code))
+
return super(TestValidTemplateMeta, cls).__new__(cls, name, bases, dct)
diff --git a/tests/logentry_tests.py b/tests/logentry_tests.py
index b43c770..77c0d88 100644
--- a/tests/logentry_tests.py
+++ b/tests/logentry_tests.py
@@ -107,8 +107,8 @@
# create test methods for the support logtype classes
for logtype in LogEntryFactory._logtypes:
- test_name = str('test_%sEntry' % logtype.title())
- dct[test_name] = test_method(logtype)
+ cls.add_method(dct, 'test_%sEntry' % logtype.title(),
+ test_method(logtype))
return super(TestLogentriesMeta, cls).__new__(cls, name, bases, dct)
diff --git a/tests/script_tests.py b/tests/script_tests.py
index 66bd657..4f58633 100644
--- a/tests/script_tests.py
+++ b/tests/script_tests.py
@@ -332,7 +332,7 @@
return test_skip_script
return testScript
- argument = dct['_argument']
+ argument = '-' + dct['_argument']
for script_name in script_list:
# force login to be the first, alphabetically, so the login
@@ -345,18 +345,14 @@
else:
test_name = 'test_' + script_name
- # it's explicitly using str() because __name__ must be str
- test_name = str(test_name)
- dct[test_name] = test_execution(script_name, ['-' + argument])
+ cls.add_method(dct, test_name,
+ test_execution(script_name, [argument]),
+ 'Test running %s %s.' % (script_name, argument))
if script_name in dct['_expected_failures']:
dct[test_name] = unittest.expectedFailure(dct[test_name])
elif script_name in dct['_allowed_failures']:
dct[test_name] = allowed_failure(dct[test_name])
-
- dct[test_name].__doc__ = 'Test running %s -%s.' % (script_name,
- argument)
- dct[test_name].__name__ = test_name
# Disable test by default in nosetests
if script_name in unrunnable_script_list:
diff --git a/tests/tools_tests.py b/tests/tools_tests.py
index 919f28c..c58e33f 100644
--- a/tests/tools_tests.py
+++ b/tests/tools_tests.py
@@ -497,6 +497,7 @@
"""Create a new test case class."""
def create_test(method):
def test_method(self):
+ """Test getargspec."""
# all expect at least self and param
expected = method(1, 2)
returned = self.getargspec(method)
@@ -508,9 +509,9 @@
for name, tested_method in list(dct.items()):
if name.startswith('_method_test_'):
suffix = name[len('_method_test_'):]
- test_name = 'test_method_' + suffix
- dct[test_name] = create_test(tested_method)
- dct[test_name].__doc__ = 'Test getargspec on {0}'.format(suffix)
+ cls.add_method(dct, 'test_method_' + suffix,
+ create_test(tested_method),
+ doc_suffix='on {0}'.format(suffix))
dct['net'] = False
return super(MetaTestArgSpec, cls).__new__(cls, name, bases, dct)
--
To view, visit
https://gerrit.wikimedia.org/r/244966
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I7797ee461d82d764c32e92f773e9c7433fda24bd
Gerrit-PatchSet: 3
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: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>