Wikimedia

Replace assertRaises with assertRaisesRegex in Pywikibot (I)

Python unittest framework provides two methods for checking that an operation raises an expected exception:

  1. assertRaises, which is not a good assertion, and
  2. unittest.assertRaises unittest.assertRaisesRegex which has an extra parameter to check the exception message, and should be used instead.

Pywikibot has 28 unit test modules using assertRaises. (See Phabricator)

For this task, pick one unit test module, and convert it to using assertRaisesRegex.

The easiest way to do this is

1. Change the method and add an unexpected message, like 'voodoo':

 -        with self.assertRaises(AssertionError):
 +        with self.assertRaisesRegex(AssertionError, 'voodoo'):

2. Run the test

 $ python -m unittest -v tests.http_tests
 ..

 E           AssertionError: "voodoo" does not match "...."

3. Replace the unexpected message, i.e. 'voodoo', with a distinctive part of the message that unittest reported occurred.

4. Submit your change using Gerrit.

Examples:

Pywikibot is a Python-based framework to write bots for MediaWiki (more information. Documentation on Gerrit is available. After you have successfully claimed this task on this site, please use the Phabricator task for communication.

Task tags

  • python
  • unittest
  • testing
  • pytest
  • assertion

Students who completed this task

David Sn, C. F. Fairfeld, Rafid Aslam, Robin Richtsfeld, Ryan Chang

Task type

  • code Code
  • done_all Quality Assurance
close

2017