如何避免 pytest_jira 插件在 jira.cfg 中硬编码 username/password

How to avoid hard coded username/password in jira.cfg for pytest_jira plugin

我正在尝试将 pytest-jira 插件集成到我的 python 脚本中。我指的是这个 link.

~/jira.cfg 中以明文形式硬编码 username/password 是不可行的。有什么办法可以从外面覆盖它们吗?基本上,我正在寻找在 jira.cfg.

中跳过密码硬编码的方法
username = USERNAME (or blank for no authentication
password = PASSWORD (or blank for no authentication)

有人可以建议一种方法吗?

您可以通过定义自己的 pytest_configure 钩子来简单地覆盖参数。这就是 pytest 的美妙之处:您几乎可以重新定义所有内容并使其适应您的特定需求。从环境变量中读取 username/password 的示例:

# conftest.py

import os
import pytest


def pytest_configure(config):
    username = os.getenv('JIRA_USER', None)
    password = os.getenv('JIRA_PASSWORD', None)
    if username and password:
        config.option.jira_username = username
        config.option.jira_password = password

就是这样!测试一下:

$ export JIRA_USER=me JIRA_PASSWORD=mypass
$ pytest --jira