python - pytest - 将值从一个测试函数传递到测试文件中的另一个

标签 python pytest fixtures

问题陈述 : 我想将一个函数返回的值从我的测试文件传递给另一个函数。

test_abc.py

@pytest.fixture(scope='function')
def hold_value():
    def _value(resp):
        return resp
    return _value


@when(parsers.cfparse('user did something with value "{a}" and "{b}" and “{c}"'))
def func1(hold_value, a, b, c):
    response = do_something(a,b,c)
    hold_value(response)

@then('user validate if above step is correct')
def func2(hold_value):
    call_another_func_and_pass_above_response(hold_value)=> what can I do here to get value from fixture

这是我尝试创建 fixture 以保存一个函数返回的值的测试文件,然后在其他函数中使用它

我不确定,如果这样做是正确的,我想将值从一个测试步骤传递到另一个。

谁能指导我达到预期的结果

最佳答案

我能够通过使用上下文作为固定装置来实现解决方案

@pytest.fixture(scope='function')
def context():
      return {}


@when(parsers.cfparse('user did something with value "{a}" and "{b}" and “{c}"'))
def func1(context, a, b, c):
    response = do_something(a,b,c)
    context[‘response’] = response

@then('user validate if above step is correct')
def func2(context):
    call_another_func_and_pass_above_response(context.get(‘response’))

关于python - pytest - 将值从一个测试函数传递到测试文件中的另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61622565/

相关文章:

spring - Grails:将 FixtureLoader 注入(inject)规范

python - 如何在同一个列表中有两种不同的排序顺序?

python - 将鼠标事件函数与 tensorflow 一起用于对象检测时列出索引超出范围错误

python - pytest 获取当前测试文件的文件路径

python - pytest 模拟 os.listdir 返回空列表

php - 有没有办法只为给定的 PHPUnit 测试加载特定的装置?

Symfony2 - Doctrine fixtures createdAt 比实际时间提前一个小时出现

python - Flask 返回 404 触发异常 block 并且不返回 Not Found

python - 无法安装 Geopandas

python - py.test 在捕捉到预期的异常后挂起