2

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?

2 Answers 2

2

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'")
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.