-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
bpo-40331: Increase test coverage for the statistics module #19608
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
Changes from 1 commit
ccd8bef
21084c1
49def61
b2b9844
50a843f
bbe16bd
74b047a
1a5c2df
ac8b586
ee55b16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -1004,6 +1004,9 @@ def test_nan(self): | |||||||
| x = statistics._convert(nan, type(nan)) | ||||||||
| self.assertTrue(_nan_equal(x, nan)) | ||||||||
|
|
||||||||
| def test_raise_type_error(self): | ||||||||
|
||||||||
| self.assertRaises(TypeError, statistics._convert, None, float) | ||||||||
tzabal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| self.assertRaises(TypeError, statistics._convert, None, float) | |
| with self.assertRaises(TypeError): | |
| statistics._convert(None, float) |
(Also in following new tests in this PR.)
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.
Sure, I will change it here, as well as in the FindLteqTest and FindRteqTest tests, as you suggested.
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.
Sorry to mention it just after you made the change already, but I find that using assertRaisesRegex() tends to be a better practice than just assertRaises(). It's more significant of an issue for things like deprecation warnings, but it can help quite a bit with ensuring that you're getting the correct exception or warning.
So, it may not be necessary in the above case, but it's worth some consideration.
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.
I find that using assertRaisesRegex() tends to be a better practice than just assertRaises().
That may be true in general, but I think that in this case it's most appropriate to simply check that a TypeError is thrown.
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.
Yeah, looking at the above test a bit more closely, it seems highly unlikely for any other TypeError to occur besides the specific one being tested. So, assertRaises() is probably fine here.
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.
Thanks for your suggestion @aeros. As you have both already said, it may not be useful in this particular test, but maybe, it would be useful in the test_single_value_unsupported_type test (introduced in this PR) because it targets a specific TypeError with a custom message, while it is possible for the function to raise other TypeErrors too (through the _fail_neg function).
If you don't have any objections, I will go and modify the test_single_value_unsupported_type to use the assertRaisesRegex instead of the assertRaises.
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.
f you don't have any objections, I will go and modify the
test_single_value_unsupported_typeto use theassertRaisesRegexinstead of theassertRaises.
Go ahead.
Outdated
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.
Let's also find a better name for this, e.g. test_invalid_input_values
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.
Done.
Outdated
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.
I took a while to think about this, but I've decided that we should remove all of the comments referencing the specific statements that this is intended to exercise. My main reason is that the referenced code will likely change over time, and these comments are very likely to become stale.
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.
The more general reason is that tests must not test the implementation, but only the desired behavior. We might take inspiration for relevant test cases from the code, but the tests should not need to be changed when the code is changed in ways that don't affect its behavior, such as refactoring and optimization.
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.
Thank you for describing your reasoning behind this decision. I will remove those comments. I agree that good tests assert the desired behavior and not the actual implementation. I put them only for helping the reader understand why each of them was added, but all of them, trigger the same behavior (throwing the ValueError). And as you said, they may become obsolete if the implementation change.
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.
Comments explaining why, conceptually, each of the test cases should result in such an error, or perhaps which edge-case it is testing, could be welcome. This is true general, and specifically in this case. They're not required though; I'm just adding this note for completeness :)
Outdated
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.
I think this test method could also be renamed, similarly to the previous ones.
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.
Sorry, I should have changed this too.
Outdated
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.
This should come after the prepare_ methods.
Also, why test only a an array with a single value of an unsupported type? This would be a good opportunity for other unsupported cases, such as longer arrays, and arrays with values of mixed types, some supported and some not.
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.
You are right, I will put it right after the test_negative_error test.
I tested it with a single value of an unsupported type because the statement that explicitly raises this TypeError is part of the elif n == 1 branch, where n is the length of the given list. So a longer list would not trigger this specific TypeError, which was the only statement without being covered in this function. [1]
If you think that it would be useful to have a test with longer lists (only with unsupported values, both supported/unsupported values) and assert that a TypeError is raised, I could add one in the next commit. However, this would be for a TypeError that is not explicitly raised by this function.
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.
I suggest adding two assertions:
- multiple string values
- mixed strings and valid values (i.e. positive numbers).
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.
Thanks for the suggestions @taleinat. I have added a test in the latest commit that asserts those cases.
Uh oh!
There was an error while loading. Please reload this page.