-
-
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
perfect-numbers: Update perfect-numbers to reflect changes to canonical data #1052
Conversation
|
||
class InvalidInputsTest(unittest.TestCase): | ||
def test_zero(self): | ||
self.assertRaises(ValueError, classify, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By convention we use context manager syntax for assertRaises
. Example:
with self.assertRaises(ValueError):
classify(0)
Also, if the canonical data specifies an error string, it is best practice to use assertRaisesRegexp
to check that the raised error contains the correct reason.
I've made the requested changes. However, it seems like Is this alright? |
@emerali I was not aware of the deprecation. In order to maintain backwards compatibility with Python 2.7, we have the following options:
import sys
if sys.version_info[0] == 2:
with self.assertRaisesRegexp(
ValueError,
("Classification is only possible"
" for positive whole numbers.")):
classify(0)
else:
with self.assertRaisesRegex(
ValueError,
("Classification is only possible"
" for positive whole numbers.")):
classify(0) @N-Parsons @m-a-ge thoughts? |
@cmccandless checking the Python version looks reasonable to me |
@cmccandless, I would prefer that we just use If we do want to continue with checking the message, then I would suggest defining a new class method that will call either try:
assertRaisesMessage = self.assertRaisesRegex # Python3
except AttributeError:
assertRaisesMessage = self.assertRaisesRegexp # Python2 If we're going to be testing that messages are passed when exceptions are raised, I think we should be careful about how strict we are with the exact message. Matching an important word might work, or we could just test that there is a non-empty message (ie. ".+") - I think it requires some thought. |
@N-Parsons the only reason I advised checking the error message is that in this case the spec for this exercise actually contains specific error messages. Normally they do not. However, I think you raise a good point on exact wording.... Perhaps it would be best to ignore error messages entirely as a track. |
Just to add my 2 cents: |
@cmccandless, while they do appear in the problem spec, I don't know how important the exact wording actually is (and there's some discussion about how to represent errors in exercism/problem-specifications#905 and exercism/problem-specifications#902). I'm happy with either checking or not checking error messages, but if we are going to expect a particular error message, then it should go into the README (via @emerali, expecting exact error messages is ok, but you will need to put a note about it in With regard to using a |
@N-Parsons I've created a new issue to discuss convention on handling exception messages. |
Pending discussion in #1080 |
@cmccandless No worries, I got a free t-shirt out of this, so I'm happy 😄 I've removed the exception message checks (and also updated the placeholder file to remove the unnecessary skeleton function), please double check and let me know if I've missed anything. Once the discussion in #1080 concludes, do let me know if you need some help updating some exercises, I'd be glad to help! |
@emerali Merging. Congrats, and thanks for working on this! |
Resolves #1001
A few things to note: