jenkins-bot has submitted this change and it was merged.
Change subject: [FIX] tests: Correctly handle abstract classes
......................................................................
[FIX] tests: Correctly handle abstract classes
The condition for an abstract test class was just that there are no test
classes in the current class it tries to create. So when a test case
subclassed another test case and didn't add more tests it was classified an
abstract class and it skipped a lot of checks, which includes whether it's a
networked test.
Also fix the `MementoTestBase` class as it subclasses just `TestCaseBase` which
doesn't do any magic like automatically determining `net` when for example a
`hostname` is set or subclassing `CheckHostnameMixin` when a `hostname`
attribute is set.
Change-Id: I0bdf4d1ee63e5123b519ba6631a79514bf3d8a7c
---
M tests/aspects.py
M tests/weblinkchecker_tests.py
2 files changed, 28 insertions(+), 15 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/aspects.py b/tests/aspects.py
index 10add8d..570f58f 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -657,7 +657,14 @@
for attr_name in dct
if attr_name.startswith('test')]
- dct['abstract_class'] = len(tests) == 0
+ base_tests = []
+ if not tests:
+ for base in bases:
+ base_tests += [attr_name
+ for attr_name, attr in base.__dict__.items()
+ if attr_name.startswith('test') and
callable(attr)]
+
+ dct['abstract_class'] = not tests and not base_tests
# Bail out if it is the abstract class.
if dct['abstract_class']:
@@ -716,8 +723,7 @@
if (('sites' not in dct and 'site' not in dct) or
('site' in dct and not dct['site'])):
# Prevent use of pywikibot.Site
- if all(not issubclass(base, DisableSiteMixin) for base in bases):
- bases = tuple([DisableSiteMixin] + list(bases))
+ bases = cls.add_base(bases, DisableSiteMixin)
# 'pwb' tests will _usually_ require a site. To ensure the
# test class dependencies are declarative, this requires the
@@ -751,29 +757,29 @@
# The following section is only processed if the test uses sites.
if 'dry' in dct and dct['dry']:
- bases = tuple([DisconnectedSiteMixin] + list(bases))
+ bases = cls.add_base(bases, DisconnectedSiteMixin)
del dct['net']
else:
dct['net'] = True
if 'cacheinfo' in dct and dct['cacheinfo']:
- bases = tuple([CacheInfoMixin] + list(bases))
+ bases = cls.add_base(bases, CacheInfoMixin)
if 'cached' in dct and dct['cached']:
- bases = tuple([ForceCacheMixin] + list(bases))
+ bases = cls.add_base(bases, ForceCacheMixin)
if 'net' in dct and dct['net']:
- bases = tuple([CheckHostnameMixin] + list(bases))
+ bases = cls.add_base(bases, CheckHostnameMixin)
else:
assert not hostnames, 'net must be True with hostnames defined'
if 'write' in dct and dct['write']:
if 'user' not in dct:
dct['user'] = True
- bases = tuple([SiteWriteMixin] + list(bases))
+ bases = cls.add_base(bases, SiteWriteMixin)
if ('user' in dct and dct['user']) or ('sysop' in dct and
dct['sysop']):
- bases = tuple([RequireUserMixin] + list(bases))
+ bases = cls.add_base(bases, RequireUserMixin)
for test in tests:
test_func = dct[test]
@@ -807,6 +813,13 @@
return super(MetaTestCaseClass, cls).__new__(cls, name, bases, dct)
+ @staticmethod
+ def add_base(bases, subclass):
+ """Return a tuple of bases with the subclasses added if not
already."""
+ if not any(issubclass(base, subclass) for base in bases):
+ bases = (subclass, ) + bases
+ return bases
+
@add_metaclass
class TestCase(TestTimerMixin, TestCaseBase):
diff --git a/tests/weblinkchecker_tests.py b/tests/weblinkchecker_tests.py
index 7526745..52a1b5a 100644
--- a/tests/weblinkchecker_tests.py
+++ b/tests/weblinkchecker_tests.py
@@ -19,12 +19,12 @@
from scripts import weblinkchecker
-from tests.aspects import unittest, require_modules, TestCase, TestCaseBase
+from tests.aspects import unittest, require_modules, TestCase
from tests import weblib_tests
@require_modules('memento_client')
-class MementoTestBase(TestCaseBase):
+class MementoTestCase(TestCase):
"""Test memento client."""
@@ -39,7 +39,7 @@
self.timegate_uri)
-class WeblibTestMementoInternetArchive(MementoTestBase,
weblib_tests.TestInternetArchive):
+class WeblibTestMementoInternetArchive(MementoTestCase,
weblib_tests.TestInternetArchive):
"""Test InternetArchive Memento using old weblib
tests."""
@@ -47,7 +47,7 @@
hostname = timegate_uri
-class WeblibTestMementoWebCite(MementoTestBase, weblib_tests.TestWebCite):
+class WeblibTestMementoWebCite(MementoTestCase, weblib_tests.TestWebCite):
"""Test WebCite Memento using old weblib tests."""
@@ -55,7 +55,7 @@
hostname = timegate_uri
-class TestMementoWebCite(MementoTestBase):
+class TestMementoWebCite(MementoTestCase):
"""New WebCite Memento tests."""
@@ -70,7 +70,7 @@
self.assertEqual(parsed.netloc, 'www.webcitation.org')
-class TestMementoDefault(MementoTestBase, TestCase):
+class TestMementoDefault(MementoTestCase):
"""Test InternetArchive is default Memento
timegate."""
--
To view, visit
https://gerrit.wikimedia.org/r/238416
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I0bdf4d1ee63e5123b519ba6631a79514bf3d8a7c
Gerrit-PatchSet: 4
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 <>