如何将环境变量传递给pytest [英] How to pass environment variables to pytest

查看:57
本文介绍了如何将环境变量传递给pytest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Python 项目中开始执行测试之前,我读取了一些环境变量并使用读取的这些值设置了一些变量.我的测试将根据读取的这些值在所需的环境中运行.

Before I start executing the tests in my Python project, I read some environment variables and set some variables with these values read. My tests will run on the desired environment based on these values read.

例如:假设环境变量被称为 ENV_NAMEENV_NUMBER

For eg: Let's say the environment variables are called ENV_NAME and ENV_NUMBER

现在,我想使用 py.test 运行测试.

Now, I would like to run the tests using py.test.

如果我对这些环境变量进行硬编码,例如:ENV_NAME = 'staging', ENV_NUMBER = '5' 在我的代码中,然后通过在根目录执行 py.test 命令来运行测试项目目录下,所有测试运行成功.

If I hard code these environment variables, for eg: ENV_NAME = 'staging', ENV_NUMBER = '5' in my code and then run the tests by executing the py.test command at the root of the project directory, all the tests run successfully.

但是,我不想对这些值进行硬编码.有没有办法,我可以将这些环境变量作为 py.test 的命令行参数发送?

But, I don't want to hardcode these values. Is there a way, I can send these environment variables as command line arguments for py.test?

我想更多的是

py.test -ENV_NAME='staging' -ENV_NUMBER='5'.

但是,这不起作用.

推荐答案

我终于找到了我一直在寻找的答案.

I finally found the answer i was looking for.

我们可以在使用 py.test 运行测试之前像这样设置环境变量

we can set the environment variables like this before running tests using py.test

ENV_NAME='staging' ENV_NUMBER='5' py.test

这篇关于如何将环境变量传递给pytest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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