举个pytest.mark.parametrize的例子
时间: 2023-06-29 09:13:47 浏览: 50
好的,下面是一个使用pytest.mark.parametrize的例子:
```python
import pytest
@pytest.mark.parametrize("test_input, expected_output", [("3+5", 8), ("2+4", 6), ("6*9", 54)])
def test_eval(test_input, expected_output):
assert eval(test_input) == expected_output
```
在这个例子中,我们使用pytest.mark.parametrize装饰器来为一个测试函数提供多组测试数据。每组测试数据都是一个包含两个元素的元组,第一个元素是测试输入,第二个元素是期望输出。在测试函数中,我们使用pytest提供的assert语句来比较实际输出和期望输出是否相等。测试框架会自动运行所有测试数据,并生成测试报告。
相关问题
@pytest.mark.parametrize和@pytest.fixture区别
`@pytest.mark.parametrize`和`@pytest.fixture`是两个在pytest测试框架中经常使用的装饰器,它们的作用不同。
`@pytest.mark.parametrize`用于参数化测试用例,可以将多组参数传递给同一个测试用例函数,从而减少代码量,提高测试效率。
`@pytest.fixture`用于创建测试用例函数的前置条件,可以在测试用例函数执行之前完成一些准备工作,例如创建测试数据、连接数据库等。
下面是一个简单的例子,演示了如何使用这两个装饰器:
```python
import pytest
# 使用@pytest.fixture创建一个前置条件
@pytest.fixture
def prepare_data():
data = [1, 2, 3, 4, 5]
return data
# 使用@pytest.mark.parametrize参数化测试用例
@pytest.mark.parametrize("test_input, expected_output", [
(1, 2),
(2, 4),
(3, 6),
(4, 8),
(5, 10)
])
def test_multiply(prepare_data, test_input, expected_output):
# 在测试用例函数中使用前置条件
result = [x * test_input for x in prepare_data]
assert result == [x * expected_output for x in prepare_data]
```
在上面的例子中,`prepare_data`是一个前置条件,它返回一个列表。`test_multiply`是一个测试用例函数,使用了`@pytest.mark.parametrize`装饰器,将多组参数传递给同一个测试用例函数。在测试用例函数中,使用了`prepare_data`前置条件,对参数化后的测试用例进行了测试。
@pytest.fixture、@pytest.mark.parametrize的区别?
`@pytest.fixture`和`@pytest.mark.parametrize`是pytest中的两个常用装饰器,用于测试用例的编写和组织。
`@pytest.fixture`装饰器用于定义测试用例执行前需要进行的准备工作,也就是测试夹具(test fixture)。夹具可以是创建、配置或者初始化测试用例所需的对象、数据或环境等。通过夹具,我们可以确保在每个测试用例执行前都有一致的环境和数据状态。
举个例子,假设我们有一个测试用例需要访问数据库,那么可以使用`@pytest.fixture`装饰器来创建一个数据库连接夹具,在每个测试用例执行前都创建一个连接,在用例执行后关闭连接,以保证每个测试用例都有独立的数据库连接。
`@pytest.mark.parametrize`装饰器用于参数化测试用例。参数化是一种有效的测试用例设计方法,它允许我们通过给定不同的参数组合来运行相同的测试逻辑,从而减少代码重复并增加测试覆盖性。使用`@pytest.mark.parametrize`装饰器,我们可以将测试用例定义为一个参数化的函数,并指定不同的参数组合。
举个例子,假设我们有一个加法函数`add(a, b)`,可以使用`@pytest.mark.parametrize`装饰器来定义一个参数化测试用例,传入不同的参数组合来验证加法函数的正确性,例如测试用例可以是`(1, 2, 3)`、`(0, 0, 0)`、`(-1, 1, 0)`等。
总结来说,`@pytest.fixture`用于准备测试用例执行前的环境和数据,而`@pytest.mark.parametrize`用于参数化测试用例。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)