如何测试多于输出行的函数



我想测试一个有多个输出行的函数。

我要测试的是函数的if。如果满足条件,它将打印一个字符串,并返回一个键和一个字典的值。

函数是这样的:

def targe_region(self, bed, chro, position):
"""
Tool 1: Look is position is covered by the Bed file input.
"""
for key, value in bed.items():
if key == chro:
for values in value:
if int(position) in range(values[0], values[1]):
print("Your position is covered by this bed file")
return print(key, values)
return "Your position is not covered by this bed file"

这是一个大脚本的一部分,它的输入已经添加到我下面显示的测试代码中。

def test_target_region():
"""
chr20    3    4    Neg2    0    -    127477031    127478198    0,0,255
chrX    1    50    Neg3    0    -    127478198    127479365    0,0,255
chrX    50    100    Pos5    0    +    127479365    127480532    255,0,0
"""
BED = Read_file("./test_bed_file_with_errors4.bed")
data_loaded = BED.load_data()
bed_data = Bed_tools(data_loaded)
user_input2 = "chrX:55"
chro = user_input2.split(":")[0]
position = user_input2.split(":")[1]
assert (
bed_data.targe_region(data_loaded, chro, position)
== """Your position is coveraged by this bed file
chrX [50, 100]"""
)

输出如下:

FAILURES =====================================================
______________________________________________________________ 
test_target_region 
______________________________________________________________
def test_target_region():
BED = Read_file("./test_bed_file_with_errors4.bed")
data_loaded = BED.load_data()
bed_data = Bed_tools(data_loaded)
user_input2 =  "chrX:55"
chro = user_input2.split(":")[0]
position = user_input2.split(":")[1]
>       assert bed_data.targe_region(data_loaded, chro, position) == hola
E       AssertionError: assert None == 'Your position is coveraged by this bed filenchrX [50, 100]'
E        +  where None = <bound method Bed_tools.targe_region of <BED_toolkit.Class.bed_class.Bed_tools object at 0x7f9c713c0280>>({'chr1': [[1, 2], [3, 4], [5, 6], [7, 8]], 'chr20': [[1, 2], [3, 4]], 'chrX': [[1, 50], [50, 100]]}, 'chrX', '55')
E        +    where <bound method Bed_tools.targe_region of <BED_toolkit.Class.bed_class.Bed_tools object at 0x7f9c713c0280>> = <BED_toolkit.Class.bed_class.Bed_tools object at 0x7f9c713c0280>.targe_region
test.py:66: AssertionError
------------------------------------------------------------- Captured stdout call -------------------------------------------------------------
Your position is coveraged by this bed file
chrX [50, 100]
=========================================================== short test summary info ============================================================
FAILED test.py::test_target_region - AssertionError: assert None == 'Your position is coveraged by this bed filenchrX [50, 100]'

如何将捕获的标准输出调用与测试

匹配?

Pytest提供了不同的fixture,允许您捕获测试函数的输出。

因此,假设您当前的测试触发了if语句,您可以像这样利用capfdfixture:
def test_target_region(capfd):
"""
chr20    3    4    Neg2    0    -    127477031    127478198    0,0,255
chrX    1    50    Neg3    0    -    127478198    127479365    0,0,255
chrX    50    100    Pos5    0    +    127479365    127480532    255,0,0
"""
BED = Read_file("./test_bed_file_with_errors4.bed")
data_loaded = BED.load_data()
bed_data = Bed_tools(data_loaded)
user_input2 = "chrX:55"
chro = user_input2.split(":")[0]
position = user_input2.split(":")[1]
captured = capfd.readouterr()
bed_data.targe_region(data_loaded, chro, position)
assert (
captured.out
== """Your position is coveraged by this bed file
chrX [50, 100]"""
)

最新更新