I have test code that downloads a set of images, does some processing on them, and asserts that the processing worked as expected:
@pytest.fixture
def image_dir(tmp_path):
test_imgs = [
# ... list of img URLs
]
for idx, im_url in enumerate(test_imgs):
urllib.request.urlretrieve(im_url, tmp_path / f"{idx}.png")
yield tmp_path
def test_op_A(image_dir: Path):
for im_path in image_dir.iterdir():
# load the image
# modify the image
# save the image back to disk
# assert that the modification worked as expected
def test_op_B(image_dir: Path):
for im_path in image_dir.iterdir():
# load the image
# modify the image
# save the image back to disk
# assert that the modification worked as expected
# ... more tests with a similar format
This works but is incredibly slow. I suspect that this is because the images are downloaded anew for each test.
Is there a clean way to create the temporary directory once, cache it, and use a copy of the directory for each test? This way each test can modify the images as desired, without influencing the other tests and while remaining performant.