在pytest中的不同测试中仅使用某些夹具参数化

Olg*_*nik 7 python fixtures pytest

我有一个叫做夹具n_groups,我希望在某些情况下参数化,但在其他情况下则不需要参数化.这是因为我的类似MVC的数据模型的结构方式,我在"模型"类中尽可能多地测试,但"控制器"类不需要如此广泛的测试,因为我已经在"模型"中完成.因此,在控制器中运行带有所有参数化的测试是多余的,我想限制测试次数,从而限制测试时间.目前,为了测试我的控制器的初始化,生成了超过18,000个测试,运行需要42分钟!请参阅Travis-CI输出.

目前,我的解决方法是做,

# Contents of conftest.py
import pytest
import pandas as pd
import numpy as np

@pytest.fixture(scope='module', params=[2, 3],
                ids=['2_groups', '3_groups'])
def n_groups(request):
    """Number of phenotype groups.

    For testing that functions work when there's only 2 groups
    """
    return request.param

@pytest.fixture(scope='module')
def n_groups_fixed():
    """Fixed number of phenotype groups (3)"""
    return 3
Run Code Online (Sandbox Code Playgroud)

然后,我将其中一个n_groups或者传递n_groups_fixed到下一个灯具链,这些灯具会创建用于测试的数据.在outliers,pooled,samples,n_samplesmetadata_phenotype_col固定装置也是参数,但超出了这个问题的范围.

# Contents of conftest.py
@pytest.fixture(scope='module')
def groups(n_groups):
    """Phenotype group names"""
    return ['group{}'.format(i + 1) for i in np.arange(n_groups)]

@pytest.fixture(scope='module')
def groups_fixed(n_groups_fixed):
    """Phenotype group names"""
    return ['group{}'.format(i + 1) for i in np.arange(n_groups_fixed)]

@pytest.fixture(scope='module')
def groupby(groups, samples):
    return dict((sample, np.random.choice(groups)) for sample in samples)

@pytest.fixture(scope='module')
def groupby_fixed(groups_fixed, samples):
    return dict((sample, np.random.choice(groups_fixed)) for sample in samples)

@pytest.fixture(scope='module')
def metadata_data(groupby, outliers, pooled, samples,
                  n_samples,
                  metadata_phenotype_col):
    df = pd.DataFrame(index=samples)
    if outliers is not None:
        df['outlier'] = df.index.isin(outliers)
    if pooled is not None:
        df['pooled'] = df.index.isin(pooled)
    df[metadata_phenotype_col] = groupby
    df['subset1'] = np.random.choice([True, False], size=n_samples)
    return df

@pytest.fixture(scope='module')
def metadata_data_groups_fixed(groupby_fixed, outliers, pooled, samples,
                  n_samples,
                  metadata_phenotype_col):
    df = pd.DataFrame(index=samples)
    if outliers is not None:
        df['outlier'] = df.index.isin(outliers)
    if pooled is not None:
        df['pooled'] = df.index.isin(pooled)
    df[metadata_phenotype_col] = groupby_fixed
    df['subset1'] = np.random.choice([True, False], size=n_samples)
    return df
Run Code Online (Sandbox Code Playgroud)

拥有*_fixed这些灯具的版本似乎相当麻烦.

测试的例子是在数据模型中进行n_groups广泛的测试,测试控制器的参数化和不太广泛的测试,它只测试一个"参数化" groups_fixed(这些不是真正的测试,只是演示的例子):

# Contents of test_model.py
class TestModel(object):
    def test__init(metadata_data, ...):
        ...

    def test_plot(metadata_data_fixed, ...);
        ...

# Contents of test_controller.py
class TestController(object):
    def test__init(metadata_data_fixed, ...):
        ...
Run Code Online (Sandbox Code Playgroud)

还有另一种方法吗?我已经阅读了pytest的参数化文档,但它似乎只是全局设置参数化,而不是基于每个测试.

我想做点什么:

# Contents of test_model.py
class TestModel(object):
    def test__init(metadata_data, ...):
        ...

    @pytest.mark.parameterize(n_groups=3)
    def test_plot(metadata_data, ...);
        ...

# Contents of test_controller.py
class TestController(object):
    @pytest.mark.parameterize(n_groups=3)
    def test__init(metadata_data_fixed, ...):
        ...
Run Code Online (Sandbox Code Playgroud)

更新:添加一个n_groups夹具TestController没有帮助,即这不起作用:

# Contents of test_controller.py
class TestController(object):
    @pytest.fixture
    def n_groups():
        return 3

    def test__init(metadata_data_fixed, ...):
        ...
Run Code Online (Sandbox Code Playgroud)

我不确定为什么,因为看起来这个夹具应该覆盖n_groups定义的全局conftest.py

Bru*_*ira 0

我不确定您可以使用builtin 做到这一点parametrize,我认为您必须根据有关正在测试的方法的一些信息来实现自定义参数化方案(例如,如果一个类Controller在其名称中包含您将对其进行不同的参数化) ,使用pytest_generate_tests 钩子可以在此处找到一些示例。