How To Assert Operators < And >= Are Not Implemented?
Initially I was not using the unittest framework, so to test that two objects of the same class are not comparable using the operators < and >= I did something like: try:
Solution 1:
Use assertRaises
as a context manager:
with self.assertRaises(TypeError):
o1 < o2
Here is an explanation of the with
statement. Here are the docs. TL;DR: It allows the execution of a code block with a "context", i.e. things to be set up and disposed before/after the execution, error handling etc.
In the case of assertRaises
, its context manager simply checks whether an execption of the required type has been raised, by checking the exc
agrument passed to its __exit__
method.
Post a Comment for "How To Assert Operators < And >= Are Not Implemented?"