Testing(测试)(续)
project_name/
├── app_name/
│ ├── __init__.py
│ ├── models.py
│ ├── services.py
│ ├── selectors.py
│ ├── apis.py
│ └── tests/
│ ├── __init__.py
│ ├── factories.py # Factory Boy 工厂定义
│ ├── models/
│ │ ├── __init__.py
│ │ └── test_some_model_name.py
│ ├── selectors/
│ │ ├── __init__.py
│ │ └── test_some_selector_name.py
│ └── services/
│ ├── __init__.py
│ └── test_some_service_name.py
└── __init__.py
📝 结构详解
tests/ 目录:
factories.py- 包含该应用所有模型的 Factory 定义models/- 模型测试selectors/- 选择器测试services/- 服务测试apis/- API 测试(可选,如果有的话)
组织原则:
- ✅ 按照代码类型组织测试
- ✅ 测试目录结构镜像主代码结构
- ✅ 每个测试文件对应一个被测试的对象
命名规范
我们遵循 2 个通用命名约定:
1. 测试文件名
格式:
test_the_name_of_the_thing_that_is_tested.py
示例:
# 被测试的服务
def a_very_neat_service(*args, **kwargs):
pass
对应的测试文件名: