【问题标题】:Parametrizing test with multiple fixtures that accept arguments [duplicate]使用多个接受参数的夹具进行参数化测试[重复]
【发布时间】:2019-02-17 19:28:45
【问题描述】:

我正在尝试测试我编写的数学函数。我想从许多不同的装置中向它提供数据。问题是所有的灯具都接受自己不同的灯具参数。

我运行的测试总是相同的(示例中为test_myfunc),并且我想要插入其中的装置都有相同的兼容返回值(代码中的clean_datanoisy_data) .因此,我想将这两个夹具“链接”在一起,以便其中一个为测试提供输入。

设置如下:

import numpy as np
import pytest
from scipy import stats

def myfunc(x, y):
    return True

_noises = {
    'normal': lambda scale, n: np.random.normal(scale=scale, size=n),
    'uniform': lambda scale, n: np.random.uniform(-scale, scale, size=n),
    'triangle': lambda scale, n: np.random.triangular(-scale, 0, scale, size=n),
}

@pytest.fixture(params=[10**x for x in range(1, 4)])
def x_data(request):
    """ Run the test on a few different densities """
    return np.linspace(-10, 10, request.param)

@pytest.fixture(params=[0, 1, 0xABCD, 0x1234])
def random_seed(request):
    """ Run the test for a bunch of datasets, but reporoducibly """
    np.random.seed(request.param)

@pytest.fixture(params=np.arange(0.5, 5.5, 0.5))
def shape(request):
    """ Run the test with a bunch of different curve shapes """
    return request.param

@pytest.fixture()
def clean_data(x_data, shape):
    """ Get a datset with no noise """
    return shape, stats.gamma.pdf(x_data, shape)

@pytest.fixture(params=["triangle", "uniform", "normal"])
def noisy_data(request, clean_data, random_seed):
    shape, base = clean_data
    noise = _noises[request.param](10, base.shape)
    return shape, base + noise

def test_myfunc(x_data, data):
    shape, y_data = data
    assert myfunc(x_data, y_data)

我使用这么多夹具的原因是我想运行完整的测试矩阵,能够随意启用、禁用、xfail 等。

由于clean_datanoisy_data 夹具返回相同类型的结果,我希望能够将它们一个接一个地用于我的测试。如何使用多个接受参数的夹具运行单个测试?

如果可能,我想避免测试生成。我熟悉间接参数化测试的想法,例如Running the same test on two different fixtures。我试图创建一个可以按名称执行 y 数据提供程序的元夹具:

@pytest.fixture()
def data(request):
    """ Get the appropriate datset based on the request """
    return request.getfuncargvalue(request.param)

@pytest.mark.parametrize('data', ['clean_data', 'noisy_data'], indirect=True)
def test_myfunc(x_data, data):
    shape, y_data = data
    assert myfunc(x_data, y_data)

当我运行测试时

pytest -v pytest-parametrized.py

我遇到了很多错误,这些错误似乎都指向这样一个事实,即间接夹具需要参数,而这些参数没有提供:

_________________ ERROR at setup of test_myfunc[10-clean_data] _________________

self = <_pytest.python.CallSpec2 object at 0x7f8a4ff06518>, name = 'shape'

    def getparam(self, name):
        try:
>           return self.params[name]
E           KeyError: 'shape'

/usr/lib/python3.6/site-packages/_pytest/python.py:684: KeyError

During handling of the above exception, another exception occurred:

self = <SubRequest 'clean_data' for <Function 'test_myfunc[10-clean_data]'>>
fixturedef = <FixtureDef name='shape' scope='function' baseid='pytest-parametrized.py' >

    def _compute_fixture_value(self, fixturedef):
        """
            Creates a SubRequest based on "self" and calls the execute method of the given fixturedef object. This will
            force the FixtureDef object to throw away any previous results and compute a new fixture value, which
            will be stored into the FixtureDef object itself.

            :param FixtureDef fixturedef:
            """
        # prepare a subrequest object before calling fixture function
        # (latter managed by fixturedef)
        argname = fixturedef.argname
        funcitem = self._pyfuncitem
        scope = fixturedef.scope
        try:
>           param = funcitem.callspec.getparam(argname)

/usr/lib/python3.6/site-packages/_pytest/fixtures.py:484: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <_pytest.python.CallSpec2 object at 0x7f8a4ff06518>, name = 'shape'

    def getparam(self, name):
        try:
            return self.params[name]
        except KeyError:
            if self._globalparam is NOTSET:
>               raise ValueError(name)
E               ValueError: shape

/usr/lib/python3.6/site-packages/_pytest/python.py:687: ValueError

During handling of the above exception, another exception occurred:

request = <SubRequest 'data' for <Function 'test_myfunc[10-clean_data]'>>

    @pytest.fixture()
    def data(request):
        """ Get the appropriate datset based on the request """
>       return request.getfuncargvalue(request.param)

pytest-parametrized.py:55: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python3.6/site-packages/_pytest/fixtures.py:439: in getfuncargvalue
    return self.getfixturevalue(argname)
/usr/lib/python3.6/site-packages/_pytest/fixtures.py:430: in getfixturevalue
    return self._get_active_fixturedef(argname).cached_result[0]
/usr/lib/python3.6/site-packages/_pytest/fixtures.py:455: in _get_active_fixturedef
    self._compute_fixture_value(fixturedef)
/usr/lib/python3.6/site-packages/_pytest/fixtures.py:526: in _compute_fixture_value
    fixturedef.execute(request=subrequest)
/usr/lib/python3.6/site-packages/_pytest/fixtures.py:778: in execute
    fixturedef = request._get_active_fixturedef(argname)
/usr/lib/python3.6/site-packages/_pytest/fixtures.py:455: in _get_active_fixturedef
    self._compute_fixture_value(fixturedef)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <SubRequest 'clean_data' for <Function 'test_myfunc[10-clean_data]'>>
fixturedef = <FixtureDef name='shape' scope='function' baseid='pytest-parametrized.py' >

    def _compute_fixture_value(self, fixturedef):
        """
            Creates a SubRequest based on "self" and calls the execute method of the given fixturedef object. This will
            force the FixtureDef object to throw away any previous results and compute a new fixture value, which
            will be stored into the FixtureDef object itself.

            :param FixtureDef fixturedef:
            """
        # prepare a subrequest object before calling fixture function
        # (latter managed by fixturedef)
        argname = fixturedef.argname
        funcitem = self._pyfuncitem
        scope = fixturedef.scope
        try:
            param = funcitem.callspec.getparam(argname)
        except (AttributeError, ValueError):
            param = NOTSET
            param_index = 0
            if fixturedef.params is not None:
                frame = inspect.stack()[3]
                frameinfo = inspect.getframeinfo(frame[0])
                source_path = frameinfo.filename
                source_lineno = frameinfo.lineno
                source_path = py.path.local(source_path)
                if source_path.relto(funcitem.config.rootdir):
                    source_path = source_path.relto(funcitem.config.rootdir)
                msg = (
                    "The requested fixture has no parameter defined for the "
                    "current test.\n\nRequested fixture '{0}' defined in:\n{1}"
                    "\n\nRequested here:\n{2}:{3}".format(
                        fixturedef.argname,
                        getlocation(fixturedef.func, funcitem.config.rootdir),
                        source_path,
                        source_lineno,
                    )
                )
>               fail(msg)
E               Failed: The requested fixture has no parameter defined for the current test.
E               
E               Requested fixture 'shape' defined in:
E               pytest-parametrized.py:27
E               
E               Requested here:
E               /usr/lib/python3.6/site-packages/_pytest/fixtures.py:526

/usr/lib/python3.6/site-packages/_pytest/fixtures.py:506: Failed

如果以某种方式提供缺少的参数是答案,那很好,但我不想那样问这个问题,因为我想我可能会在这里遇到一个巨大的 XY 问题。

【问题讨论】:

    标签: python python-3.x pytest


    【解决方案1】:

    pytest 不支持将夹具作为参数传递给参数化标记。有关详细信息,请参阅问题 #349:Using fixtures in pytest.mark.parametrize。当需要使用夹具参数化时,我通常会创建一个辅助夹具来接受所有参数夹具,然后在测试中间接对其进行参数化。因此,您的示例将变为:

    @pytest.fixture
    def data(request, clean_data, noisy_data):
        type = request.param
        if type == 'clean':
            return clean_data
        elif type == 'noisy':
            return noisy_data
        else:
            raise ValueError('unknown type')
    
    @pytest.mark.parametrize('data', ['clean', 'noisy'], indirect=True)
    def test_myfunc(x_data, data):
        shape, y_data = data
        assert myfunc(x_data, y_data)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多