简体   繁体   English

conftest.py 可以从 pytest.ini 读取现有值吗?

[英]Can conftest.py _read_ an existing value from pytest.ini?

conftest.py can be used to define properties in pytest.ini with parser.addini() but can conftest.py also read existing values from pytest.ini? conftest.py 可用于使用parser.addini()定义 pytest.ini 中的属性,但 conftest.py 还可以从 pytest.ini 中读取现有值吗?

I would like to look at the value of a parameter in pytest.ini and use it to set the default value of another parameter in pytest.ini我想查看 pytest.ini 中一个参数的值,并用它来设置 pytest.ini 中另一个参数的默认值

Is this possible without having to use configparser myself in conftest.py?这是否可能无需在 conftest.py 中自己使用 configparser?

I would like to look at the value of a parameter in pytest.ini and use it to set the default value of another parameter in pytest.ini我想查看 pytest.ini 中一个参数的值,并用它来设置 pytest.ini 中另一个参数的默认值

Best is to add a custom impl of the pytest_configure hook.最好是添加pytest_configure钩子的自定义 impl。 Example: assume you have two custom ini options defined in your conftest.py :示例:假设您在conftest.py中定义了两个自定义 ini 选项:

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

Now spam should be set to bacon whenever fizz is not buzz (not the default value).现在spam应该设置为bacon ,只要fizz不是buzz (不是默认值)。 Extend conftest.py with:扩展conftest.py

def pytest_configure(config):
    fizz = config.getini("fizz")
    spam = config.getini("spam")
    print("values parsed from ini: fizz:", fizz, "spam:", spam)

    if not fizz == "buzz":
        # override parsed ini value
        config._inicache["spam"] = "bacon"

    print("spam was replaced to:", config.getini("spam"))

When running pytest -s (and fizz set to something else than buzz in pytest.ini ), you will get the following output:运行pytest -s时(并且在pytest.ini中设置的fizz声不是buzz ),您将获得以下 output:

values parsed from ini: fizz: fuzz spam: eggs
spam was replaced to: bacon
============================= test session starts =============================
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以在conftest.py之外定义pytest钩子吗? - can we define pytest hooks outside conftest.py? 有没有办法自定义从 conftest.py 登录 pytest 的格式 - Is there a way to customise the format for logging in pytest from conftest.py 在pytest中,conftest.py文件有什么用? - In pytest, what is the use of conftest.py files? 在Robot Framework上是否有conftest.py pytest等价物 - Is there a conftest.py pytest equivalent on Robot Framework PyTest - 在 conftest.py 中指定清理测试 - PyTest - Specify cleanup tests in conftest.py 如何在pytest中的conftest.py文件中传递命令行选项值 - How to pass command line option value within conftest.py file in pytest 除了 pytest 的测试 package 之外,conftest.py 文件是否可以放在另一个 package 中? - Can the conftest.py file be put in another package aside the tests package for pytest? 如何在 conftest.py 中设置会话详细信息以获取收益以及 pytest 中的夹具? - How can I set session details in conftest.py for yield along with fixture in pytest? 如何在 cmd.exe 中命令变量值从 conftest.py 到 pytest - how to command variables values from conftest.py to pytest in cmd.exe pytest html - 将图像从测试文件传递给 conftest.py 中的钩子 - pytest html - pass images to the hook in conftest.py from a test file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM