Wikimedia

Pywikibot: Replace assertRaises with assertRaisesRegex

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 unitest.assertRaisesRegex which has an extra parameter to check the exception message, and should be used instead.

Pywikibot has 33 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: *https://gerrit.wikimedia.org/r/#/c/330099/

In a different project:

Pywikibot is a Python-based framework to write bots for MediaWiki. See https://www.mediawiki.org/wiki/Manual:Pywikibot for more information. Documentation on Gerrit can be found at https://www.mediawiki.org/wiki/Manual:Pywikibot/Gerrit. After you have successfully claimed this task on this site, please use the Phabricator task for communication.

Task tags

  • python
  • regex
  • unittest
  • assert
  • re

Students who completed this task

Justin Du, Geoffrey Mon, David Sn, Kenstin, Yudhistira Gowo Samiaji, Maciej Chodorowski

Task type

  • code Code
  • done_all Quality Assurance
close

2016