pytest 函数不会 return 数据用于同一 pytest 文件中的后续函数?
pytest functions don't return data to be used in following functions in the same pytest file?
我开发了一个由几个函数组成的程序 (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 会 return 第二次测试所需的数据。不过pytest函数好像没有return测试过什么?
最好的方法是什么?难道我做错了什么?这是我第一次使用 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
我知道怎么做了
def test_load_sample():
'''Verify all rows of the body of the vcf file are taken'''
data_to_test = load_sample (NAME_FILE_1)
return data_to_test
assert len(data_to_test) == 1400
data_to_test = test_load_sample()
# Now, data_to_test can be used in the following test functions
你所做的不是惯例(可能是反模式)。您应该考虑的几点:
- 测试函数并不意味着 return 任何值。他们只是习惯了运行源代码并对结果进行断言,仅此而已。
- 每个测试都应设计为相互隔离,如果 test1 失败,并不意味着 test2 到 test10 也会失败。
- 此外,需要这种隔离,因为根据测试的方式 运行,它们可能会并行(同时)执行,而不是顺序执行。因此,如果有多次先执行 test2,则使 test2 依赖于 test1 将会失败。
如果您有想要在多个测试用例之间共享的公共资源,请在测试中配置它。因此,如果 load_sample(NAME_FILE_1)
的输出将用于多个测试,并且您不希望它为每个测试用例重新执行,您可能需要使用带有 session scope 的 pytest fixture。
session: the fixture is destroyed at the end of the test session.
所以对于这个测试:
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
- 如您所见,我们能够在所有测试中使用所有固定装置。唯一的区别是我们的会话 fixture
my_session_fixture
只被实例化一次,而不是为每个测试用例重新执行。
我开发了一个由几个函数组成的程序 (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 会 return 第二次测试所需的数据。不过pytest函数好像没有return测试过什么?
最好的方法是什么?难道我做错了什么?这是我第一次使用 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
我知道怎么做了
def test_load_sample():
'''Verify all rows of the body of the vcf file are taken'''
data_to_test = load_sample (NAME_FILE_1)
return data_to_test
assert len(data_to_test) == 1400
data_to_test = test_load_sample()
# Now, data_to_test can be used in the following test functions
你所做的不是惯例(可能是反模式)。您应该考虑的几点:
- 测试函数并不意味着 return 任何值。他们只是习惯了运行源代码并对结果进行断言,仅此而已。
- 每个测试都应设计为相互隔离,如果 test1 失败,并不意味着 test2 到 test10 也会失败。
- 此外,需要这种隔离,因为根据测试的方式 运行,它们可能会并行(同时)执行,而不是顺序执行。因此,如果有多次先执行 test2,则使 test2 依赖于 test1 将会失败。
如果您有想要在多个测试用例之间共享的公共资源,请在测试中配置它。因此,如果 load_sample(NAME_FILE_1)
的输出将用于多个测试,并且您不希望它为每个测试用例重新执行,您可能需要使用带有 session scope 的 pytest fixture。
session: the fixture is destroyed at the end of the test session.
所以对于这个测试:
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
- 如您所见,我们能够在所有测试中使用所有固定装置。唯一的区别是我们的会话 fixture
my_session_fixture
只被实例化一次,而不是为每个测试用例重新执行。