【问题标题】:pytest - pass value from one test function to another in test filepytest - 将值从一个测试函数传递到测试文件中的另一个函数
【发布时间】:2020-08-20 16:04:49
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: python pytest fixtures


    【解决方案1】:

    我能够通过使用上下文作为夹具来实现解决方案

    @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’))
    

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 2020-09-04
      • 2014-10-21
      • 2016-03-01
      • 1970-01-01
      • 2020-09-26
      • 2021-08-14
      • 1970-01-01
      相关资源
      最近更新 更多