'Pytest: Creating tests parameters via a function: Using MarkGenerator trigger a deprecation warning

So, in order to reuse tests parameters for several tests, I sometimes to the following:

def parameters():
    from _pytest.mark import MarkGenerator
    generator = MarkGenerator()
    return generator.parametrize("the_argument, the_result", [(1, 2), (2, 4)])

@parameters():
def test_multiplication("the_argument, the_result"):
    pass

Pytest is now issuing a PytestDeprecationWarning: A private pytest class or function was used. and the doc is saying that this is going to become an hard error

So my question is, how do I do the exact same thing, but in a legitimate way ?



Solution 1:[1]

Like this:

import pytest

parameters = pytest.mark.parametrize(
        "the_argument, the_result",
        [(1, 2), (2, 4)],
)

@parameters
def test_multiplication(the_argument, the_result):
    pass

@parameters
def test_multiplication_2(the_argument, the_result):
    pass

To briefly explain, decorators are objects just like everything else in python, so there's nothing stopping us from assigning them to variables to use/reuse later.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kale Kundert