Dargasia has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/326245 )
Change subject: Document test decorators. ...................................................................... Document test decorators. Documentation regarding mock.patch, tests.aspects.require_modules, unittest.skipIf and unittest.skipUnless is added to the README file. Bug: T152068 Change-Id: I335323a1704a9912dbc39bcec8042ae8ee706234 --- M tests/README.rst 1 file changed, 62 insertions(+), 1 deletion(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/45/326245/1 diff --git a/tests/README.rst b/tests/README.rst index fff848f..90f88db 100644 --- a/tests/README.rst +++ b/tests/README.rst @@ -226,6 +226,68 @@ Enabling only 'edit failure' tests or 'write' tests won't enable the other tests automatically. +Decorators +===================== + +pywikibot's test suite, including Python's unittest module, provides decorators +to modify the behaviour of the test cases. + +@unittest.skipIf +----------------- +Skip a test if the condition is true. Refer to unittest's documentation. + +:: + + import unittest + [......] + @unittest.skipIf(check_if_fatal(), 'Something is not okay.') + def test_skipIf(self): + +@unittest.skipUnless +--------------------- +Skip a test unless the condition is true. Refer to unittest's documentation. + +:: + + import unittest + [......] + @unittest.skipUnless(check_if_true(), 'Something must happen.') + def test_skipUnless(self): + +@tests.aspects.require_modules +------------------------------- +Require that the given list of modules can be imported. + +:: + + from tests.aspects import require_modules + [......] + @require_modules(['important1', 'musthave2']) + def test_require_modules(self): + +@(unittest.)mock.patch +----------------------- +Replaces `target` with object specified in `new`. Refer to mock's documentation. +This is especially useful in tests, where requests to third-parties should be +avoided. + +In Python 3, this is part of the built-in unittest module. + +:: + + if sys.version_info[0] > 2: + from unittest.mock import patch + else: + from mock import patch + + + def fake_ping(url): + return 'pong' + [......] + @patch('http_ping', side_effect=fake_ping) + def test_patch(self): + self.assertEqual('pong', http_ping()) + Contributing tests ================== @@ -293,4 +355,3 @@ - ``user = True`` : test class needs to login to site - ``sysop = True`` : test class needs to login to site as a sysop - ``write = True`` : test class needs to write to a site - -- To view, visit https://gerrit.wikimedia.org/r/326245 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I335323a1704a9912dbc39bcec8042ae8ee706234 Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Dargasia <hx...@dargasea.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits