pytest夹具返回倍数

时间:2018-09-13 00:47:35

标签: python pytest

是否有可能返回字典和数据框的灯具?

import somefile
import pytest

@pytest.fixture()
def setup():
    dictionary, dataframe = somefile.get_Di_And_Df()
    return(dictionary, dataframe)

def test_check(setup):
    assert dictionary['movie']['action'] == 'Avengers'
    assert dataframe.shape[0] == 5

2 个答案:

答案 0 :(得分:1)

夹具的返回值(或屈服值)实际上是在测试执行期间作为函数参数注入的对象:

def test_check(setup):
    dictionary, dataframe = setup
    assert dictionary['movie']['action'] == 'Avengers'
    assert dataframe.shape[0] == 5

答案 1 :(得分:0)

您可以模拟被调用的方法

@patch("somefile.get_Di_And_Df", MagicMock=(return_value=(dictionary, dataframe)))
def test_check():
  _dict, _df = somefile.get_Di_And_Df()
  assert _dict['movie']['action'] == 'Avengers'
  assert _df.shape[0] == 5