-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing exception messages #1080
Comments
Worth noting is that we have a precedent for methods that differ between Python2 and Python3. |
Personally I'm not a great fan of requiring particular error messages to be raised by the user. Meaningful messages are great, but exact wording just feels like unnecessary copy-and-pasting to me, and doesn't give the learner the freedom to think about and express what the meaning/source of the error is. This would particularly frustrate me if I thought that the error message was imprecise or I wanted to also display key values. If we want to test whether people are including messages with their exceptions, we can do a regex for |
Usually, there are two issues:
If we address this two issues this will be a good enough. |
Sounds like @N-Parsons 's solution of using
This is situational; I don't know that there's a good standardized way to handle this other than |
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 |
(Fixes exercism#1080) * Add self.assertRaisesWithMessage method to relevant exercise tests - Uses self.assertRaisesRegex - Checks only for the presence of a message, not content * Add meaningful messages to failing examples * octal: Switch to using a context manager for exception tests
(Fixes exercism#1080) * Add self.assertRaisesWithMessage method to relevant exercise tests - Uses self.assertRaisesRegex - Checks only for the presence of a message, not content * Add meaningful messages to failing examples * octal: Switch to using a context manager for exception tests
* Implement checks for messages being raised with exceptions (Fixes #1080) * Add self.assertRaisesWithMessage method to relevant exercise tests - Uses self.assertRaisesRegex - Checks only for the presence of a message, not content * Add meaningful messages to failing examples * octal: Switch to using a context manager for exception tests * Add note regarding error messages to the insert * simple-linked-list: Move hints.md to correct location * simple-cipher: Remove extra whitespace from lines * collatz-conjecture: Update hints.md * Regenerate README to include exceptions section
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".+") |
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):
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 byassertRaisesRegex
in Python3, so as mentioned in #1052 there are a few ways of handling this difference.The text was updated successfully, but these errors were encountered: