python - 如何在 conftest.py 中访问 json 文件(像 config.json 这样的测试数据)
问题描述
假设在这个例子中,如何config.json
在conftest
执行测试套件时使用pytest
.
$ pwd
/home/user/repo/main
$ pytest testcases/project_(1/2)/test_suite_(1/2).py
目录结构:
├── main
│ ├── conftest.py # conftest file for my fixtures
│ ├── testcases
│ ├── project_1
│ │ (contains these files -- test_suite_1.py, config.json)
│ └── project_2
│ (contains these files -- test_suite_2.py, config.json)
├── workflows
│ └── libs
解决方案
您可以通过访问当前执行模块request.node.fspath
的路径并构建config.json
相对于它的路径。request
是由 提供的夹具pytest
。这是一个基于您提供的目录结构的示例。
# main/conftest.py
import json
import pathlib
import pytest
@pytest.fixture(autouse=True)
def read_config(request):
file = pathlib.Path(request.node.fspath)
print('current test file:', file)
config = file.with_name('config.json')
print('current config file:', config)
with config.open() as fp:
contents = json.load(fp)
print('config contents:', contents)
如果您将上面的代码复制到您的conftest.py
并使用 运行测试-s
,您应该得到类似于以下的输出:
$ pytest -sv
=============================== test session starts ===============================
platform linux -- Python 3.6.5, pytest-3.4.1, py-1.5.3, pluggy-0.6.0 -- /data/gentoo64/usr/bin/python3.6
cachedir: .pytest_cache
rootdir: /data/gentoo64/tmp/so-50329629, inifile:
collected 2 items
main/project1/test_one.py::test_spam
current file: /data/gentoo64/tmp/so-50329629/main/project1/test_one.py
current config: /data/gentoo64/tmp/so-50329629/main/project1/config.json
config contents: {'name': 'spam'}
PASSED
main/project2/test_two.py::test_eggs
current file: /data/gentoo64/tmp/so-50329629/main/project2/test_two.py
current config: /data/gentoo64/tmp/so-50329629/main/project2/config.json
config contents: {'name': 'eggs'}
PASSED
============================= 2 passed in 0.08 seconds ============================
使用解析的配置值
您可以通过在夹具中返回解析的 JSON 数据并将夹具用作测试参数之一来访问它。我从上面稍微修改了夹具,使其返回解析的数据并删除autouse=True
:
@pytest.fixture
def json_config(request):
file = pathlib.Path(request.node.fspath.strpath)
config = file.with_name('config.json')
with config.open() as fp:
return json.load(fp)
现在只需在测试参数中使用夹具名称,该值将是夹具返回的值。例如:
def test_config_has_foo_set_to_bar(json_config):
assert json_config['foo'] == 'bar'
推荐阅读
- neo4j - 如何避免 Neo4j 密码查询中的循环
- angular - 角度 *ngIf 条件
- c# - С# 表单按钮不更新;当它们更新时
- python - 在特定位置使用 pdfrw 将图像插入可填充的 pdf
- node.js - Express 路由器删除和 Firebase Cloud Function 给出 TypeError: Cannot read property 'apply' of undefined
- lambda - 你如何处理通过 lambdas 共享的常量?
- html - 通过来自不同组件的按钮单击提交
- antlr - 在规则中使用同名的非终结符
- arrays - 查找字符串中的重复模式
- php - 具有相同列值的行的总和