I have been using local implementation of s3 bucket from cloudpathlib
following a similar pattern to the documentation here: https://cloudpathlib.drivendata.org/~latest/testing_mocked_cloudpathlib/#2-get-a-pathlibpath-object-that-points-to-the-local-storage-directory
I make two tests, one checking the bucket contains at least a file, and one where the bucket is empty. When I run pytest for all tests, the second fails even when it should have reset the local storage directory to point to an empty folder. It does succeed when running on its own.
Here’s a simplified version of my actual code but reproducing the same issue. I am setting up the local s3 bucket correctly?
Code to test
from cloudpathlib import CloudPath
def main(s3_bucket: str):
s3_bucket_path = CloudPath(s3_bucket)
if list(s3_bucket_path.glob('*.txt')):
return True
else:
return False
Tests
import pytest
from cloudpathlib import implementation_registry
from cloudpathlib.local import LocalS3Client, local_s3_implementation
from cloudpath import main
@pytest.fixture
def local_bucket_name():
return 'cloudpathlib-test-bucket'
@pytest.fixture
def local_s3_bucket(monkeypatch, local_bucket_name):
"""
Fixture that patches CloudPath dispatch and create bucket in LocalS3Client
"""
monkeypatch.setitem(implementation_registry, 's3', local_s3_implementation)
local_pathlib_path = LocalS3Client.get_default_storage_dir() / local_bucket_name
local_pathlib_path.mkdir(exist_ok=True, parents=True)
yield local_pathlib_path
LocalS3Client.reset_default_storage_dir() # clean up temp directory and replace with new one
class TestMain:
def test_returns_true_if_contains_txt_files(self, local_s3_bucket, local_bucket_name):
(local_s3_bucket / 'test.txt').touch()
assert main(f's3://{local_bucket_name}')
def test_returns_false_if_bucket_does_not_contains_txt_files(self, local_s3_bucket, local_bucket_name):
assert not main(f's3://{local_bucket_name}')
Lucie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.