【问题标题】:how to access a json file(test data like config.json) in conftest.py如何在 conftest.py 中访问 json 文件(像 config.json 这样的测试数据)
【发布时间】:2018-05-14 11:51:57
【问题描述】:

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

$ 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 

【问题讨论】:

    标签: python testing pytest


    【解决方案1】:

    您可以通过request.node.fspath 访问当前执行模块的路径,并构建相对于它的config.json 的路径。 requestpytest 提供的夹具。这是一个基于您提供的目录结构的示例。

    # 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'
    

    【讨论】:

    • 感谢 Hoefling,这对我有用,只需稍加改动 request.node.fspath.strpath。并且还想知道如何在测试用例或测试套件中访问此 json 数据。
    • 嗨@SunilKumar,请查看更新后的答案,其中包含访问配置值的示例。至于strpath——我猜你使用的Python版本早于3.6,那么是的,你需要先将fspath转换为字符串——使用fspath.strpathstr(request.fspath)
    • 嗨@Hoefling,这里的夹具是否位于conftest.py中?我只需要一种方法来访问项目目录中测试套件文件中 test_class/test_cases 中的数据。
    • 你可以把fixture放在conftest文件或者test文件中,不管怎样都可以。
    • 很高兴能帮到你!如果您发现答案有用,您可以接受它以将问题标记为已解决,或投赞成票,或两者兼而有之。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2017-07-14
    相关资源
    最近更新 更多