I am using pytest to test my django rest framework API and am gettin gan error on the following test:
def test_client_gets_invalid_when_submitting_invlaid_data(self):
client = APIClient()
response = client.post(path="/user/register/", data={})
assert response.status_code is 400
traceback in pytest is as follows:
> assert response.status_code is 400
E assert 400 is 400
E where 400 = <Response status_code=400, "application/json">.status_code
core\tests\test_views.py:26: AssertionError
I dont understand how this error can be happening when 400 is literally equal to 400?
CodePudding user response:
So it turns out that the keyword is
tests for identity in memory, where as ==
tests for value.
So the solution was to simply change from is
to ==
https://www.geeksforgeeks.org/python-object-comparison-is-vs/
CodePudding user response:
The is
operator compares the identity of the two objects, not their value. In this case, response.status_code
is an int object, and 400 is also an int object, but they are different instances of int objects.
You should use the ==
operator instead to compare their values.
def test_client_gets_invalid_when_submitting_invlaid_data(self):
client = APIClient()
response = client.post(path="/user/register/", data={})
assert response.status_code == 400