Skip to content
Advertisement

Parametrizing fixtures and functions without combinations

Here’s the thing:

I have an Array2D class that receives parameters ‘shape’ and ‘val’. The constructor is as follows:

JavaScript

I would like to perform tests on this class. To do this I have declared a variable ARRAY_CONFIG, which lists parameters for different two-dimensional arrays. Here is an example:

JavaScript

I have also defined other lists that store the expected values for future tests. Take a look:

JavaScript

Now I want to build a test function that by the parameterization decorator, makes comparison between these lists. Here is an example without the decorators from the PyTest library:

JavaScript

I managed to develop something, but it seems somewhat repetitive to me. Here it is:

JavaScript

What is the best way to perform this kind of test where there is a list of “expected values”? I’ve tried performing two parameterizations, but I end up performing combinations between the values, rather than one-to-one comparisons. As done here:

JavaScript

Thanks in advance.

Advertisement

Answer

It’s much simpler if you use the combine function as the parameter in parametrize. You can yield every set of values

JavaScript

Or more explicit parameters

JavaScript

Output

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement