My test that tests whether the correct recipe is returned by a search filter that uses the name field. The data in the database is stored ordered by the “-date_created” field. my fixture @pytest.fixture def recipes(user, ingredients): recipes = [ Recipe.objects.create( name="Pizza Margarrita", category="lunch", description = "some fantastic pizza", steps = "1 step, 2 step, 3 step", total_cooking_time=60, difficulty="medium", country="Italy", author=user ), Recipe.objects.create( name="Ratatouille", category="side_dish", description="some fantastic ratatouille", steps="1 step, 2 step, 3 step", total_cooking_time=90, difficulty="easy", country="France", author=user ) ] recipes[0].ingredients.set(ingredients["pizza"]) recipes[1].ingredients.set(ingredients["ratatouille"]) return list(Recipe.objects.all()) test @pytest.mark.django_db() def test_search_filter_with_field_name(client, recipes): recipe_url = reverse("recipe-list") response = client.get(recipe_url, {"search": "pizza"}) data = response.data["results"] expected_recipe_name = recipes[0].name unexpected_recipe_name = recipes[1].name returned_name = data[0]["name"] print(data) assert response.status_code == 200 assert len(data) == 1 assert expected_recipe_name == returned_name assert unexpected_recipe_name not in returned_name it works incorrectly 2 times, correctly 1 time and vice versa. Error AssertionError: assert 'Ratatouille' == 'Pizza Margarrita' I supose that the problem is in sorting by date, because some objects can be created faster in the database. I'd be happy to hear advice on how to solve this problem Continue reading...