Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何访问conftest.py中的json文件(测试数据,如config.json)_Python_Testing_Pytest - Fatal编程技术网

Python 如何访问conftest.py中的json文件(测试数据,如config.json)

Python 如何访问conftest.py中的json文件(测试数据,如config.json),python,testing,pytest,Python,Testing,Pytest,假设在本例中,在使用pytest执行测试套件时,如何访问conftest fixture中相应的config.json文件 目录结构: ├── main │ ├── conftest.py # conftest file for my fixtures │ ├── testcases │ ├── project_1 │ │ (contains these files -- test_suite_1.py, config.json) │ └── pro

假设在本例中,在使用pytest执行测试套件时,如何访问conftest fixture中相应的config.json文件

目录结构:

├── 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.sjon的路径。请求是pytest提供的一个fixture。下面是一个基于您提供的目录结构的示例

main/conftest.py 导入json 导入路径库 导入pytest @pytest.fixtureautouse=True def read_配置请求: file=pathlib.Pathrequest.node.fspath 打印“当前测试文件:”,文件 config=file.with_name'config.json' 打印“当前配置文件:”,配置 使用config.open作为fp: contents=json.loadfp 打印“配置内容:”,内容 如果将上述代码复制到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 ============================
使用解析的配置值 您可以通过在fixture中返回解析的JSON数据并将fixture用作测试参数之一来访问它。我从上面稍微修改了fixture,使其返回解析的数据,并删除了autouse=True:

现在只需在测试参数中使用fixture名称,该值将是fixture返回的值。例如:

def test_config_has_foo_set_to_bar(json_config):
    assert json_config['foo'] == 'bar'

您可以通过request.node.fspath访问当前执行的模块的路径,并构建相对于它的config.sjon的路径。请求是pytest提供的一个fixture。下面是一个基于您提供的目录结构的示例

main/conftest.py 导入json 导入路径库 导入pytest @pytest.fixtureautouse=True def read_配置请求: file=pathlib.Pathrequest.node.fspath 打印“当前测试文件:”,文件 config=file.with_name'config.json' 打印“当前配置文件:”,配置 使用config.open作为fp: contents=json.loadfp 打印“配置内容:”,内容 如果将上述代码复制到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 ============================
使用解析的配置值 您可以通过在fixture中返回解析的JSON数据并将fixture用作测试参数之一来访问它。我从上面稍微修改了fixture,使其返回解析的数据,并删除了autouse=True:

现在只需在测试参数中使用fixture名称,该值将是fixture返回的值。例如:

def test_config_has_foo_set_to_bar(json_config):
    assert json_config['foo'] == 'bar'

感谢Hoefling,这对我来说非常有效,只需要一点点更改请求。node.fspath.strpath。还想知道如何在测试用例或测试套件中访问这些json数据。Hi@SunilKumar,请查看更新后的答案和访问配置值的示例。至于strpath-我猜您使用的是3.6以上的Python版本,那么是的,您需要首先将fspath转换为字符串-使用fspath.strpath或strequest.fspath.Hi@Hoefling,这里的fixture是否位于conftest.py中?我只需要一种方法来访问项目目录中的测试套件文件中的测试类/测试用例中的数据。您可以将夹具放在conftest文件或测试文件中,这两种方法都可以。很高兴我能帮助您!如果您觉得答案有用,您可以接受它,将问题标记为已解决,或投票表决,或两者兼而有之。感谢Hoefling,这对我来说非常有效,只需稍加更改请求。node.fspath.strpath。还想知道如何在测试用例或测试套件中访问这些json数据。Hi@SunilKumar,请查看更新后的答案和访问配置值的示例。至于strpath-我猜您使用的是3.6以上的Python版本,那么是的,您需要首先将fspath转换为字符串-使用fspath.strpath或strequest.fspath.Hi@Hoefling,这里的fixture是否位于conftest.py中?我只需要一种方法来访问项目目录中的测试套件文件中的测试类/测试用例中的数据。您可以将夹具放在conftest文件或测试文件中,这两种方法都可以。很高兴我能帮助您!如果你觉得答案有用,你可以接受它,将问题标记为已解决,或投票表决,或两者兼而有之。