【发布时间】:2020-06-04 21:24:54
【问题描述】:
我有一个 AWS S3 目录,其中包含几个 JSON 文件,用作测试输入。
我创建了一个 PyTest 模块,它使用模块范围 fixture 下载所有 JSON 文件一次,然后运行几个测试函数 - 每个函数都在 JSON 集上进行参数化:
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...
这个测试模块本身就可以工作——唯一的痛点是如何在模块\会话结束时清理temp_dir。
由于download_jsons() 被直接调用,所以在json_file 夹具甚至启动之前 - 它没有自己的上下文。所以在所有测试完成后我不能让它干净temp_dir。
我想让download_jsons() 本身成为一个模块\会话范围fixture。比如:
fixture(scope='module')
def download_jsons():
temp_dir = mkdtemp()
# Download and as glob, as above
yield json_filenames
shutil.rmtree(temp_dir)
或
fixture(scope='module')
def download_jsons(tmpdir_factory):
#...
正如@Gabriela Melo 所建议的那样。
问题是如何使json_file 夹具参数化在download_jsons() 返回的列表上,而不直接调用它?
我尝试使用 mark.parametrize、setup_module() 或 pytest_generate_tests(metafunc) 来实现此解决方案 - 但无法实现我正在寻找的确切功能。
【问题讨论】:
-
您是否有关于使用夹具作为参数化参数的问题?这不受支持,请参阅issue #349。还是只是清理临时目录?这可以通过多种方式轻松完成,无需将代码放在额外的夹具中。
-
谢谢!我错过了那个问题。很高兴知道不支持。我的问题是如何在第一次评估
json_load()夹具之前创建临时目录,以便可以在临时目录的内容上对其进行参数化。然后我需要在 last 调用夹具之后删除 tempdir(它被多个测试函数多次使用)。我找不到这样做的方法。 -
这甚至可能超出单个
module- 我可能需要在每个会话中下载一次 JSON,在 tempdir 的内容上对来自不同模块的测试进行参数化,然后在会话结束时删除该目录.