可以在 pytest.ini 中设置测试的任意配置吗? [英] Can arbirary configuration for tests be set in pytest.ini?

查看:85
本文介绍了可以在 pytest.ini 中设置测试的任意配置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的测试集中配置,看起来 pytest.ini 就是这个地方,但我无法为此找到示例/功能.

I want to centralize configuration for my tests and it seems like pytest.ini would be the place but im having trouble finding an example/feature for this.

例如,我有一个目录,其中包含我的测试可能需要的名为test_resources"的文件.这是我的结构:

For example I have a directory with files my tests might need called "test_resources". this is my structure:

├── pytest.ini
├── myproject
│    └── someFunctionsOne
│        ├── one.py
│        └── two.py
└── tests
    ├── integration
    │   └── someFunctionsOneTestblah.py
    ├── test_resources
    │   ├── sample-data.json
    │   └── test-data-blahablahb.csv
    └── unit
        └── someFunctionsOne
            ├── one.py
            └── two.py

我想在pytest.ini中设置test_resources"的路径.因此,集成和单元测试知道在哪里可以找到该文件夹​​ - 我想避免在我的测试文件中使用这样的硬编码路径.

I want to set the path of "test_resources" in pytest.ini. So integration and unit tests know where to find that folder- I'd like to avoid hard coding paths like this in my test files themselves.

是否有在 pytest.ini 中设置任意配置并从测试中检索它的功能?我怀疑我的测试可能有其他类似的配置设置,如果所有这些都存在于 pytest.ini 文件中,那么对于这个项目的其他开发人员来说,事情会变得更加清晰——一个地方可以进行所有测试配置.我的 应用程序 已经有一个配置文件,它在启动时加载,但那是不同的.我想将单元/集成测试配置与我的应用程序配置隔离.pytest.ini 似乎是最好的地方,因为它已经存在并被测试使用.这样我就不需要创建另一个配置文件并滚动我自己的机制来为测试加载它

Is there a feature to set arbitrary config in pytest.ini and retrieve it from tests? I suspect I might have other config settings like this for my tests and if all that lives in the pytest.ini file that makes things much clearer for other devs on this project- one place to go for all test configuration stuff. I already have a configuration file for my application which is loaded when it starts but thats different. I want to isolate the unit/integration test config from my application config. pytest.ini seems like the best place because its already there and used by the tests. This way I dont need to create another config file and roll my own mechanism for loading it for the tests

另外,我知道没有什么可以阻止我使用 configparser 甚至自己加载和解析 pytest.ini 但如果测试已经在使用它我希望有一个内置功能可以从中读取任意 kvs 或类似的东西那个

Also, I know there is nothing preventing me from using configparser or even loading and parsing pytest.ini myself but if tests are already using it I was hoping there would be a built in feature to read arbitrary kvs from it or something like that

推荐答案

您在 pytest.ini 中定义自定义键的方式与定义自定义命令行参数的方式相同,仅使用 Parser.addini 方法:

You define custom keys in pytest.ini same way as you define custom command line arguments, only using the Parser.addini method:

# conftest.py

def pytest_addoption(parser):
    parser.addini("mykey", help="help for my key", default="fizz")

(注意pytest_addoption 钩子实现应该位于顶级conftest.py).

(Note that pytest_addoption hook impls should be located in the top-level conftest.py).

您现在可以在pytest.ini中定义mykey:

[pytest]
mykey = buzz

在测试中访问 mykey 值:

def test_spam(request):
    value = request.config.getini("mykey")
    assert value == "buzz"

这篇关于可以在 pytest.ini 中设置测试的任意配置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆