Calmcode - pytest tricks: pytest ids

Using Ids in Pytest

1 2 3 4 5 6 7 8

In general, it's best to add an ids parameter to parametrize decorators. In the long run, this makes your code more maintainable.

import pytest
import numpy as np

def normalize(X):
    return (X - X.min())/(X.max() - X.min())

@pytest.mark.parametrize("x", [1, 2, 30], ids=lambda d: f"x={d}")
@pytest.mark.parametrize("y", [1, 2, 30], ids=lambda d: f"y={d}")
def test_shape_same(x, y):
    if x == y:
        pytest.skip("Not of interest")
    X = np.random.normal((x, y))
    X_norm = normalize(X)
    assert X.shape == X_norm.shape

When you now run the unit tests with --verbose you'll see more elaborate information.

# The verbose setting is optional, but gives more information
pytest --verbose