I am using pytest to run test case for a package I am developing. The tests use a small image file that I have saved as a github asset. The code below works just fine, but I think that pytest is downloading the image each time it runs a new test and that takes unnecessary time and resources. I was trying to figure out how I can download the file once, and then share it across test cases
Here is some sample code.
# -- in conftest.py --
import sys
import pytest
import os
import shutil
import requests
@pytest.fixture(scope="function")
def small_image(tmpdir):
url = 'https://github.com/.../sample_image_small.tif'
r = requests.get(url)
with open(os.path.join(str(tmpdir), 'sample_image_small.tif'), 'wb') as f:
f.write(r.content)
return os.path.join(str(tmpdir), 'sample_image_small.tif')
Then here are some very simple test cases that should be able to share the same image.
# -- test_package.py --
import pytest
import os
@pytest.mark.usefixtures('small_image')
def test_ispath(small_image, compression):
assert os.path.exists(small_image)
def test_isfile(small_image, compression):
assert os.path.isfile(small_image)
Now I believe that pytest will try and isolate each test by itself and so that is what causes the repeated downloads of files. I tried to set the @pytest.fixture(scope="module") instead of function but that was generating strange errors:
ScopeMismatch: You tried to access the 'function' scoped fixture 'tmpdir' with a 'module' scoped request object, involved factories
Is there a better way to setup the tests so that I don't keep download the file over and over?
Aucun commentaire:
Enregistrer un commentaire