Coverage for tests/test_tutorial/test_dependencies/test_tutorial008c.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1abcde

2from fastapi.exceptions import FastAPIError 1abcde

3from fastapi.testclient import TestClient 1abcde

4 

5 

6@pytest.fixture(name="client") 1abcde

7def get_client(): 1abcde

8 from docs_src.dependencies.tutorial008c import app 1abcde

9 

10 client = TestClient(app) 1abcde

11 return client 1abcde

12 

13 

14def test_get_no_item(client: TestClient): 1abcde

15 response = client.get("/items/foo") 1abcde

16 assert response.status_code == 404, response.text 1abcde

17 assert response.json() == {"detail": "Item not found, there's only a plumbus here"} 1abcde

18 

19 

20def test_get(client: TestClient): 1abcde

21 response = client.get("/items/plumbus") 1abcde

22 assert response.status_code == 200, response.text 1abcde

23 assert response.json() == "plumbus" 1abcde

24 

25 

26def test_fastapi_error(client: TestClient): 1abcde

27 with pytest.raises(FastAPIError) as exc_info: 1abcde

28 client.get("/items/portal-gun") 1abcde

29 assert "No response object was returned" in exc_info.value.args[0] 1abcde

30 

31 

32def test_internal_server_error(): 1abcde

33 from docs_src.dependencies.tutorial008c import app 1abcde

34 

35 client = TestClient(app, raise_server_exceptions=False) 1abcde

36 response = client.get("/items/portal-gun") 1abcde

37 assert response.status_code == 500, response.text 1abcde

38 assert response.text == "Internal Server Error" 1abcde