jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/832276 )
Change subject: [tests] Improvement for test_tests ......................................................................
[tests] Improvement for test_tests
- Add UtilsTests to test fixed_generator and entered_loop - Explain the HttpServerProblemTestCase - Explain expected_failure of TestLengthAssertion - Add documentation to fixed_generator
Change-Id: Ie7481ac3a05e945105806fbc793077fc785fa9c5 --- M tests/tests_tests.py M tests/utils.py 2 files changed, 45 insertions(+), 5 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/tests_tests.py b/tests/tests_tests.py index e69df36..47c1974 100755 --- a/tests/tests_tests.py +++ b/tests/tests_tests.py @@ -8,6 +8,7 @@ from contextlib import suppress
from tests.aspects import TestCase +from tests import utils
class HttpServerProblemTestCase(TestCase): @@ -21,13 +22,18 @@ }
def test_502(self): - """Test a HTTP 502 response using http://httpbin.org/status/502.""" - self.fail('The test framework should skip this test.') + """Test that framework is skipping this test due to HTTP status 502.""" + self.fail("The test framework should skip this test but it hasn't.")
-class TestLengthAssert(TestCase): +class TestLengthAssertion(TestCase):
- """Test length assertion methods.""" + """Test length assertion methods. + + ``@unittest.expectedFailure`` is used to test the failure of a test; + this is intentional. If the decorated test passes unexpectedly the + test will fail. + """
net = False
@@ -70,6 +76,24 @@ self.assertLength(None, self.seq)
+class UtilsTests(TestCase): + + """Tests for tests.utils.""" + + net = False + pattern = 'Hello World' + + def test_fixed_generator(self): + """Test utils.fixed_generator.""" + gen = utils.fixed_generator(self.pattern) + self.assertEqual(list(gen(1, 'foo', bar='baz')), list(self.pattern)) + + def test_entered_loop(self): + """Test utils.entered_loop.""" + self.assertTrue(utils.entered_loop(self.pattern)) + self.assertFalse(utils.entered_loop('')) + + if __name__ == '__main__': # pragma: no cover with suppress(SystemExit): unittest.main() diff --git a/tests/utils.py b/tests/utils.py index 536adcb..9b8b815 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -42,7 +42,23 @@
def fixed_generator(iterable): - """Return a dummy generator ignoring all parameters.""" + """Return a dummy generator ignoring all parameters. + + This can be used to overwrite a generator method and yield + predefined items: + + >>> from tests.utils import fixed_generator + >>> site = pywikibot.Site() + >>> page = pywikibot.Page(site, 'Any page') + >>> list(page.linkedPages(total=1)) + [] + >>> gen = fixed_generator([ + ... pywikibot.Page(site, 'User:BobBot/Redir'), + ... pywikibot.Page(site, 'Main Page')]) + >>> page.linkedPages = gen + >>> list(page.linkedPages(total=1)) + [Page('Benutzer:BobBot/Redir'), Page('Main Page')] + """ def gen(*args, **kwargs): yield from iterable
pywikibot-commits@lists.wikimedia.org