Skip to content

Idea: @pytest.mark.parametrize on @pytest.fixture #3960

@nyanpasu64

Description

@nyanpasu64

Miniconda Python 3.6.6, pytest 3.8.0 from conda-forge

STEREO_PATHS = ['stereo 1.wav', 'stereo 2.wav']

@pytest.mark.parametrize("path", STEREO_PATHS)
@pytest.mark.parametrize("cfg_factory", [list, dict])   # not actual params
@pytest.fixture(scope="module")
def stereo_cfg(path, cfg_factory): ...
          @pytest.mark.parametrize("path", STEREO_PATHS)
          @pytest.mark.parametrize("cfg_factory", [n163_cfg, unrounded_cfg])
          @pytest.fixture(scope="module")
          def stereo_cfg(path, cfg_factory):
E       fixture 'path' not found
  • @pytest.fixture(params) does not allow me to parameterize one fixture with multiple orthogonal parameters (I want all 4 combinations of path and cfg_factory).

  • I find the pytest.fixture(params=[...]) to be extremely inelegant and confusing compared to @pytest.mark.parametrize. This is because of the requirement that the function argument be named request, the actual parameter be named request.param (making the parameter name non-self-documenting and confusing at first).

maybe test functions can accept parametric parameters directly, while fixture functions can only accept fixtures as parameters? if so, that's a problem that should be solved.


Workaround: Boilerplate fixtures which wrap lists:

STEREO_PATHS = ['stereo 1.wav', 'stereo 2.wav']

@pytest.fixture(scope="module", params=STEREO_PATHS)
def path(request):
    path = request.param
    return Path('test_waves', path)

@pytest.fixture(scope="module", params=[list, dict])
def cfg_factory(request):
    return request.param

@pytest.fixture(scope="module")
def stereo_cfg(path, cfg_factory): ...

But this adds many boilerplate lines of code. I thought pytest was a boilerplate-free test system.

# packages in environment at /home/jimbo1qaz/miniconda3/envs/wavetable:
#
# Name                    Version                   Build  Channel
atomicwrites              1.2.1                    py36_0  
attrs                     18.2.0           py36h28b3542_0  
blas                      1.0                         mkl  
ca-certificates           2018.8.24            ha4d7672_0    conda-forge
certifi                   2018.8.24                py36_1    conda-forge
click                     6.7                      py36_0  
dataclasses               0.6                        py_0    conda-forge
intel-openmp              2018.0.3                      0  
libedit                   3.1.20170329         h6b74fdf_2  
libffi                    3.2.1                hd88cf55_4  
libgcc-ng                 8.2.0                hdf63c60_1  
libgfortran-ng            7.3.0                hdf63c60_0  
libstdcxx-ng              8.2.0                hdf63c60_1  
mkl                       2018.0.3                      1  
mkl_fft                   1.0.4            py36h4414c95_1  
mkl_random                1.0.1            py36h4414c95_1  
more-itertools            4.3.0                    py36_0  
ncurses                   6.1                  hf484d3e_0  
numpy                     1.15.1           py36h1d66e8a_0  
numpy-base                1.15.1           py36h81de0dd_0  
openssl                   1.0.2p               h470a237_0    conda-forge
pip                       10.0.1                   py36_0  
pluggy                    0.7.1            py36h28b3542_0  
py                        1.6.0                    py36_0  
pytest                    3.8.0                    py36_0    conda-forge
python                    3.6.6                hc3d631a_0  
readline                  7.0                  h7b6447c_5  
ruamel.yaml               0.15.65          py36h470a237_0    conda-forge
scipy                     1.1.0            py36hfa4b5c9_1  
setuptools                40.2.0                   py36_0  
six                       1.11.0                   py36_1  
sqlite                    3.24.0               h84994c4_0  
tk                        8.6.8                hbc83047_0  
waveform-analysis         0.1                       <pip>
wavetable                 0.0.0                     <pip>
wheel                     0.31.1                   py36_0  
xz                        5.2.4                h14c3975_4  
zlib                      1.2.11               ha838bed_2  

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytopic: parametrizerelated to @pytest.mark.parametrize

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions