本文档描述了TheFuck项目使用的测试框架,解释了如何运行现有测试和编写新测试。它涵盖了测试架构、测试类型以及如何执行它们。有关添加新规则到TheFuck的信息,请参阅添加新规则。
TheFuck使用pytest作为其主要的测试框架,并使用tox进行跨多个Python版本的测试自动化。测试系统旨在通过单元测试和功能测试来验证跨不同Shell环境的修复。
来源: tox.ini1-7 tests/functional/test_bash.py1-4 tests/functional/test_zsh.py1-4 tests/functional/test_fish.py1-3 tests/functional/test_tcsh.py1-3
TheFuck 使用 tox 来跨多个 Python 版本运行测试。该项目已配置为针对 Python 2.7 到 3.11 进行测试。
运行所有支持的 Python 版本的所有测试
运行特定 Python 版本的测试
为了更快的开发周期,您可以直接运行 pytest
运行特定的测试文件
来源: tox.ini1-7
功能测试验证TheFuck在真实Shell环境中的行为。这些测试使用Docker容器为不同的Shell创建隔离的测试环境。
来源: tests/functional/test_bash.py25-36 tests/functional/test_zsh.py23-34 tests/functional/test_fish.py9-14 tests/functional/test_tcsh.py9-15
每个Shell都有一个特定的测试设置文件,该文件
例如,bash测试设置
来源: tests/functional/test_bash.py25-36
功能测试验证常见场景
| 测试场景 | 描述 |
|---|---|
test_with_confirmation | 测试启用确认时的命令修复 |
test_without_confirmation | 测试禁用确认时的命令修复 |
test_refuse_with_confirmation | 测试在启用确认时拒绝修复 |
test_select_command_with_arrows | 测试使用箭头键从多个修复选项中选择 |
test_how_to_configure_alias | 测试未配置别名时显示的说明 |
来源: tests/functional/test_bash.py39-66 tests/functional/test_zsh.py37-64 tests/functional/test_fish.py17-34 tests/functional/test_tcsh.py18-35
test_plots 模块包含功能测试的可重用测试模式。这些函数封装了在不同Shell测试中使用的常见测试场景。
来源: tests/functional/plots.py1-88
关键测试 Plot 函数
_set_confirmation: 配置TheFuck要求或跳过确认with_confirmation: 测试启用确认时的修复without_confirmation: 测试禁用确认时的修复refuse_with_confirmation: 测试拒绝修复select_command_with_arrows: 测试使用箭头键选择修复history_changed: 验证Shell历史记录是否已更新history_not_changed: 验证Shell历史记录是否未更改how_to_configure: 测试配置说明来源: tests/functional/plots.py1-88
要为特定规则或功能添加单元测试
tests/rules/test_new_rule.py)要添加新的功能测试场景
tests/functional/plots.py 添加新函数@pytest.mark.functional 标记测试添加新功能测试的示例
来源: tests/functional/test_bash.py39-66 tests/functional/plots.py8-21
TheFuck 的功能测试使用 Docker 容器来提供隔离的 Shell 环境。容器使用特定的标签定义
| 容器 | 描述 |
|---|---|
thefuck/python3 | 包含 Python 3 的容器 |
thefuck/python2 | 包含 Python 2 的容器 |
这些容器配置了不同的 Shell
每个测试文件都会启动相应的容器并在运行测试之前初始化Shell环境。
来源: tests/functional/test_bash.py7-13 tests/functional/test_zsh.py7-8 tests/functional/test_fish.py5-6 tests/functional/test_tcsh.py5-6
TheFuck 使用 tox 进行持续集成测试。 tox.ini 文件定义了测试环境和命令。
此配置确保测试在多个 Python 版本(2.7 至 3.11)上运行。
来源: tox.ini1-7
编写 TheFuck 的测试时,请遵循以下最佳实践
@pytest.mark.functional来源: tests/functional/plots.py8-83 tests/functional/test_bash.py39-66 tests/functional/test_zsh.py37-64