@pytest.mark.parametrize("caseinfo", YamlUtil('./requestdata/login.yaml').read_yaml()) 这样写对吗
时间: 2023-05-22 17:01:07 浏览: 73
这是一个使用 pytest.mark.parametrize 装饰器的 Python 代码片段,它从 './requestdata/login.yaml' 文件中读取 YAML 格式的数据并将其传递给测试用例。根据提供的信息,这段代码看起来是正确的。
相关问题
@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` 才能正常使用。
@pytest.mark.parametrize和@pytest.fixture函数可以一起用吗
可以一起使用。`@pytest.mark.parametrize`是用于参数化测试用例的装饰器,而`@pytest.fixture`则是用于定义测试用例依赖的装饰器。通过将`@pytest.fixture`与`@pytest.mark.parametrize`一起使用,可以在测试用例中使用多个参数化的fixture来定义测试用例的依赖关系。例如:
```python
import pytest
@pytest.fixture
def my_fixture(request):
param = request.param
return f"Hello {param}!"
@pytest.mark.parametrize("param", ["World", "Pytest"])
def test_my_test(my_fixture):
assert my_fixture == "Hello World!" or my_fixture == "Hello Pytest!"
```
在上面的例子中,`@pytest.fixture`定义了一个名为`my_fixture`的fixture,它接受一个参数`request.param`,并返回一个字符串。`@pytest.mark.parametrize`则定义了一个名为`param`的参数化参数,它的取值分别是`["World", "Pytest"]`。在测试用例`test_my_test`中,`my_fixture`作为参数传入,pytest会将`param`的取值分别传入`my_fixture`中,执行测试用例并进行断言。