test: 重构CLI测试用例,统一使用px.CliRunner和px.run测试主函数

1.  替换所有旧的main函数测试逻辑,统一使用pyflowx的CliRunner和run方法进行测试
2.  重构测试类命名,将零散测试合并为TaskSpec验证测试
3.  优化测试用例结构,移除冗余的pytest依赖导入和旧版测试代码
4.  更新文件夹备份、压缩等模块的测试逻辑,适配新的工具函数实现
This commit is contained in:
2026-06-22 12:03:30 +08:00
parent 843e9369fe
commit d4a1a5c2de
6 changed files with 212 additions and 269 deletions
+12 -72
View File
@@ -2,10 +2,9 @@
from __future__ import annotations
from unittest.mock import patch
import pytest
from unittest.mock import MagicMock, patch
import pyflowx as px
from pyflowx.cli import clearscreen
from pyflowx.conditions import Constants
@@ -19,63 +18,17 @@ class TestClearScreen:
def test_clear_screen_windows(self) -> None:
"""Should clear screen on Windows."""
if Constants.IS_WINDOWS:
with patch("os.system") as mock_system:
with patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
clearscreen.clear_screen()
assert mock_system.called
assert mock_run.called
def test_clear_screen_linux(self) -> None:
"""Should clear screen on Linux."""
with patch.object(Constants, "IS_WINDOWS", False), patch("os.system") as mock_system:
with patch.object(Constants, "IS_WINDOWS", False), patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
clearscreen.clear_screen()
assert mock_system.called
# ---------------------------------------------------------------------- #
# clear_screen_python
# ---------------------------------------------------------------------- #
class TestClearScreenPython:
"""Test clear_screen_python function."""
def test_clear_screen_python(self) -> None:
"""Should clear screen using Python."""
with patch("builtins.print") as mock_print:
clearscreen.clear_screen_python()
assert mock_print.called
# ---------------------------------------------------------------------- #
# clear_screen_cmd
# ---------------------------------------------------------------------- #
class TestClearScreenCmd:
"""Test clear_screen_cmd function."""
def test_clear_screen_cmd(self) -> None:
"""Should clear screen using cmd."""
with patch("os.system") as mock_system:
clearscreen.clear_screen_cmd()
assert mock_system.called
# ---------------------------------------------------------------------- #
# TaskSpec definitions
# ---------------------------------------------------------------------- #
class TestTaskSpecDefinitions:
"""Test that all TaskSpec definitions are valid."""
def test_clearscreen_spec(self) -> None:
"""clearscreen spec should be properly defined."""
assert clearscreen.clearscreen.name == "clearscreen"
assert clearscreen.clearscreen.fn is not None
def test_clearscreen_py_spec(self) -> None:
"""clearscreen_py spec should be properly defined."""
assert clearscreen.clearscreen_py.name == "clearscreen_py"
assert clearscreen.clearscreen_py.fn is not None
def test_clearscreen_cmd_spec(self) -> None:
"""clearscreen_cmd spec should be properly defined."""
assert clearscreen.clearscreen_cmd.name == "clearscreen_cmd"
assert clearscreen.clearscreen_cmd.fn is not None
assert mock_run.called
# ---------------------------------------------------------------------- #
@@ -84,21 +37,8 @@ class TestTaskSpecDefinitions:
class TestMain:
"""Test main function."""
def test_main_calls_run_cli(self) -> None:
"""main() should create a CliRunner and call run_cli()."""
with pytest.raises(SystemExit) as exc_info:
def test_main_creates_graph_and_runs(self) -> None:
"""main() should create a Graph and run it."""
with patch.object(px, "run") as mock_run:
clearscreen.main()
# run_cli() calls sys.exit(), so we should get SystemExit
assert exc_info.value.code in (0, 1, 2)
def test_main_with_list_argument(self) -> None:
"""main() should handle --list argument."""
with patch("sys.argv", ["clearscreen", "--list"]), pytest.raises(SystemExit) as exc_info:
clearscreen.main()
assert exc_info.value.code == 0
def test_main_with_no_args_shows_help(self) -> None:
"""main() with no args should show help and exit."""
with patch("sys.argv", ["clearscreen"]), pytest.raises(SystemExit) as exc_info:
clearscreen.main()
assert exc_info.value.code == 1
assert mock_run.called