question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing exception messages

See original GitHub issue

Re discussion in #1052, what is the general consensus on testing for exception messages? The way I see it, there are a few ways we can handle this (in order of work required):

  1. Ignore exception messages
  2. Test only for exception messages that are explicitly included in the canonical data for the exercise
  3. When exception messages are not explicitly stated in the canonical data, require that the exercise contributor invent messages for all expected exceptions

Additionally, if we are to test for exception messages at all, we need to have a conventional method of doing so in both Python2 and Python3. assertRaisesRegexp is replaced by assertRaisesRegex in Python3, so as mentioned in #1052 there are a few ways of handling this difference.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
N-Parsonscommented, Nov 8, 2017

If we do implement checks for the presence of error messages, I think we should definitely add something about it to the README insert so that it’s clear that we’re expecting them (and to encourage people to make them meaningful).

Exception types are controlled by the test-suite, so there’s not really much problem on the learner side, but we should try to have some consistency between the test-suites. We could create something along the lines of the Choosing Exceptions to Raise doc pandas has to give everyone something to refer to.

0reactions
cmccandlesscommented, Mar 7, 2019

For future record, here is the proper way to handle exception testing in this repository:

class TestClass(unittest.TestCase):
	def test_raises_exception(self):
        with self.assertRaisesWithMessage(ValueError):
            method_that_raises_ValueError()

    # Utility functions
    def setUp(self):
        try:
            self.assertRaisesRegex
        except AttributeError:
            self.assertRaisesRegex = self.assertRaisesRegexp

    def assertRaisesWithMessage(self, exception):
        return self.assertRaisesRegex(exception, r".+")

Edit: if Python 2.7 is no longer supported, the above snippet may be simplified to the following:

class TestClass(unittest.TestCase):
    def test_raises_exception(self):
        with self.assertRaisesWithMessage(ValueError):
            method_that_raises_ValueError()

    # Utility functions
    def assertRaisesWithMessage(self, exception):
        return self.assertRaisesRegex(exception, r".+")
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I assert my exception message with JUnit Test ...
If my test method throws a checked exception and if I want to assert the message along with the exception, is there a...
Read more >
Testing the wording of an Exception message
The main point of testing the exception message content is to make sure the right exception is thrown. There may be multiple reasons...
Read more >
JUnit Test Exception Examples - How to assert an exception is ...
1. Test Exception in JUnit 5 - using assertThrows() method ... You put the code that can throw exception in the execute() method...
Read more >
JUnit Assert Exception - JUnit 5 and JUnit 4 - DigitalOcean
We can test expected exceptions using JUnit 5 assertThrows assertion. This JUnit assertion method returns the thrown exception, so we can use it ......
Read more >
Java: how to assert exception messages with JUnit 4 - LinkedIn
Introduction. It's easy to write a unit test to check if a certain exception is thrown, like this: @Test(expected = IndexOutOfBoundsException.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found