解释下面代码:import pytest @pytest.mark.parametrize('username, passwd', [('zz', '123456'), ('sf', '123456'), ('cf', '123456')]) def test01(username, passwd): print('\n' + username) print(passwd) if __name__ == '__main__': pytest.main(['-s', 'test_multi_param.py'])
时间: 2023-08-20 18:42:11 浏览: 45
这段代码是一个使用 Pytest 测试框架编写的测试函数,实现了对多组数据进行参数化测试,但是与前面两个示例不同之处在于参数化的方式。
使用 pytest.mark.parametrize 装饰器,将多组参数作为元组传递给 test01 函数。这样,test01 函数将被执行三次,每次使用元组中的一个值作为 username 参数,另一个值作为 passwd 参数。
在 test01 函数中,使用 print 语句输出 username 和 passwd 变量的值。
总之,这段代码同样实现了对多组数据进行参数化测试,但是使用了不同的参数传递方式。需要注意的是,这种方式适用于参数比较少的情况,如果参数较多,建议使用前面两个示例中的方式进行参数化测试。
相关问题
解释下面代码:import pytest @pytest.mark.parametrize('a', ['aaa', 'bbb', 'ccc']) def test01(a): print('\n' + a) if __name__ == '__main__': pytest.main(['-s', 'test_single_param.py'])
这段代码是使用了 `pytest` 框架进行单参数化测试的例子。
首先,通过 `import pytest` 导入 `pytest` 模块。
然后,使用 `@pytest.mark.parametrize` 装饰器来指定参数化的参数。在本例中,参数化的参数是一个字符串列表 `['aaa', 'bbb', 'ccc']`,而且只有一个参数。
接下来,定义了一个测试函数 `test01`,该函数接收一个参数 `a`,用于接收由 `@pytest.mark.parametrize` 装饰器指定的参数化参数。在本例中,该参数是字符串类型的。在函数内部,使用 `print` 函数输出参数 `a`。
最后,在 `if __name__ == '__main__':` 语句块中,调用 `pytest.main` 函数来运行测试用例。其中 `'-s'` 参数是为了显示 `print` 输出的内容,`'test_single_param.py'` 是指定要运行的测试用例文件名。
当运行该测试用例时,pytest 会将参数列表中的每个参数分别传入函数 `test01`,并执行该函数。因此,该测试用例会运行三次,并依次打印参数 `'aaa'`、`'bbb'` 和 `'ccc'`。
@pytest.mark.parametrize 或者 @pytest.mark.allure.step用法
`@pytest.mark.parametrize` 是 pytest 提供的一个装饰器,可以用于参数化测试用例。它可以让我们在测试用例中使用多组参数来执行同一个测试用例,从而减少测试用例的编写量,提高效率。
示例代码:
```python
import pytest
@pytest.mark.parametrize("input, expected_output", [
(1, 2),
(2, 3),
(3, 4),
(4, 5),
])
def test_increment(input, expected_output):
assert input+1 == expected_output
```
`@pytest.mark.allure.step` 也是 pytest 提供的一个装饰器,用于生成测试报告。它可以将测试用例中的每个步骤作为报告的一个单独的步骤展示,从而更直观地展示测试用例的执行情况。
示例代码:
```python
import pytest
import allure
@allure.step("输入用户名和密码")
def input_username_and_password(username, password):
pass
@allure.step("点击登录按钮")
def click_login_button():
pass
@allure.step("验证登录成功")
def verify_login_success():
pass
def test_login():
input_username_and_password("testuser", "testpass")
click_login_button()
verify_login_success()
```
以上是 `@pytest.mark.parametrize` 和 `@pytest.mark.allure.step` 的基本用法。需要注意的是,`@pytest.mark.allure.step` 需要安装 `pytest-allure-adaptor` 才能正常使用。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)