如何在命令行中将环境变量传递给 pytest 以测试函数 [英] How to pass an environment variable in command line to pytest to test a function
问题描述
我有一个脚本使用 os.environ.get() 从命令行获取变量,例如 JENKINS_HOST="xx" JENKINS_AUTH="xx" JENKINS_TOKEN="xx" python script.py
在 script.py 中有一个像这样的函数:
def init_auth():登录模式 = 真JENKINS_HOST = os.environ.get("JENKINS_HOST")JENKINS_AUTH = os.environ.get("JENKINS_AUTH")JENKINS_TOKEN = os.environ.get("JENKINS_TOKEN")
当我使用pytest测试init_auth()函数时,如何将cli环境转移到这个函数上?
我不确定我是否完全理解您的问题..基本上,您想从 CLI 中检索值而不是从环境中检索值?
>
如果是这样,我这样做的一种方法是在与测试相同的目录中创建一个 conftest.py
文件并使用 pytest_addoption
和 pytest_generate_tests
钩子.
conftest.py:
def pytest_addoption(解析器):"""将 CLI 选项添加到 `pytest` 以将这些选项传递给测试用例.这些选项在 `pytest_generate_tests` 中使用."""parser.addoption('--jenkins-host')parser.addoption('--jenkins-auth')parser.addoption('--jenkins-token')def pytest_generate_tests(metafunc):metafunc.parametrize('jenkins_host, jenkins_auth, jenkins_token',[(metafunc.config.getoption('jenkins_host'),metafunc.config.getoption('jenkins_auth'),metafunc.config.getoption(jenkins_token'))]
TestFile.py
class TestThis:def test_my_thing(jenkins_host, jenkins_auth, jenkins_token):# 在这里做测试
CLI
pytest TestFile.py --jenkins-host "http://my-jenkinshost.com" --jenkins-auth 不管 --jenkins-token THE_TOKEN
测试用例中的参数是参数化的(相当于在pytest_generate_tests
中添加注释@pytest.mark.parametrize(...)
.
这很有效,而且 pytest
完全支持它.这是一个基本示例,因为您可以执行更多操作.请参阅此处有关这些和其他挂钩如何工作的更多信息.>
I have a script using os.environ.get() to get variable from command line something like JENKINS_HOST="xx" JENKINS_AUTH="xx" JENKINS_TOKEN="xx" python script.py
In script.py has a function it likes this:
def init_auth():
login_mode = True
JENKINS_HOST = os.environ.get("JENKINS_HOST")
JENKINS_AUTH = os.environ.get("JENKINS_AUTH")
JENKINS_TOKEN = os.environ.get("JENKINS_TOKEN")
when I use pytest to test the function init_auth(), how could I transfer the cli environment to this function?
I'm not sure I quite understood your question.. basically, instead of retrieving values from the environment, you want to retrieve them from the CLI?
If so, one way I did that was by creating a conftest.py
file in the same directory as the test and use the pytest_addoption
and pytest_generate_tests
hooks.
conftest.py:
def pytest_addoption(parser):
"""
Add CLI options to `pytest` to pass those options to the test cases.
These options are used in `pytest_generate_tests`.
"""
parser.addoption('--jenkins-host')
parser.addoption('--jenkins-auth')
parser.addoption('--jenkins-token')
def pytest_generate_tests(metafunc):
metafunc.parametrize(
'jenkins_host, jenkins_auth, jenkins_token',
[(metafunc.config.getoption('jenkins_host'),
metafunc.config.getoption('jenkins_auth'),
metafunc.config.getoption(jenkins_token'))]
TestFile.py
class TestThis:
def test_my_thing(jenkins_host, jenkins_auth, jenkins_token):
# Do tests here
CLI
pytest TestFile.py --jenkins-host "http://my-jenkinshost.com" --jenkins-auth whatever --jenkins-token THE_TOKEN
The arguments in the test case are parametrized (the equivalent of adding the annotation @pytest.mark.parametrize(...)
in pytest_generate_tests
.
This works well and it's fully supported by pytest
. It's a basic example as there's a lot more you can do. See here more info on how these and other hooks work.
这篇关于如何在命令行中将环境变量传递给 pytest 以测试函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!