'Persisting PyTest data parameters across multiple test cases

Is there a way from me to persist the "ref" parameters across multiple test cases inside the same Python script?

This setup "works", but it feels sub-optimal to have to copy/paste the same data for each test case.

@pytest.mark.parametrize('ref', [
    'crmpicco1872',
    'crmpicco2001',
    'crmpicco2008',
    'crmpicco2013',
    'crmpicco2021',
])
def test_data_directory_does_not_already_exist(ref):
    datadir = f"/var/lib/data/sites/{ref}"

    assert not os.path.exists(datadir)


@pytest.mark.parametrize('ref', [
    'crmpicco1872',
    'crmpicco2001',
    'crmpicco2008',
    'crmpicco2013',
    'crmpicco2021',
])
def test_data_directory_is_created(logger_fixture, config_parser_fixture, clean_context_fixture, ref):
    datadir = f"/var/lib/data/sites/{ref}"

    clean_context_fixture.data = "data0"
    clean_context_fixture.ref = ref

    create_data = CreateData(logger_fixture, config_parser_fixture)
    create_data.execute(clean_context_fixture)

    assert os.path.exists(datadir)
    shutil.rmtree(datadir)


Sources

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

Source: Stack Overflow

Solution Source