给定json文件:
["smtp.gmail.com", "mail.python.org"]
您可以简单地将其加载到Python对象,然后将该对象传递给装饰器。
import json import pytest import smtplib def load_params_from_json(json_path): with open(json_path) as f: return json.load(f) @pytest.fixture(scope="module", params=load_params_from_json('path/to/file.json')) def smtp(request): smtp = smtplib.SMTP(request.param) def fin(): print ("finalizing %s" % smtp) smtp.close() request.addfinalizer(fin) return smtp