I have an AWS S3 directory containing several JSON files, which are used as test inputs.
I've created a PyTest module that downloads all JSON files once using a module wide fixture
, and then runs several test functions - each being parameterized over the set of JSONs:
import pytest
import os
from tempfile import mkdtemp, TemporaryDirectory
from glob import glob
JSONS_AWS_PATH = 's3://some/path/'
def download_jsons():
temp_dir = mkdtemp()
aws_sync_dir(JSONS_AWS_PATH, temp_dir)
json_filenames = glob(os.path.join(local_path, "*.json"))
return json_filenames
@pytest.fixture(scope='module', params=download_jsons()) #<-- Invoking download_jsons() directly
def json_file(request):
return request.param
def test_something(json_file):
# Open the json file and test something
def test_something_else(json_file):
# Open the json file and test something else
def test_another_thing(json_file):
# you got the point...
This test module in itself works - the only pain point is how to cleanup the temp_dir
at the end of the module\session.
Since download_jsons()
is being invoked directly, before json_file
fixture is even started - it has no context of its own. So I can't make it clean temp_dir
after all the tests are done.
I would like to make download_jsons()
a module\session scope fixture
in itself. Something like:
fixture(scope='module')
def download_jsons():
temp_dir = mkdtemp()
# Download and as glob, as above
yield json_filenames
shutil.rmtree(temp_dir)
or
fixture(scope='module')
def download_jsons(tmpdir_factory):
#...
as @Gabriela Melo has suggested.
The question is how to make the json_file
fixture parameterized over the list returned by download_jsons()
, without invoking it directly?
I've tried implementing this solution with either mark.parametrize
, setup_module()
, or pytest_generate_tests(metafunc)
- but wasn't able to implement the exact functionality I was looking for.