[英]How to mock out `s3fs.FileSystem().glob`?
I have a function which includes the following:我有一个功能,其中包括以下内容:
FS = s3fs.S3FileSystem()
def my_function(s3_path: str):
files = FS.glob('s3://'+s3_path)
It lists all files on AWS S3 which match a certain string s3_path
, and then (later) downloads them.它列出了 AWS S3 上与某个字符串
s3_path
匹配的所有文件,然后(稍后)下载它们。
When testing this function, I don't want to connect to AWS S3 each time.在测试此功能时,我不想每次都连接到 AWS S3。 But I do want to check what happens to the files I download.
但我确实想检查我下载的文件会发生什么。
So, I have locally made a folder called testinputs
.所以,我在本地创建了一个名为
testinputs
的文件夹。 In here, I have copied some of the files I have on AWS S3.在这里,我复制了我在 AWS S3 上的一些文件。
My idea then was to mock out the FS.glob(s3_path)
call so that, instead, it returns the output of我的想法是模拟
FS.glob(s3_path)
调用,以便它返回
import glob
glob.glob(os.path.join('testinputs', s3_path))
How can I do that?我怎样才能做到这一点? I have tried using
mock
from unittest
to write:我尝试使用
unittest
mock
来编写:
from unittest import mock
def test_my_function():
with mock.patch('s3fs.S3FileSystem') as mock_fs:
mock_fs.glob = glob.glob
my_function('<SOME FILE PATH>')
But this doesn't seem to be changing the behaviour of FS.glob
at all.但这似乎根本没有改变
FS.glob
的行为。
Got it eventually:终于明白了:
import pytest
from glob import glob
def test_my_function(monkeypatch):
monkeypatch.setattr(mypackage.myfile.FS, "glob", glob)
my_function('<SOME FILE PATH>')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.