Coverage for tests / test_fastapi_cli.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import os 1abcd

2import subprocess 1abcd

3import sys 1abcd

4from unittest.mock import patch 1abcd

5 

6import fastapi.cli 1abcd

7import pytest 1abcd

8 

9 

10def test_fastapi_cli(): 1abcd

11 result = subprocess.run( 1hij

12 [ 

13 sys.executable, 

14 "-m", 

15 "coverage", 

16 "run", 

17 "-m", 

18 "fastapi", 

19 "dev", 

20 "non_existent_file.py", 

21 ], 

22 capture_output=True, 

23 encoding="utf-8", 

24 env={**os.environ, "PYTHONIOENCODING": "utf-8"}, 

25 ) 

26 assert result.returncode == 1, result.stdout 1hij

27 assert "Path does not exist non_existent_file.py" in result.stdout 1hij

28 

29 

30def test_fastapi_cli_not_installed(): 1abcd

31 with patch.object(fastapi.cli, "cli_main", None): 1efg

32 with pytest.raises(RuntimeError) as exc_info: 1efg

33 fastapi.cli.main() 1efg

34 assert "To use the fastapi command, please install" in str(exc_info.value) 1efg