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

Python Pytest-对不同的输入数据集运行相同的测试,python,pytest,Python,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

我有许多函数要使用
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\u bam
sample\u mapping\u file
pb\u errors\u file
pb\u 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参数化包装器

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
将在夹具之前运行,因此您也可以将
文件作为参数发送给它们

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

如果您不想依赖索引创建一个以文件名作为属性的类,并从
data\u source()

不指定输入类型,它会将
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
将在夹具之前运行,因此您也可以将
文件作为参数发送给它们

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

如果您不想依赖索引,请创建一个以文件名作为属性的类,并从
data\u source()

返回它,也许您可以对此进行更多的扩展?您可以在这里参考官方文档。我仍然不太清楚这将如何工作。我有几个不同的
test\u bam
(例如)文件,我想针对多个测试运行这些文件。我是否应该修饰使用
@pytest.mark.parametrize
运行的每个测试?这是如何与
@fixtures
一起工作的?我不明白。你想使用不同的测试设置资源吗?这正是为之设计的
参数化
。也许你可以对此进行更多的扩展?你可以在这里参考官方文档。我仍然不太清楚这将如何工作。我有几个不同的
test\u bam
(例如)文件,我想针对多个测试运行这些文件。我是否应该修饰使用
@pytest.mark.parametrize
运行的每个测试?这是如何与
@fixtures
一起工作的?我不明白。您想使用不同的测试设置资源吗?这正是为您设计的
parametrize