Coverage for tests / test_empty_router.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import pytest 1abcd

2from fastapi import APIRouter, FastAPI 1abcd

3from fastapi.exceptions import FastAPIError 1abcd

4from fastapi.testclient import TestClient 1abcd

5 

6app = FastAPI() 1abcd

7 

8router = APIRouter() 1abcd

9 

10 

11@router.get("") 1abcd

12def get_empty(): 1abcd

13 return ["OK"] 1efgh

14 

15 

16app.include_router(router, prefix="/prefix") 1abcd

17 

18 

19client = TestClient(app) 1abcd

20 

21 

22def test_use_empty(): 1abcd

23 with client: 1efgh

24 response = client.get("/prefix") 1efgh

25 assert response.status_code == 200, response.text 1efgh

26 assert response.json() == ["OK"] 1efgh

27 

28 response = client.get("/prefix/") 1efgh

29 assert response.status_code == 200, response.text 1efgh

30 assert response.json() == ["OK"] 1efgh

31 

32 

33def test_include_empty(): 1abcd

34 # if both include and router.path are empty - it should raise exception 

35 with pytest.raises(FastAPIError): 1ijkl

36 app.include_router(router) 1ijkl