pytest.mark.parametrize与yaml
时间: 2023-04-30 22:06:09 浏览: 149
pytest.mark.parametrize和yaml可以用于测试数据的参数化。pytest.mark.parametrize可以将多个测试数据传递给测试用例进行测试,而yaml则是一种方便存储和管理测试数据的文件格式,可以更好地组织和维护测试用例。
相关问题
pytest.mark.parametrize与yaml如何使用
pytest.mark.parametrize 和 YAML 都可以用于参数化测试,下面分别介绍它们的使用方法:
1. pytest.mark.parametrize
`pytest.mark.parametrize` 可以用于为测试函数提供参数化输入,其语法如下:
```python
import pytest
@pytest.mark.parametrize("arg1, arg2, expected_output", [
(1, 2, 3),
(10, 20, 30),
(-1, 1, 0),
])
def test_addition(arg1, arg2, expected_output):
assert arg1 + arg2 == expected_output
```
在上面的例子中,`test_addition` 函数被参数化了三次,每次传入的参数分别是 `(1, 2, 3)`,`(10, 20, 30)` 和 `(-1, 1, 0)`。 pytest 会自动运行这个函数三次,并检查每次运行的结果是否符合预期。
2. YAML
YAML 是一种轻量级的数据序列化格式,可以用于存储测试数据。下面是一个使用 YAML 的例子:
```yaml
- name: addition_test
args:
arg1: 1
arg2: 2
expected_output: 3
- name: subtraction_test
args:
arg1: 10
arg2: 5
expected_output: 5
```
在上面的例子中,我们定义了两个测试用例,一个是加法测试,一个是减法测试。每个测试用例包含一个名称和一组参数,这些参数会被传递给测试函数进行测试。
在测试函数中,我们可以使用 PyYAML 库读取 YAML 文件,并将其转换为 Python 对象,然后遍历每个测试用例并运行测试函数。下面是一个示例代码:
```python
import yaml
import pytest
def load_test_data(file_path):
with open(file_path) as f:
test_data = yaml.safe_load(f)
return test_data
def test_addition():
test_data = load_test_data("test_data.yaml")
for test_case in test_data:
args = test_case["args"]
expected_output = args.pop("expected_output")
assert sum(args.values()) == expected_output
def test_subtraction():
test_data = load_test_data("test_data.yaml")
for test_case in test_data:
args = test_case["args"]
expected_output = args.pop("expected_output")
assert args["arg1"] - args["arg2"] == expected_output
```
在上面的代码中,`load_test_data` 函数用于读取 YAML 文件并将其转换为 Python 对象,`test_addition` 和 `test_subtraction` 函数分别用于测试加法和减法。在每个测试函数中,我们先调用 `load_test_data` 函数加载测试数据,然后遍历每个测试用例并运行测试函数。
pytest.mark.parametrize 文件
pytest.mark.parametrize是pytest框架中的一个装饰器,用于参数化测试用例。它可以将不同的参数传递给同一个测试函数,以便多次运行测试用例。在引用\[1\]和引用\[2\]中,示例展示了如何使用pytest.mark.parametrize来传递单个变量的参数,其中引用\[1\]使用元组作为参数,引用\[2\]使用列表作为参数。而在引用\[3\]中,展示了如何结合yaml数据使用pytest.mark.parametrize。首先,通过read_yaml函数将yaml文件中的数据读取出来,并将其转换为列表形式。然后,将这个列表作为参数传递给pytest.mark.parametrize装饰器,从而实现参数化测试用例。在这个示例中,参数名为"data",并且测试函数test_add_case4可以通过"data"来访问每个测试数据中的具体值。这样,pytest框架会根据参数化的设置,多次运行测试函数,每次传递不同的参数值。
#### 引用[.reference_title]
- *1* *2* [4.pytest parametrize()参数化](https://blog.csdn.net/lzmlc0109/article/details/125719941)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [@pytest.mark.parametrize用法(yaml和fixture结合使用)](https://blog.csdn.net/qq_42870572/article/details/128149867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]