import pytest

This commit is contained in:
2025-11-25 20:55:09 +09:00
parent 2a85c03253
commit d7f00408e5
6 changed files with 603 additions and 0 deletions
+88
View File
@@ -0,0 +1,88 @@
# 测试指南
## 运行测试
### 运行所有测试
```bash
pytest
```
### 运行特定测试文件
```bash
pytest tests/test_playlist_merge.py
```
### 运行特定测试类或函数
```bash
pytest tests/test_playlist_merge.py::TestPlaylistMergeLocalPriority
pytest tests/test_playlist_merge.py::TestPlaylistMergeLocalPriority::test_merge_local_priority
```
### 运行特定参数化测试用例
```bash
# 只运行 case1 的测试
pytest tests/test_playlist_merge.py -k "case_num-1"
```
### 查看详细输出
```bash
pytest -v # verbose
pytest -vv # more verbose
pytest -s # 显示 print 输出
```
### 查看测试覆盖率
```bash
pip install pytest-cov
pytest --cov=app --cov-report=html
```
然后在浏览器中打开 `htmlcov/index.html`
## 测试结构
- `test_playlist_merge.py` - 播放列表合并功能的单元测试
- `conftest.py` - pytest fixtures 和配置
- `pytest.ini` - pytest 配置文件
## 编写新测试
1.`tests/` 目录下创建 `test_*.py` 文件
2. 创建以 `Test` 开头的测试类
3. 编写以 `test_` 开头的测试函数
4. 使用 `assert` 进行断言
5. 使用 `@pytest.mark.parametrize` 进行参数化测试
示例:
```python
import pytest
class TestMyFeature:
def test_basic_functionality(self):
result = my_function(input_data)
assert result == expected_output
@pytest.mark.parametrize("input,expected", [
(1, 2),
(2, 4),
(3, 6),
])
def test_with_multiple_inputs(self, input, expected):
assert my_function(input) == expected
```
## 调试测试
### 在测试失败时进入调试器
```bash
pytest --pdb
```
### 只运行上次失败的测试
```bash
pytest --lf
```
### 先运行失败的测试
```bash
pytest --ff
```