I have a bunch of classes which deal with validation of values. For instance, a
RangeValidator class checks whether a value is within the specified range.
Every validator class contains two methods:
is_valid(value), which returns
False depending on the value, and
ensure_valid(value) which checks for a specified value and either does nothing if the value is valid, or throws a specific exception if the value doesn't match the predefined rules.
There are currently two unit tests associated with this method:
The one which passes an invalid value, and ensures that the exception was thrown.
def test_outside_range(self): with self.assertRaises(demo.ValidationException): demo.RangeValidator(0, 100).ensure_valid(-5)
The one which passes a valid value.
def test_in_range(self): demo.RangeValidator(0, 100).ensure_valid(25)
Although the second test does its job—fails if the exception is thrown, and succeeds if
ensure_valid doesn't throw anything—the fact that there is no
asserts inside looks strange. Someone who reads such code would immediately ask himself why is there a test which appears to be doing nothing.
Is this a current practice when testing methods which don't return a value and don't have side effects? Or should I rewrite the test in a different way? Or simply put a comment explaining what I'm doing?