那你这里@pytest.fixture 装饰器放在 @pytest.mark.parametrize 装饰器的上面。
时间: 2023-07-06 12:27:32 浏览: 54
非常抱歉,我的回答有误。`@pytest.fixture` 装饰器应该放在 `@pytest.mark.parametrize` 装饰器的下面,因为 fixture 函数需要接收被 parametrize 装饰器传入的参数。正确的写法应该是这样的:
```
import pytest
@pytest.fixture(scope='function', autouse=True)
def my_fixture(request):
# do something
my_param = request.param
# do something
yield
# do something after the test
@pytest.mark.parametrize("my_param", ["value1", "value2"])
def test_func(my_param):
# do something with my_param
assert True
```
在这个例子中,`my_fixture` 函数接收 `request` 参数,并从中获取 `my_param` 参数的取值。`test_func` 函数通过 `@pytest.mark.parametrize` 装饰器指定 `my_param` 参数的不同取值,这些取值会传递给 `my_fixture` 函数。在 `my_fixture` 函数中,我们可以使用 `my_param` 参数的具体取值来执行一些操作。注意,`my_fixture` 函数必须使用 `yield` 语句来分隔 setup 和 teardown 阶段,以确保在每个测试用例执行前后执行指定的操作。