Search code examples
pytest-django

Why is pytest client giving 404 error when I should be getting 400 error


Problem summary: I am going through testdriven.io tdd django course and ran into an issue. Pytest client has a different http response than what postman and httpie are giving for the same payload

What I tried: I tried posting the same data on all three clients(httpie, postman, pytest) I am testing. When i use httpie or postman i get a 400 error so I would expect when I write the test the testing client will see a 400 error but test fails because client is seeing 404.

@pytest.mark.django_db
def test_add_movie_invalid_json_keys(client):
    movies = Movie.objects.all()
    assert len(movies) == 0

    resp = client.post(
      "api/movies/",
      {"title": "The Big Lebowski", "year": "1998"},
      content_type="application/json",
    )
    assert resp.status_code == 400

    movies = Movie.objects.all()
    assert len(movies) == 0

pytest output


Solution

  • You're missing a forward slash in the URL. It should be "/api/movies/".