from pytest import fixture
@fixture
def env():
return {"key1": "value1", "key2": "value2"}
def do_work(env):
print("working")
def test_0(env):
do_work(env)
def test_1(env):
env["key1"] = "new_value1"
do_work(env)
def test_2(env):
env["key2"] = "new_value3"
do_work(env)例如,我有test_1和test_2,它们执行相同的do_work,但在调用编辑夹具之前。如何使用参数化(或其他任何方法)来避免编写两个测试?我不能就这样使用fixture parametrization,因为test_0不需要参数化。
发布于 2022-07-13 15:12:05
您可以使用参数化,也可以检查如何将测试夹具参数化
@pytest.mark.parametrize("key, value", (
("key1", "new_value1"),
("key2", "new_value2")
))
def test_1(env, key, value):
env[key] = value
do_work(env)https://stackoverflow.com/questions/72968549
复制相似问题