jenkins-bot has submitted this change and it was merged.
Change subject: [FIX] tests: Support Python 3 in metaclass tests
......................................................................
[FIX] tests: Support Python 3 in metaclass tests
Some tests (date, l10n and logentry) were just using __metaclass__ which is not
supported by Python 3. The metaclasses in aspects and script_tests were using
six on Python 3 to avoid that. This adds a generic decorator to tests.utils
which looks for __metaclass__ in the class itself and then uses six's
add_metaclass (like in aspects and script_tests) to add this metaclass.
Change-Id: I8cb46bffb39ef4640ec523e596fba5fbf48fbd8d
---
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/utils.py
M tox.ini
7 files changed, 27 insertions(+), 15 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
Ricordisamoa: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/tests/aspects.py b/tests/aspects.py
index ae29255..f96d9f0 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -36,9 +36,6 @@
import time
import warnings
-if sys.version_info[0] > 2:
- import six
-
import pywikibot
from pywikibot import config, log, ServerError, Site
@@ -50,7 +47,7 @@
import tests
from tests import unittest, patch_request, unpatch_request
-from tests.utils import execute_pwb, DrySite, DryRequest
+from tests.utils import execute_pwb, DrySite, DryRequest, add_metaclass
class TestCaseBase(unittest.TestCase):
@@ -794,6 +791,7 @@
return super(MetaTestCaseClass, cls).__new__(cls, name, bases, dct)
+@add_metaclass
class TestCase(TestTimerMixin, TestLoggingMixin, TestCaseBase):
"""Run tests on pre-defined sites."""
@@ -925,10 +923,6 @@
raise unittest.SkipTest("Did not find a page that does not
exist.")
return page
-
-
-if sys.version_info[0] > 2:
- TestCase = six.add_metaclass(MetaTestCaseClass)(TestCase)
class SiteAttributeTestCase(TestCase):
diff --git a/tests/date_tests.py b/tests/date_tests.py
index 178da8f..526b358 100644
--- a/tests/date_tests.py
+++ b/tests/date_tests.py
@@ -10,7 +10,9 @@
from datetime import datetime
from pywikibot import date
+
from tests.aspects import unittest, MetaTestCaseClass, TestCase
+from tests.utils import add_metaclass
class TestDateMeta(MetaTestCaseClass):
@@ -54,6 +56,7 @@
return type.__new__(cls, name, bases, dct)
+@add_metaclass
class TestDate(TestCase):
"""Test cases for date library processed by
unittest."""
diff --git a/tests/l10n_tests.py b/tests/l10n_tests.py
index 2dbe1c8..46981a7 100644
--- a/tests/l10n_tests.py
+++ b/tests/l10n_tests.py
@@ -10,10 +10,12 @@
__version__ = '$Id$'
import re
-from tests.aspects import unittest, MetaTestCaseClass, TestCase
+
import pywikibot
from pywikibot import i18n
+from tests.aspects import unittest, MetaTestCaseClass, TestCase
+from tests.utils import add_metaclass
PACKAGES = (
'redirect-broken-redirect-template', # speedy deletion template
@@ -62,11 +64,13 @@
return type.__new__(cls, name, bases, dct)
+@add_metaclass
class TestValidTemplate(TestCase):
"""Test cases L10N message templates processed by
unittest."""
__metaclass__ = TestValidTemplateMeta
+
net = True # magic flag tells jenkins to not run the test.
diff --git a/tests/logentry_tests.py b/tests/logentry_tests.py
index 772b652..afa49e5 100644
--- a/tests/logentry_tests.py
+++ b/tests/logentry_tests.py
@@ -18,6 +18,7 @@
from tests.aspects import (
unittest, MetaTestCaseClass, TestCase, DeprecationTestCase
)
+from tests.utils import add_metaclass
if sys.version_info[0] > 2:
unicode = str
@@ -65,6 +66,7 @@
return super(MetaTestCaseClass, cls).__new__(cls, name, bases, dct)
+@add_metaclass
class TestLogentries(TestCase):
"""Test TestLogentries processed by unittest."""
diff --git a/tests/script_tests.py b/tests/script_tests.py
index d6b97d7..bf11a8b 100644
--- a/tests/script_tests.py
+++ b/tests/script_tests.py
@@ -15,7 +15,7 @@
from tests import _root_dir
from tests.aspects import unittest, DefaultSiteTestCase, MetaTestCaseClass, PwbTestCase
-from tests.utils import allowed_failure, execute_pwb
+from tests.utils import allowed_failure, execute_pwb, add_metaclass
if sys.version_info[0] > 2:
basestring = (str, )
@@ -386,6 +386,7 @@
return super(TestScriptMeta, cls).__new__(cls, name, bases, dct)
+@add_metaclass
class TestScript(DefaultSiteTestCase, PwbTestCase):
"""Test cases for scripts.
@@ -400,10 +401,6 @@
user = True
-
-if sys.version_info[0] > 2:
- import six
- TestScript = six.add_metaclass(TestScriptMeta)(TestScript)
if __name__ == '__main__':
try:
diff --git a/tests/utils.py b/tests/utils.py
index 093c6b5..f55093a 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -17,6 +17,9 @@
from warnings import warn
+if sys.version_info[0] > 2:
+ import six
+
import pywikibot
from pywikibot import config
@@ -90,6 +93,15 @@
return lambda orig: orig
+def add_metaclass(cls):
+ """Call six's add_metaclass with the site's __metaclass__ in
Python 3."""
+ if sys.version_info[0] > 2:
+ return six.add_metaclass(cls.__metaclass__)(cls)
+ else:
+ assert(cls.__metaclass__)
+ return cls
+
+
class DryParamInfo(dict):
"""Dummy class to use instead of
L{pywikibot.data.api.ParamInfo}."""
diff --git a/tox.ini b/tox.ini
index e34218b..67e1655 100644
--- a/tox.ini
+++ b/tox.ini
@@ -143,7 +143,7 @@
[testenv:nose34]
basepython = python3
-commands =
+commands =
python -W error::UserWarning -m generate_user_files -family:test -lang:test -v
nosetests --version
nosetests --with-doctest --with-doctest-ignore-unicode -v -a "!net" tests
pywikibot {[params]nose_skip}
--
To view, visit
https://gerrit.wikimedia.org/r/214907
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I8cb46bffb39ef4640ec523e596fba5fbf48fbd8d
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: Ricordisamoa <ricordisamoa(a)openmailbox.org>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>