FOSSASIA

coala: Replace assertRaises with assertRaisesRegex

Python unittest framework provides two methods for checking that an operation raises an expected exception: assertRaises and assertRaisesRegex.

Using unittest.assertRaises should be avoided. unitest.assertRaisesRegex has an extra parameter to check the exception message, and should be used instead. By checking the exception message, the unit test verifies that the exception is precisely the one which was expected, rather than only of the same type as expected.

coala-bears has 6 unit test modules using assertRaises, and coala has 44.

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 tests, e.g.

     $ py.test-3 tests/java/CheckstyleBearTest.py
     ..
    
     E           AssertionError: "voodoo" does not match "...."
    
  3. Replace the unexpected message, like 'voodoo', with a distinctive part of the message that py.test reported occurred.

  4. Create a Pull Request.

Follow http://coala.io/newcomer to join the coala organisation, create your commit, and submit the pull request.

Examples:

REQUIREMENTS

  • get added to the coala organization publicly.

EXPECTED OUTCOME

  • Accepted pull request

LINKS

Task tags

  • python
  • regex
  • unittest
  • pytest
  • re

Students who completed this task

Giovan Isa Musthofa, yuki_is_bored, Abishek V Ashok, Ridhwanul Haque

Task type

  • code Code
  • done_all Quality Assurance
close

2016