jenkins-bot merged this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[tests] Use allowed_failure with Python 2 behaviour

With Python 3 unittest.expectedFailure fails tests if they passes unexpectedly.
In some circumstances tests fail or pass if the test parameters aren't
deterministic. In such cases the test suite should be able to pass like
it does with Python 2.

- Don't care about exceptions other than AssertionError; they should
always fail.
- Unfortunately this decorator does not work with subtests
- Remove deprecation warnings and allow this decorator to be used in
such cases described above
- Additional documentation

Bug: T223030
Bug: T233484
Change-Id: Iab611d29c655b49ad442384e3c8c1edcfeccca9c
---
M tests/generate_family_files_tests.py
M tests/utils.py
2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/tests/generate_family_files_tests.py b/tests/generate_family_files_tests.py
index 7c4f008..54deeaa 100644
--- a/tests/generate_family_files_tests.py
+++ b/tests/generate_family_files_tests.py
@@ -12,6 +12,7 @@
from pywikibot import Site

from tests.aspects import unittest, DefaultSiteTestCase
+from tests.utils import allowed_failure

import generate_family_file

@@ -52,7 +53,7 @@
self.assertIsInstance(self.generator_instance.wikis, dict)
self.assertIsInstance(self.generator_instance.langs, list)

- @unittest.expectedFailure # T194138
+ @allowed_failure # T194138
def test_attributes_after_run(self):
"""Test FamilyFileGenerator attributes after run()."""
self.generator_instance.run()
@@ -61,9 +62,8 @@
self.assertIn(lang, self.generator_instance.wikis)
for i in range(10):
lang = choice(self.generator_instance.langs)
- with self.subTest(lang=lang['prefix']):
- site = Site(url=lang['url'])
- self.assertEqual(site.lang, lang['prefix'])
+ site = Site(url=lang['url'])
+ self.assertEqual(site.lang, lang['prefix'])


if __name__ == '__main__': # pragma: no cover
diff --git a/tests/utils.py b/tests/utils.py
index 76b4091..9b57c5a 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -38,7 +38,6 @@
from pywikibot.data.api import Request as _original_Request
from pywikibot.site import Namespace
from pywikibot.tools import (
- deprecated,
PY2, PYTHON_VERSION,
UnicodeType as unicode,
)
@@ -73,23 +72,27 @@
return lambda orig: orig


-@deprecated('unittest.expectedFailure', since='20190512')
def allowed_failure(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 an Assertion failure,
+ reports the result and considers it a skipped test. This is a
+ similar behaviour like expectedFailure in Python 2. Passing a test
+ will not count as failure (unexpected success) which Python 3 does.

- This decorator runs the test and, if it is a failure, reports the result
- and considers it a skipped test.
+ This decorator should be used if a test passes or fails due to
+ random test parameters. If tests fails deterministic expectedFailure
+ decorator should be used instead.
+
+ @note: This decorator does not support subTest content manager.
"""
@wraps(func)
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except AssertionError:
+ pywikibot.exception(tb=False)
tb = traceback.extract_tb(sys.exc_info()[2])
for depth, line in enumerate(tb):
if re.match('assert[A-Z]', line[2]):
@@ -97,9 +100,6 @@
tb = traceback.format_list(tb[:depth])
pywikibot.error('\n' + ''.join(tb)[:-1]) # remove \n at the end
raise unittest.SkipTest('Test is allowed to fail.')
- except Exception:
- pywikibot.exception(tb=True)
- raise unittest.SkipTest('Test is allowed to fail.')

if PY2:
return unittest.expectedFailure(func)
@@ -107,11 +107,13 @@
return wrapper


-@deprecated('expected_failure_if', since='20190512')
def allowed_failure_if(expect):
"""
Unit test decorator to allow failure under conditions.

+ See allowed_failure method for more details.
+
+ @note: This decorator does not support subTest content manager.
@param expect: Flag to check if failure is allowed
@type expect: bool
"""

To view, visit change 548768. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Iab611d29c655b49ad442384e3c8c1edcfeccca9c
Gerrit-Change-Number: 548768
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Dvorapa <dvorapa@seznam.cz>
Gerrit-Reviewer: Framawiki <framawiki@tools.wmflabs.org>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot (75)
Gerrit-CC: Mpaa <mpaa.wiki@gmail.com>