jenkins-bot has submitted this change and it was merged.
Change subject: Print test duration greater than 10s ......................................................................
Print test duration greater than 10s
unittest only provides test duration for all tests. It is useful to see timing information per test, if it is longer than a reasonable time period for a test, and recording this information into travis build logs.
Tests which take a long period of time may need to be refactored to achieve the same goal with less setup work done inside the test, or even split into multiple tests.
Change-Id: I8134a99c37b0435ecc20a2ff571e77347f9c785e --- M tests/api_tests.py M tests/archivebot_tests.py M tests/date_tests.py M tests/dry_site_tests.py M tests/i18n_tests.py M tests/ipregex_tests.py M tests/page_tests.py M tests/pagegenerators_tests.py M tests/textlib_tests.py M tests/timestripper_tests.py M tests/utils.py M tests/weblib_tests.py M tests/wikidataquery_tests.py M tests/xmlreader_tests.py 14 files changed, 48 insertions(+), 26 deletions(-)
Approvals: John Vandenberg: Looks good to me, but someone else must approve Merlijn van Deen: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/api_tests.py b/tests/api_tests.py index 510f41a..ec3a454 100644 --- a/tests/api_tests.py +++ b/tests/api_tests.py @@ -14,7 +14,7 @@ mysite = pywikibot.Site('en', 'wikipedia')
-class TestApiFunctions(unittest.TestCase): +class TestApiFunctions(PywikibotTestCase):
def testObjectCreation(self): """Test that api.Request() creates an object with desired attributes""" diff --git a/tests/archivebot_tests.py b/tests/archivebot_tests.py index aab0031..fa1d915 100644 --- a/tests/archivebot_tests.py +++ b/tests/archivebot_tests.py @@ -11,7 +11,7 @@
import pywikibot import pywikibot.page -from tests.utils import unittest +from tests.utils import unittest, PywikibotTestCase
THREADS = { 'als': 4, 'ar': 1, 'bar': 0, 'bg': 0, 'bjn': 1, 'bs': 0, 'ca': 5, 'ckb': 2, @@ -82,7 +82,7 @@ return type.__new__(cls, name, bases, dct)
-class TestArchiveBot(unittest.TestCase): +class TestArchiveBot(PywikibotTestCase):
"""Test archivebot script"""
diff --git a/tests/date_tests.py b/tests/date_tests.py index 932b409..44b89d9 100644 --- a/tests/date_tests.py +++ b/tests/date_tests.py @@ -6,7 +6,7 @@ # __version__ = '$Id$'
-from tests.utils import unittest +from tests.utils import unittest, PywikibotTestCase from pywikibot import date
@@ -49,7 +49,7 @@ return type.__new__(cls, name, bases, dct)
-class TestDate(unittest.TestCase): +class TestDate(PywikibotTestCase): """Test cases for date library processed by unittest""" __metaclass__ = TestDateMeta
diff --git a/tests/dry_site_tests.py b/tests/dry_site_tests.py index 19f95a0..573c89e 100644 --- a/tests/dry_site_tests.py +++ b/tests/dry_site_tests.py @@ -52,6 +52,7 @@ self.family.name = 'test' self._logged_in_as = None self.obsolete = False + super(TestMustBe, self).setUp()
def login(self, sysop): # mock call diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py index af9e90c..78ddc89 100644 --- a/tests/i18n_tests.py +++ b/tests/i18n_tests.py @@ -8,10 +8,10 @@
from pywikibot import i18n
-from tests.utils import unittest +from tests.utils import unittest, PywikibotTestCase
-class TestTranslate(unittest.TestCase): +class TestTranslate(PywikibotTestCase): def setUp(self): self.msg_localized = {'en': u'test-localized EN', 'nl': u'test-localized NL', @@ -20,6 +20,7 @@ 'nl': u'test-semi-localized NL'} self.msg_non_localized = {'en': u'test-non-localized EN'} self.msg_no_english = {'ja': u'test-no-english JA'} + super(TestTranslate, self).setUp()
def testLocalized(self): self.assertEqual(i18n.translate('en', self.msg_localized, @@ -69,12 +70,14 @@ u'test-no-english JA')
-class TestTWN(unittest.TestCase): +class TestTWN(PywikibotTestCase): def setUp(self): self.orig_messages_package_name = i18n.messages_package_name i18n.messages_package_name = 'tests.i18n' + super(TestTWN, self).setUp()
def tearDown(self): + super(TestTWN, self).tearDown() i18n.messages_package_name = self.orig_messages_package_name
diff --git a/tests/ipregex_tests.py b/tests/ipregex_tests.py index 2d97459..4cc9ea9 100644 --- a/tests/ipregex_tests.py +++ b/tests/ipregex_tests.py @@ -6,20 +6,20 @@ # Distributed under the terms of the MIT license. __version__ = '$Id$'
-import pywikibot - -from tests.utils import unittest +from tests.utils import unittest, PywikibotTestCase from pywikibot.page import ip_regexp
-class PyWikiIpRegexCase(unittest.TestCase): +class PyWikiIpRegexCase(PywikibotTestCase): """Unit test class for ip_regexp"""
def setUp(self): self.total = 0 self.fail = 0 + super(PyWikiIpRegexCase, self).setUp()
def tearDown(self): + super(PyWikiIpRegexCase, self).tearDown() print '%d tests done, %d failed' % (self.total, self.fail) if self.fail: raise AssertionError diff --git a/tests/page_tests.py b/tests/page_tests.py index 7a023d6..bc27989 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -22,7 +22,7 @@ site))
-class TestLinkObject(unittest.TestCase): +class TestLinkObject(PywikibotTestCase): """Test cases for Link objects"""
enwiki = pywikibot.Site("en", "wikipedia") diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py index de98968..f5183bb 100644 --- a/tests/pagegenerators_tests.py +++ b/tests/pagegenerators_tests.py @@ -10,10 +10,10 @@ import pywikibot from pywikibot import pagegenerators
-from tests.utils import unittest +from tests.utils import unittest, PywikibotTestCase
-class TestPageGenerators(unittest.TestCase): +class TestPageGenerators(PywikibotTestCase): """Test pagegenerators methods"""
titles = [ @@ -35,6 +35,7 @@
def setUp(self): self.site = pywikibot.Site('en', 'wikipedia') + super(TestPageGenerators, self).setUp()
def assertFunction(self, obj): self.assertTrue(hasattr(pagegenerators, obj)) diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py index 3297c7d..ba7da8e 100644 --- a/tests/textlib_tests.py +++ b/tests/textlib_tests.py @@ -17,7 +17,7 @@ import pywikibot.textlib as textlib from pywikibot import config
-from tests.utils import unittest +from tests.utils import unittest, PywikibotTestCase
files = {} dirname = os.path.join(os.path.dirname(__file__), "pages") @@ -27,11 +27,12 @@ 'r', 'utf-8').read()
-class TestSectionFunctions(unittest.TestCase): +class TestSectionFunctions(PywikibotTestCase): def setUp(self): self.site = pywikibot.Site('en', 'wikipedia') self.catresult1 = ('[[Category:Cat1]]%(LS)s[[Category:Cat2]]%(LS)s' % {'LS': config.LS}) + super(TestSectionFunctions, self).setUp()
def contains(self, fn, sn): return textlib.does_text_contain_section( @@ -89,7 +90,7 @@ self.assertNotContains("enwiki_help_editing", u"Helpful tips", "section header must contain a link")
-class TestFormatFunctions(unittest.TestCase): +class TestFormatFunctions(PywikibotTestCase):
@classmethod def setUpClass(cls): diff --git a/tests/timestripper_tests.py b/tests/timestripper_tests.py index 019ac0b..79a1878 100644 --- a/tests/timestripper_tests.py +++ b/tests/timestripper_tests.py @@ -16,12 +16,13 @@ from pywikibot.textlib import TimeStripper, tzoneFixedOffset
-class TestTimeStripper(unittest.TestCase): +class TestTimeStripper(PywikibotTestCase): """Test cases for Link objects"""
def setUp(self): site = pywikibot.Site('fr', 'wikipedia') self.ts = TimeStripper(site) + super(TestTimeStripper, self).setUp()
def test_findmarker(self): """Test that string which is not part of text is found""" diff --git a/tests/utils.py b/tests/utils.py index a37e75c..9d02bba 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -4,8 +4,11 @@ # # Distributed under the terms of the MIT license. # +from __future__ import print_function __version__ = '$Id$' # +import time +import sys 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 @@ -17,6 +20,9 @@ import unittest2 as unittest except ImportError: import unittest + +# Number of seconds each test may consume before a note is added after the test. +test_duration_warning_interval = 10
def collector(): @@ -37,5 +43,14 @@ def setUp(self): patch_request()
+ self.test_start = time.time() + def tearDown(self): + self.test_completed = time.time() + duration = self.test_completed - self.test_start + + if duration > test_duration_warning_interval: + print(' %0.3fs' % duration, end=' ') + sys.stdout.flush() + unpatch_request() diff --git a/tests/weblib_tests.py b/tests/weblib_tests.py index 8d2a97c..b5183fc 100644 --- a/tests/weblib_tests.py +++ b/tests/weblib_tests.py @@ -13,10 +13,10 @@ from urllib.parse import urlparse
import pywikibot.weblib as weblib -from tests.utils import unittest +from tests.utils import unittest, PywikibotTestCase
-class TestArchiveSites(unittest.TestCase): +class TestArchiveSites(PywikibotTestCase): def testInternetArchiveNewest(self): archivedversion = weblib.getInternetArchiveURL('https://google.com') parsed = urlparse(archivedversion) diff --git a/tests/wikidataquery_tests.py b/tests/wikidataquery_tests.py index 8c9d83f..019887e 100644 --- a/tests/wikidataquery_tests.py +++ b/tests/wikidataquery_tests.py @@ -12,7 +12,7 @@
import pywikibot.data.wikidataquery as query -from tests.utils import unittest +from tests.utils import unittest, PywikibotTestCase
import pywikibot from pywikibot.page import ItemPage, PropertyPage, Claim @@ -21,7 +21,7 @@ import time
-class TestApiFunctions(unittest.TestCase): +class TestApiFunctions(PywikibotTestCase):
def setUp(self): super(TestApiFunctions, self).setUp() @@ -228,7 +228,7 @@ self.assertEqual(qs, "q=link%5Benwiki%5D&labels=en,fr&props=prop")
-class TestApiSlowFunctions(unittest.TestCase): +class TestApiSlowFunctions(PywikibotTestCase):
def testQueryApiGetter(self): """ diff --git a/tests/xmlreader_tests.py b/tests/xmlreader_tests.py index 10ecb59..e3ac959 100644 --- a/tests/xmlreader_tests.py +++ b/tests/xmlreader_tests.py @@ -12,10 +12,10 @@
import os.path from pywikibot import xmlreader -from tests.utils import unittest +from tests.utils import unittest, PywikibotTestCase
-class XmlReaderTestCase(unittest.TestCase): +class XmlReaderTestCase(PywikibotTestCase):
@classmethod def setUpClass(cls):
pywikibot-commits@lists.wikimedia.org