Coverage for tests / test_tutorial / test_arguments / test_envvar / test_tutorial001.py: 100%

44 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-02-09 12:36 +0000

1import importlib 1abcdefgh

2import subprocess 1abcdefgh

3import sys 1abcdefgh

4from types import ModuleType 1abcdefgh

5 

6import pytest 1abcdefgh

7import typer 1abcdefgh

8import typer.core 1abcdefgh

9from typer.testing import CliRunner 1abcdefgh

10 

11runner = CliRunner() 1abcdefgh

12 

13 

14@pytest.fixture( 1abcdefgh

15 name="mod", 

16 params=[ 

17 pytest.param("tutorial001_py39"), 

18 pytest.param("tutorial001_an_py39"), 

19 ], 

20) 

21def get_mod(request: pytest.FixtureRequest) -> ModuleType: 1abcdefgh

22 module_name = f"docs_src.arguments.envvar.{request.param}" 1abcdefgh

23 mod = importlib.import_module(module_name) 1abcdefgh

24 return mod 1abcdefgh

25 

26 

27def test_help(mod: ModuleType): 1abcdefgh

28 result = runner.invoke(mod.app, ["--help"]) 1abcdefgh

29 assert result.exit_code == 0 1abcdefgh

30 assert "[OPTIONS] [NAME]" in result.output 1abcdefgh

31 assert "Arguments" in result.output 1abcdefgh

32 assert "env var: AWESOME_NAME" in result.output 1abcdefgh

33 assert "default: World" in result.output 1abcdefgh

34 

35 

36def test_help_no_rich(monkeypatch: pytest.MonkeyPatch, mod: ModuleType): 1abcdefgh

37 monkeypatch.setattr(typer.core, "HAS_RICH", False) 1abcdefgh

38 result = runner.invoke(mod.app, ["--help"]) 1abcdefgh

39 assert result.exit_code == 0 1abcdefgh

40 assert "[OPTIONS] [NAME]" in result.output 1abcdefgh

41 assert "Arguments" in result.output 1abcdefgh

42 assert "env var: AWESOME_NAME" in result.output 1abcdefgh

43 assert "default: World" in result.output 1abcdefgh

44 

45 

46def test_call_arg(mod: ModuleType): 1abcdefgh

47 result = runner.invoke(mod.app, ["Wednesday"]) 1abcdefgh

48 assert result.exit_code == 0 1abcdefgh

49 assert "Hello Mr. Wednesday" in result.output 1abcdefgh

50 

51 

52def test_call_env_var(mod: ModuleType): 1abcdefgh

53 result = runner.invoke(mod.app, env={"AWESOME_NAME": "Wednesday"}) 1abcdefgh

54 assert result.exit_code == 0 1abcdefgh

55 assert "Hello Mr. Wednesday" in result.output 1abcdefgh

56 

57 

58def test_call_env_var_arg(mod: ModuleType): 1abcdefgh

59 result = runner.invoke(mod.app, ["Czernobog"], env={"AWESOME_NAME": "Wednesday"}) 1abcdefgh

60 assert result.exit_code == 0 1abcdefgh

61 assert "Hello Mr. Czernobog" in result.output 1abcdefgh

62 

63 

64def test_script(mod: ModuleType): 1abcdefgh

65 result = subprocess.run( 1abcdefgh

66 [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], 

67 capture_output=True, 

68 encoding="utf-8", 

69 ) 

70 assert "Usage" in result.stdout 1abcdefgh