【问题标题】:How to take data from a pytest function(monekypath)如何从 pytest 函数中获取数据(monekypath)
【发布时间】:2021-09-15 13:31:44
【问题描述】:

我开发了一个包含许多功能的程序。我现在正在测试这些功能。为此,我需要使用猴子路径方法,因为测试的函数调用输入。

因为要测试函数我需要获取之前测试的数据,用monkeypath函数我发现了困难。

data_to_test_1 = test_load_sample() # I take the data from the previous test here

# SECOND TEST
def test_take_sample(monkeypatch):
    '''
    take_sample() requests the name of the column
    and take that input to split the data in new columns
    This can be tested by checking some ofthe first values of that
    columns (GT:AD:DP:GQ:PL)
    monkeypatch simulate the input of the user
    '''
    monkeypatch.setattr('builtins.input', lambda _: "373978487") # The 9th sample in the file
    data_to_test_2 = take_sample(data_to_test_1,NAME_FILE_1)
    return data_to_test_2
    assert data_to_test_2["GT"] == "0/1"     # What I test
    assert data_to_test_2["AD"] == "28,46"
    assert data_to_test_2["DP"] == "74:99"


# Now, I want the output of the test_take_sample()
data_to_test_3 = test_take_sample() 

def test_filter_1():
   ... # this function will use data_to_test_3

我在之前的函数中采用了相同的方法将数据从一个测试连接到下一个测试,但这涉及到我得到的猴子路径

test_take_sample() missing 1 required positional argument: 'monkeypatch'

【问题讨论】:

    标签: python pytest monkeypatching


    【解决方案1】:

    对于第一种情况,我认为函数 test_load_sample() 不需要参数。所以你为函数'test_load_sample()'的值分配'data_to_test_1'

    如果你做 data_to_test_1 = test_load_sample() data_to_test_1 接收 test_load_sample() 的返回值而不是函数本身

    我认为您认为您正在为 data_to_test_1 分配功能,但事实并非如此。你只需分配函数的值。

    如果要将函数分配给某个变量,请使用 'class' 类型

    【讨论】:

    • 是的,你是对的。 data_to_test_1 接收返回值。在每一步中,我都需要上一个测试的值。在以前的测试中它有效,但现在在我使用金钱路径的测试中,我不知道出了什么问题
    • 在 data_to_test_3 我需要返回 test_take_sample()
    • 我不想将函数分配给某个变量
    • 嗯...我无法完全理解您的问题。我以为您编写代码是因为您想使用类类型,但您只需要函数的值。如果您看到错误代码,“test_take_sample() 缺少 1 个必需的位置参数:'monkeypatch'”表示您需要将参数 'monkeypatch' 插入 test_take_sample。不是问题吗?像; hahapatch = 'asdfasdf'; data_to_test_1 = test_load_sample(hahapatch);
    【解决方案2】:

    我已经解决了这个问题

    data_to_test_2,Name_sample = take_sample(data_to_test_1,NAME_FILE_1)
    
    

    我已经跳过了测试 test_take_sample() 并且我刚刚运行了函数 take_sample。这询问了我的输入,我已经介绍了在测试运行时添加 -s 的输入:pythest test_1.py -s

    【讨论】:

      猜你喜欢
      • 2015-05-29
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多