Skip to content

Commit 0375dba

Browse files
JonZeollaclaude
andcommitted
fix: update tests for argparse in main.py
Mock sys.argv in test_main_function to avoid pytest arg conflicts. Add test_main_version and test_main_as_script_version to verify --version works both in-process and as a subprocess. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6cde0d9 commit 0375dba

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

  • {{cookiecutter.project_name|replace(" ", "")}}/tests

{{cookiecutter.project_name|replace(" ", "")}}/tests/test_main.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,20 @@ def test_main_function():
2222
"""Test that main() raises NotImplementedError"""
2323
from main import main
2424

25-
# Mock the argument parsing to avoid conflicts with pytest args
26-
with patch("{{ cookiecutter.project_slug }}.config.get_args_config") as mock_args:
27-
import logging
25+
with patch("sys.argv", ["main"]):
26+
with pytest.raises(NotImplementedError):
27+
main()
2828

29-
mock_args.return_value = {"loglevel": logging.WARNING}
3029

31-
with pytest.raises(NotImplementedError):
30+
@pytest.mark.unit
31+
def test_main_version():
32+
"""Test that --version prints the version and exits"""
33+
from main import main
34+
35+
with patch("sys.argv", ["main", "--version"]):
36+
with pytest.raises(SystemExit) as exc_info:
3237
main()
38+
assert exc_info.value.code == 0
3339

3440

3541
@pytest.mark.unit
@@ -46,3 +52,18 @@ def test_main_as_script():
4652
# Should exit with code 1 due to NotImplementedError
4753
assert result.returncode == 1
4854
assert "NotImplementedError" in result.stderr
55+
56+
57+
@pytest.mark.unit
58+
def test_main_as_script_version():
59+
"""Test that --version works when run as a script"""
60+
main_path = Path(__file__).parent.parent / "src" / "main.py"
61+
62+
result = subprocess.run(
63+
[sys.executable, str(main_path), "--version"],
64+
capture_output=True,
65+
text=True,
66+
)
67+
68+
assert result.returncode == 0
69+
assert "0.0.0" in result.stdout

0 commit comments

Comments
 (0)