Calmcode - pytest tricks: pytest skip

Skipping Tests in Pytest

1 2 3 4 5 6 7 8 9 10

When working with big parametrize grids, it can be very helpful to skip a few combinations. The pytest.skip command will be of great help here.


Here's an example that will skip the test sometimes.

import pytest
import numpy as np

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

@pytest.mark.parametrize("x", [1, 2, 30])
@pytest.mark.parametrize("y", [1, 2, 30])
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

If you run this test suite, you'll notice that some of the tests are skipped.

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