jenkins-bot has submitted this change and it was merged.
Change subject: tests: Use unittest2 on python 2.6
......................................................................
tests: Use unittest2 on python 2.6
unittest2 is a backport of python 2.7s unittest features to 2.6.
Use it when running the tests that already use those features.
Change-Id: I5332e9e0815475d4bb4147bb5c1ef0d0ddc5c3bd
---
M setup.py
M tests/api_tests.py
M tests/dry_api_tests.py
M tests/dry_site_tests.py
M tests/i18n_tests.py
M tests/page_tests.py
M tests/pwb_tests.py
M tests/site_tests.py
M tests/textlib_tests.py
M tests/ui_tests.py
M tests/utils.py
M tests/wikibase_tests.py
12 files changed, 61 insertions(+), 32 deletions(-)
Approvals:
Merlijn van Deen: Looks good to me, approved
jenkins-bot: Verified
diff --git a/setup.py b/setup.py
index a38cfe8..b022d2f 100644
--- a/setup.py
+++ b/setup.py
@@ -17,10 +17,17 @@
from setuptools import setup, find_packages
from setuptools.command import install
+test_deps = []
+
if sys.version_info[0] != 2:
raise RuntimeError("ERROR: Pywikipediabot only runs under Python 2")
elif sys.version_info[1] < 6:
raise RuntimeError("ERROR: Pywikipediabot only runs under Python 2.6 or
higher")
+elif sys.version_info[1] == 6:
+ test_deps = ['unittest2']
+ testcollector = "tests.utils.collector"
+else:
+ testcollector = "tests"
class pwb_install(install.install):
@@ -47,7 +54,8 @@
dependency_links=[
'https://git.wikimedia.org/zip/?r=pywikibot/externals/httplib2.git&format=gz#egg=httplib2-0.8-pywikibot1'
],
- test_suite="tests",
+ test_suite=testcollector,
+ tests_require=test_deps,
classifiers=[
'License :: OSI Approved :: MIT License',
'Development Status :: 4 - Beta'
diff --git a/tests/api_tests.py b/tests/api_tests.py
index 7c21ff0..4437036 100644
--- a/tests/api_tests.py
+++ b/tests/api_tests.py
@@ -9,8 +9,7 @@
import datetime
import pywikibot
import pywikibot.data.api as api
-import unittest
-from utils import PywikibotTestCase
+from utils import PywikibotTestCase, unittest
mysite = pywikibot.Site('en', 'wikipedia')
diff --git a/tests/dry_api_tests.py b/tests/dry_api_tests.py
index cc18083..82437af 100644
--- a/tests/dry_api_tests.py
+++ b/tests/dry_api_tests.py
@@ -1,11 +1,11 @@
-import datetime
-import pywikibot
-from pywikibot.data.api import CachedRequest
-import unittest
-
-
-class DryAPITests(unittest.TestCase):
-
+import datetime
+import pywikibot
+from pywikibot.data.api import CachedRequest
+from utils import unittest
+
+
+class DryAPITests(unittest.TestCase):
+
def setUp(self):
self.parms = {'site': pywikibot.Site('en'),
'action': 'query',
diff --git a/tests/dry_site_tests.py b/tests/dry_site_tests.py
index 8847345..61b6452 100644
--- a/tests/dry_site_tests.py
+++ b/tests/dry_site_tests.py
@@ -1,8 +1,9 @@
-import pywikibot
-import unittest
-
-
-class DrySite(pywikibot.site.APISite):
+import pywikibot
+
+from utils import unittest
+
+
+class DrySite(pywikibot.site.APISite):
@property
def userinfo(self):
return self._userinfo
diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py
index 8bef1e6..104f308 100644
--- a/tests/i18n_tests.py
+++ b/tests/i18n_tests.py
@@ -6,11 +6,12 @@
#
__version__ = '$Id$'
-import unittest
import os
from pywikibot import i18n
import shutil
+from utils import unittest
+
class TestTranslate(unittest.TestCase):
def setUp(self):
diff --git a/tests/page_tests.py b/tests/page_tests.py
index 5a60678..d764f68 100644
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -10,11 +10,10 @@
__version__ = '$Id$'
-import unittest
import pywikibot
import pywikibot.page
-from utils import PywikibotTestCase
+from utils import PywikibotTestCase, unittest
site = pywikibot.Site('en', 'wikipedia')
mainpage = pywikibot.Page(pywikibot.page.Link("Main Page", site))
diff --git a/tests/pwb_tests.py b/tests/pwb_tests.py
index bd30b33..51db60b 100644
--- a/tests/pwb_tests.py
+++ b/tests/pwb_tests.py
@@ -9,15 +9,18 @@
import os
import sys
import subprocess
-
-import unittest
-
import pywikibot
+
+from utils import unittest
pypath = sys.executable
basepath = os.path.split(os.path.split(__file__)[0])[0]
pwbpath = os.path.join(basepath, 'pwb.py')
testbasepath = os.path.join(basepath, 'tests', 'pwb')
+
+
+def check_output(command):
+ return subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
class TestPwb(unittest.TestCase):
@@ -35,8 +38,8 @@
the environment we get when directly running a script."""
test = os.path.join(testbasepath, 'print_locals.py')
- direct = subprocess.check_output([pypath, test])
- vpwb = subprocess.check_output([pypath, pwbpath, test])
+ direct = check_output([pypath, test])
+ vpwb = check_output([pypath, pwbpath, test])
self.assertEqual(direct, vpwb)
if __name__ == "__main__":
diff --git a/tests/site_tests.py b/tests/site_tests.py
index 3958601..a3f6d81 100644
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -10,11 +10,10 @@
__version__ = '$Id$'
-import unittest
import pywikibot
import warnings
from tests import patch_request, unpatch_request
-from utils import PywikibotTestCase
+from utils import PywikibotTestCase, unittest
mysite = None
mainpage = None
@@ -35,7 +34,6 @@
def testBaseMethods(self):
"""Test cases for BaseSite methods"""
-
self.assertEqual(mysite.family.name, self.family)
self.assertEqual(mysite.code, self.code)
self.assertType(mysite.lang, basestring)
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index 3f504c0..52aa8e2 100644
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -10,13 +10,14 @@
import mwparserfromhell
except ImportError:
mwparserfromhell = False
-import unittest
import codecs
import os
import pywikibot
import pywikibot.textlib as textlib
+from utils import unittest
+
files = {}
dirname = os.path.join(os.path.dirname(__file__), "pages")
diff --git a/tests/ui_tests.py b/tests/ui_tests.py
index d4ba066..2f01410 100644
--- a/tests/ui_tests.py
+++ b/tests/ui_tests.py
@@ -9,13 +9,14 @@
#
__version__ = '$Id$'
-import unittest
import cStringIO
import StringIO
import logging
import os
import sys
+from utils import unittest
+
if os.name == "nt":
from multiprocessing.managers import BaseManager
import threading
diff --git a/tests/utils.py b/tests/utils.py
index adb31b7..bccf715 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -1,4 +1,23 @@
-import unittest
+try:
+ # Unittest2 is a backport of python 2.7s unittest module to python 2.6
+ # Trying to import unittest2 has to happen first because 2.6 does have a
+ # unittest module in the standard library but that doesn't support all the
+ # features of the one found in python 2.7, so importing unittest first and
+ # then trying to figure out if it supports the features used would mean
+ # checking the module contents etc. Just catching an ImportError once is
+ # much easier.
+ import unittest2 as unittest
+except ImportError:
+ import unittest
+
+
+def collector():
+ # This test collector loads tests from the `tests` directory in files
+ # matching the pattern `*tests.py`. This gets used by `setup.py test` when
+ # running on Python 2.6 to use the unittest2 test runner instead of the
+ # unittest one.
+ return unittest.loader.defaultTestLoader.discover("tests",
"*tests.py")
+
from tests import patch_request, unpatch_request
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index ef39990..eb653d8 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -10,11 +10,10 @@
__version__ = '$Id$'
import os
-import unittest
import pywikibot
import json
-from utils import PywikibotTestCase
+from utils import PywikibotTestCase, unittest
site = pywikibot.Site('en', 'wikipedia')
mainpage = pywikibot.Page(pywikibot.page.Link("Main Page", site))
--
To view, visit
https://gerrit.wikimedia.org/r/86436
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I5332e9e0815475d4bb4147bb5c1ef0d0ddc5c3bd
Gerrit-PatchSet: 4
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mineo <themineo(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Legoktm <legoktm.wikipedia(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: jenkins-bot