jenkins-bot has submitted this change and it was merged. Change subject: [FEAT] tests: Add decorator to check installs ......................................................................
[FEAT] tests: Add decorator to check installs Some tests require that other parts are installed and currently this was always checked manually. This adds a decorator which defines which modules a method or class requires and skips the test if one of the modules can't be imported. Change-Id: Id581b427aa4b5414d94a94317d55be195f15e755 --- M tests/aspects.py M tests/interwiki_graph_tests.py M tests/oauth_tests.py M tests/proofreadpage_tests.py M tests/textlib_tests.py M tests/weblinkchecker_tests.py 6 files changed, 32 insertions(+), 44 deletions(-) Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified diff --git a/tests/aspects.py b/tests/aspects.py index 991a140..3fdb963 100644 --- a/tests/aspects.py +++ b/tests/aspects.py @@ -331,6 +331,25 @@ super(TestTimerMixin, self).tearDown() +def require_modules(*required_modules): + """Require that the given list of modules can be imported.""" + def test_requirement(obj): + """Test the requirement and return an optionally decorated object.""" + missing = [] + for required_module in required_modules: + try: + __import__(required_module, globals(), locals(), [], 0) + except ImportError: + missing += [required_module] + if missing: + return unittest.skip('{0} not installed'.format( + ', '.join(missing)))(obj) + else: + return obj + + return test_requirement + + class DisableSiteMixin(TestCaseBase): """Test cases not connected to a Site object. diff --git a/tests/interwiki_graph_tests.py b/tests/interwiki_graph_tests.py index fa4d539..57a4039 100644 --- a/tests/interwiki_graph_tests.py +++ b/tests/interwiki_graph_tests.py @@ -12,10 +12,11 @@ from pywikibot import interwiki_graph -from tests.aspects import unittest, SiteAttributeTestCase +from tests.aspects import unittest, require_modules, SiteAttributeTestCase from tests.utils import DryPage +@require_modules('pydot') class TestWiktionaryGraph(SiteAttributeTestCase): """Tests for interwiki links to local sites.""" @@ -39,8 +40,6 @@ @classmethod def setUpClass(cls): """Setup test class.""" - if isinstance(interwiki_graph.pydot, ImportError): - raise unittest.SkipTest('pydot not installed') super(TestWiktionaryGraph, cls).setUpClass() cls.pages = { diff --git a/tests/oauth_tests.py b/tests/oauth_tests.py index a0210de..ecb9fb2 100644 --- a/tests/oauth_tests.py +++ b/tests/oauth_tests.py @@ -11,31 +11,21 @@ import os -try: - import mwoauth -except ImportError as e: - mwoauth = e - from pywikibot.login import OauthLoginManager from tests.aspects import ( unittest, + require_modules, TestCase, DefaultSiteTestCase, ) +@require_modules('mwoauth') class OAuthSiteTestCase(TestCase): """Run tests related to OAuth authentication.""" oauth = True - - @classmethod - def setUpClass(cls): - """Check if mwoauth is installed.""" - super(OAuthSiteTestCase, cls).setUpClass() - if isinstance(mwoauth, ImportError): - raise unittest.SkipTest('mwoauth not installed') def _get_oauth_tokens(self): """Get valid OAuth tokens from environment variables.""" diff --git a/tests/proofreadpage_tests.py b/tests/proofreadpage_tests.py index 6bf2181..cf3cc2b 100644 --- a/tests/proofreadpage_tests.py +++ b/tests/proofreadpage_tests.py @@ -11,16 +11,11 @@ import json -try: - from bs4 import BeautifulSoup -except ImportError as e: - BeautifulSoup = e - import pywikibot from pywikibot.data import api from pywikibot.proofreadpage import IndexPage, ProofreadPage -from tests.aspects import unittest, TestCase +from tests.aspects import unittest, require_modules, TestCase from tests.basepage_tests import ( BasePageMethodsTestBase, @@ -207,16 +202,12 @@ self.assertEqual(json.loads(page_text), json.loads(loaded_text)) +@require_modules('bs4') class IndexPageTestCase(TestCase): """Run tests related to IndexPage ProofreadPage extension.""" - @classmethod - def setUpClass(cls): - """Check if beautifulsoup4 is installed.""" - super(IndexPageTestCase, cls).setUpClass() - if isinstance(BeautifulSoup, ImportError): - raise unittest.SkipTest('beautifulsoup4 not installed') + pass class TestIndexPageInvalidSite(IndexPageTestCase): diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py index adb7c21..f5e3bad 100644 --- a/tests/textlib_tests.py +++ b/tests/textlib_tests.py @@ -14,11 +14,6 @@ import os import re -try: - import mwparserfromhell -except ImportError as e: - mwparserfromhell = e - import pywikibot import pywikibot.textlib as textlib @@ -26,7 +21,9 @@ from pywikibot.site import _IWEntry from pywikibot.tools import OrderedDict -from tests.aspects import unittest, TestCase, DefaultDrySiteTestCase +from tests.aspects import ( + unittest, require_modules, TestCase, DefaultDrySiteTestCase, +) files = {} dirname = os.path.join(os.path.dirname(__file__), "pages") @@ -353,11 +350,9 @@ ('2', u'd')])), ('b', OrderedDict([('1', 'c')]))]) + @require_modules('mwparserfromhell') def test_extract_templates_params_mwpfh(self): """Test using mwparserfromhell.""" - if isinstance(mwparserfromhell, ImportError): - raise unittest.SkipTest('mwparserfromhell not available') - func = textlib.extract_templates_and_params_mwpfh self._common_results(func) self._order_differs(func) diff --git a/tests/weblinkchecker_tests.py b/tests/weblinkchecker_tests.py index 280a2e9..7526745 100644 --- a/tests/weblinkchecker_tests.py +++ b/tests/weblinkchecker_tests.py @@ -19,20 +19,14 @@ from scripts import weblinkchecker -from tests.aspects import unittest, TestCase, TestCaseBase +from tests.aspects import unittest, require_modules, TestCase, TestCaseBase from tests import weblib_tests +@require_modules('memento_client') class MementoTestBase(TestCaseBase): """Test memento client.""" - - @classmethod - def setUpClass(cls): - """Set up test class.""" - if isinstance(weblinkchecker.memento_client, ImportError): - raise unittest.SkipTest('memento_client not imported') - super(MementoTestBase, cls).setUpClass() def _get_archive_url(self, url, date_string=None): if date_string is None: -- To view, visit https://gerrit.wikimedia.org/r/238353 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Id581b427aa4b5414d94a94317d55be195f15e755 Gerrit-PatchSet: 5 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: XZise <commodorefabia...@gmx.de> Gerrit-Reviewer: John Vandenberg <jay...@gmail.com> Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com> Gerrit-Reviewer: XZise <commodorefabia...@gmx.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ Pywikibot-commits mailing list Pywikibot-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits