Parameterized testing using Pytest

Pytest provides a feature for parameterized testing in Python. The built-in pytest.mark.parametrize decorator enables parametrization of arguments for a test function. This allows the user to compare the values for input and output.

Here is a typical example which shows its usage:

get_hips_order_for_resolution_pars = [
    dict(tile_width=512, resolution=0.01232, resolution_res=0.06395791924665553, order=4),
    dict(tile_width=256, resolution=0.0016022, resolution_res=0.003997369952915971, order=8),
    dict(tile_width=128, resolution=0.00009032, resolution_res=0.00012491781102862408, order=13),

@pytest.mark.parametrize('pars', get_hips_order_for_resolution_pars)
def test_get_hips_order_for_resolution(pars):
    hips_order = _get_hips_order_for_resolution(pars['tile_width'], pars['resolution'])
    assert hips_order == pars['order']
    hips_resolution = hp.nside2resol(hp.order2nside(hips_order))
    assert_allclose(hips_resolution, pars['resolution_res'])

Without the support of parameterized testing, the code had to be duplicated three times. Thus, through parameterized testing the user can see the expected values without running the tests.