Pytest-对不同的输入数据集运行相同的测试



我有许多函数要使用pytest进行测试。

在整个测试过程中,我使用了几个在脚本顶部指定的输入文件:

import pytest
from mymodule.mymodule import *
test_bam = 'bam/test/test_reads_pb.bwa.bam'
sample_mapping_file = 'tests/test_sample_mapping.txt'
pb_errors_file = 'tests/data/pb_test_out.json'
pb_stats = 'tests/data/pb_stats.json'

然后我使用这个输入运行了几个测试:

@pytest.fixture
def options():
o, a = get_args()
return o
@pytest.fixture
def samples_dict():
d = get_sample_mapping(sample_mapping_file)
return d

@pytest.fixture
def read_stats(options, samples_dict):
stats, bam = clean_reads(options, test_bam, samples_dict)
return stats

@pytest.fixture
def clean_bam(options, samples_dict):
stats, bam = clean_reads(options, test_bam, samples_dict)
return bam

def test_errors(options, samples_dict, clean_bam):
"""Test successful return from find_errors"""
sample, genome, chrom = set_genome(options, test_bam, samples_dict)
base_data = find_errors(options, chrom, sample, clean_bam)
assert base_data

我希望能够在多个不同的输入集上运行相同的测试,其中test_bamsample_mapping_filepb_errors_filepb_stats都不同。

在不同的输入数据集上运行相同测试的最佳方式是什么

我曾经尝试过使用标记来运行特定于输入的功能:

@pytest.mark.pd
def get_pb_data():
"""Read in all pb-related files"""
@pytest.mark.ab
def get_ab_data():
"""Read in all ab-related files"""

但这对我设置的固定装置不起作用(除非我错过了什么(。

任何建议都会很棒!

使用pytest参数化包装器。

test_bam = 'bam/test/test_reads_pb.bwa.bam'
sample_mapping_file = 'tests/test_sample_mapping.txt'
pb_errors_file = 'tests/data/pb_test_out.json'
pb_stats = 'tests/data/pb_stats.json'
@pytest.mark.parametrize("config", [test_bam, sample_mapping_file, pb_errors_file, pb_stats])
def do_something(config):
#

它将在每个配置测试输入上创建多个测试,并分配给config变量。

@pytest.mark.pd没有指定输入类型,它将pd标记添加到测试中,可以在运行测试时使用,例如运行所有用pd标记的测试

pytest TestsFolder -m pd

如果你想在不同的文件集上运行测试,你可以将文件名存储在csv中,并在测试参数化标记中读取这些文件集

def data_source():
for files in read_files_groups_from_csv():
yield files
@pytest.mark.parametrize('files', data_source())
def test_errors(options, samples_dict, clean_bam, files):
"""for example, files parameter will be ['bam/test/test_reads_pb.bwa.bam', 'tests/test_sample_mapping.txt', 'tests/data/pb_test_out.json', 'tests/data/pb_stats.json']"""

mark.parametrize将在固定装置之前运行,因此您可以将files作为参数发送给它们以及

@pytest.fixture
def options(files):
d = samples_dict(files[1])
return d

如果您不想依赖索引,请创建一个以文件名为属性的类,并从data_source()返回它。

最新更新