I'm writing a unit test whereby I want to assert that the error I get:
<ValidationError: "'a list' is not of type 'array'">
is equal too
assert ValidationError(message="'a list' is not of type 'array'")
I'm using a simple assertion:
assert actual == expected
When I run pytest
I get the following error:
assert <ValidationError: "'a list' is not of type 'array'"> == <ValidationError: "'a list' is not of type 'array'">
The validation error comes from jsonschema
from jsonschema import ValidationError
Even though they are identical, the assertion still fails. Does anyone know why?
CodePudding user response:
Normally I would structure this using pytest.raises
def test_validation_error():
with pytest.raises(ValidationError, match="'a list' is not of type 'array'"):
# whatever you are doing to raise the error
assert ValidationError(message="'a list' is not of type 'array'")
CodePudding user response:
This exception doesn't seem to have __eq__
method defined, so it is being compared using ids - and since those are 2 different objects, they have different ids. Just like this code will throw an error, because Python doesn't really know how 2 different TypeErrors should be compared.
assert TypeError('a') == TypeError('a')
If you check pytest documentation, suggested way to handle expected exceptions looks like this:
def test_zero_division():
with pytest.raises(ZeroDivisionError):
1 / 0