Repository navigation
Module 11 testing: assertIsNone vs assertEqual(..., None) for the invalid-input test?
#22
|
def add_numbers(a, b):
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
return None
return a + b
# TODO: TestAddNumbers with test_positive_numbers, test_negative_numbers, test_invalid_inputFor |
Replies: 1 comment
|
"Both work —
So the full method: def test_invalid_input(self):
self.assertIsNone(add_numbers("2", 3))
self.assertIsNone(add_numbers(3, None))One bonus trap worth an extra test if you want to go past the exercise: booleans. |
"Both work —
assertEqual(add_numbers("2", 3), None)passes — butassertIsNoneis the better choice, and here's why it's worth deviating from the TODO's literal wording:assertIsNone(result)reads as "this returns None", which is the test's meaning.assertEqual(result, None)reads as "this equals None", which happens to be the same thing only becauseNoneis a singleton. Reviewers and graders read the first as fluency.assertIsNone's failure output is purpose-built (None is not Noneplus the actual repr), whileassertEqual's generic diff buries the point.assertTrue(x)vsassertIs(True, ...),assertIn,assertRaises— the specific assertions…