John Vandenberg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/178069

Change subject: test decorator allowedFailure
......................................................................

test decorator allowedFailure

Test runners each have different interpretations of what should be
the result of an @expectedFailure test if it succeeds.  Some
consider it to be a pass; others a failure.

This decorator runs the test and, if it is a failure, reports the
failure and considers it a skipped test.

Change-Id: Ia87dda2780a9fe4130b2820298f07f983d5db80b
---
M tests/api_tests.py
M tests/site_tests.py
M tests/utils.py
3 files changed, 45 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/69/178069/1

diff --git a/tests/api_tests.py b/tests/api_tests.py
index 9746229..cd9fe60 100644
--- a/tests/api_tests.py
+++ b/tests/api_tests.py
@@ -19,6 +19,7 @@
     DefaultSiteTestCase,
     DefaultDrySiteTestCase,
 )
+from tests.utils import allowedFailure
 
 
 class TestApiFunctions(DefaultSiteTestCase):
@@ -395,7 +396,7 @@
             count += 1
         self.assertEqual(len(links), count)
 
-    @unittest.expectedFailure
+    @allowedFailure
     def test_many_continuations_limited(self):
         mainpage = self.get_mainpage()
         links = list(self.site.pagelinks(mainpage, total=30))
@@ -419,7 +420,7 @@
         self.assertEqual(len(links), count)
         # FIXME: AssertionError: 30 != 6150
 
-    @unittest.expectedFailure
+    @allowedFailure
     def test_two_continuations_limited(self):
         # FIXME: test fails
         mainpage = self.get_mainpage()
diff --git a/tests/site_tests.py b/tests/site_tests.py
index 5d7a964..de50124 100644
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -19,7 +19,6 @@
 from pywikibot.tools import MediaWikiVersion as LV
 from pywikibot.data import api
 
-from tests.utils import expectedFailureIf
 from tests.aspects import (
     unittest, TestCase,
     DefaultSiteTestCase,
@@ -27,6 +26,7 @@
     WikidataTestCase,
     DefaultWikidataClientTestCase,
 )
+from tests.utils import allowedFailure, allowedFailureIf
 
 if sys.version_info[0] > 2:
     basestring = (str, )
@@ -678,7 +678,7 @@
             self.assertIsInstance(using, pywikibot.Page)
             self.assertIn(imagepage, list(using.imagelinks()))
 
-    @expectedFailureIf(os.environ.get('TRAVIS', 'false') == 'true')
+    @allowedFailureIf(os.environ.get('TRAVIS', 'false') == 'true')
     def test_image_usage_in_redirects(self):
         """Test the site.imageusage() method on redirects only."""
         mysite = self.get_site()
@@ -1772,7 +1772,7 @@
             if count > 5:
                 break
 
-    @unittest.expectedFailure
+    @allowedFailure
     def test_preload_langlinks_normal(self):
         """Test preloading continuation works."""
         # FIXME: test fails
@@ -1792,7 +1792,7 @@
             if count >= 6:
                 break
 
-    @unittest.expectedFailure
+    @allowedFailure
     def test_preload_langlinks_count(self):
         """Test preloading continuation works."""
         # FIXME: test fails
@@ -1834,7 +1834,7 @@
 
         self.assertEqual(len(links), count)
 
-    @unittest.expectedFailure
+    @allowedFailure
     def test_preload_templates(self):
         """Test preloading templates works."""
         mysite = self.get_site()
@@ -1854,7 +1854,7 @@
             if count >= 6:
                 break
 
-    @unittest.expectedFailure
+    @allowedFailure
     def test_preload_templates_and_langlinks(self):
         """Test preloading templates and langlinks works."""
         mysite = self.get_site()
diff --git a/tests/utils.py b/tests/utils.py
index c932e59..3abee7b 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -8,6 +8,7 @@
 from __future__ import print_function
 __version__ = '$Id$'
 #
+
 import pywikibot
 from pywikibot.tools import SelfCallDict
 from pywikibot.site import Namespace
@@ -26,13 +27,46 @@
 
 def expectedFailureIf(expect):
     """
-    Unit test decorator to expect/allow failure under conditions.
+    Unit test decorator to expect failure under conditions.
+
+    @param expect: Flag to check if failure is expected
+    @type expect: bool
+    """
+    if expect:
+        return unittest.expectedFailure
+    else:
+        return lambda orig: orig
+
+
+def allowedFailure(func):
+    """
+    Unit test decorator to allow failure.
+
+    Test runners each have different interpretations of what should be
+    the result of an @expectedFailure test if it succeeds.  Some consider
+    it to be a pass; others a failure.
+
+    This decorator runs the test and, if it is a failure, reports the result
+    and considers it a skipped test.
+    """
+    def wrapper(*args, **kwargs):
+        try:
+            func(*args, **kwargs)
+        except Exception:
+            pywikibot.exception(tb=True)
+            raise unittest.SkipTest()
+    return wrapper
+
+
+def allowedFailureIf(expect):
+    """
+    Unit test decorator to allow failure under conditions.
 
     @param expect: Flag to check if failure is allowed
     @type expect: bool
     """
     if expect:
-        return unittest.expectedFailure
+        return allowedFailure
     else:
         return lambda orig: orig
 

-- 
To view, visit https://gerrit.wikimedia.org/r/178069
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia87dda2780a9fe4130b2820298f07f983d5db80b
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to