首页 > 解决方案 > pytest 函数不返回要在同一 pytest 文件中的以下函数中使用的数据?

问题描述

我开发了一个由几个函数组成的程序(file.py),现在我想从 test_file.py 测试每个函数。我已经正确测试了第一个函数,但是当我想测试第二个函数时,我注意到我需要从第一个函数生成的数据。做这个的最好方式是什么?

我的第一次参加是这个

def test_load_sample():  # The first test for the first function
    '''Verify all rows of the body of the vcf file are taken'''
    data_to_test = load_sample (NAME_FILE_1) # load_sample() is the first function of file.py
    return data_to_test     # I need this data for the following tests
    assert len(data_to_test) == 1400
    

data_to_test = data_to_test

因为我希望函数 test_load_sample 会返回我想要的第二次测试的数据。但是,似乎 pytest 函数没有返回已测试的内容?

做这个的最好方式是什么?难道我做错了什么?这是我第一次使用 pytest。

编辑

# File.py

input_data = 1

function_1(data):
return data +1

data1 = function_1(input_data)

# Now input data is 2

function_2(data):
return data +1

data2 = function_2(data1)
...

现在我想测试函数来测试function_2

test_function_1():
data_to_test = function_1(data_input)
assert data_to_test = 2

test_function_2():
# I need data_to_test from the previous function

标签: pythonpytest

解决方案


您所做的不是惯例(可能是反模式)。您应该考虑的几点:

  • 测试函数并不意味着返回任何值。它们仅用于运行源代码并对结果执行断言,仅此而已。
  • 每个测试都应该被设计成相互隔离,如果 test1 失败,这并不意味着 test2 到 test10 也会失败。
  • 此外,需要这种隔离,因为根据运行测试的方式,它们可能会并行(同时)执行而不是顺序执行。因此,如果有多次 test2 先执行,则使 test2 依赖于 test1 将失败。

如果您有想要在多个测试用例之间共享的公共资源,请在测试中对其进行配置。因此,如果 的输出load_sample(NAME_FILE_1)将在多个测试中使用,并且您不希望它为每个测试用例重新执行,您可能希望使用带有session scope的 pytest 固定装置。

会话:夹具在测试会话结束时被销毁。

所以对于这个测试:

import pytest


@pytest.fixture(scope="function")
def my_funcion_fixture():
    print("Function fixture initialized")
    return 123


@pytest.fixture(scope="session")
def my_session_fixture():
    print("Session fixture initialized")
    return 456
    # Ideally, this is where you put <data_to_test = load_sample (NAME_FILE_1)> and return the <data_to_test>


def test_first(my_funcion_fixture, my_session_fixture):
    assert my_funcion_fixture == 123
    assert my_session_fixture == 456


def test_second(my_funcion_fixture, my_session_fixture):
    assert my_funcion_fixture == 123
    assert my_session_fixture == 456


def test_third(my_funcion_fixture, my_session_fixture):
    assert my_funcion_fixture == 123
    assert my_session_fixture == 456


def test_fourth(my_funcion_fixture, my_session_fixture):
    assert my_funcion_fixture == 123
    assert my_session_fixture == 456

输出

$ pytest -q -rP
....
[100%]
================================================================================================= PASSES ==================================================================================================
_______________________________________________________________________________________________ test_first ________________________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout setup ------------------------------------------------------------------------------------------
Session fixture initialized
Function fixture initialized
_______________________________________________________________________________________________ test_second _______________________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout setup ------------------------------------------------------------------------------------------
Function fixture initialized
_______________________________________________________________________________________________ test_third ________________________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout setup ------------------------------------------------------------------------------------------
Function fixture initialized
_______________________________________________________________________________________________ test_fourth _______________________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout setup ------------------------------------------------------------------------------------------
Function fixture initialized
4 passed in 0.07s
  • 如您所见,我们能够在所有测试中使用所有的夹具。唯一的区别是我们的会话夹具my_session_fixture只实例化了一次,并且没有为每个测试用例重新执行。

推荐阅读