pytest中yaml文件用法
时间: 2023-10-04 12:05:56 浏览: 149
在pytest中,可以使用yaml文件来组织测试数据,并使用pyyaml库来读取和解析yaml文件。
以下是使用yaml文件的示例:
1. 安装pyyaml库:
```python
pip install pyyaml
```
2. 创建一个yaml文件,例如data.yaml:
```yaml
# 测试数据
test_data:
- name: test1
input: 1
expected_output: 2
- name: test2
input: 2
expected_output: 4
```
3. 在pytest测试文件中,使用pyyaml库读取yaml文件:
```python
import yaml
def test_yaml_data():
# 读取yaml文件
with open("data.yaml", encoding="utf-8") as f:
data = yaml.safe_load(f)
# 遍历测试数据
for test_case in data["test_data"]:
input_data = test_case["input"]
expected_output = test_case["expected_output"]
# 执行测试
assert input_data * 2 == expected_output
```
在上述示例中,我们使用yaml文件中的测试数据来执行测试,并使用assert语句来验证测试结果是否符合预期。
阅读全文
相关推荐


















