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

fug*_*ugu 8 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 = 'tests/data/pb_stats.json'
Run Code Online (Sandbox Code Playgroud)

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

@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
Run Code Online (Sandbox Code Playgroud)

我希望能够对多个不同的输入集运行相同的测试,其中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"""
Run Code Online (Sandbox Code Playgroud)

但这不适用于我设置的装置(除非我遗漏了某些东西)。

任何建议都会很棒!

gcd*_*sss 7

使用 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):
   #
Run Code Online (Sandbox Code Playgroud)

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