大佬教程收集整理的这篇文章主要介绍了Pytest - 如何从命令行传递函数参数,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些使用不同 pytest 标记运行的测试用例。我有一个配置文件,从中可以获取我在这些测试用例中使用的变量的值。现在配置文件增加了,而且还会继续增加。
最初我想我会从这里使用 pytest_generate_tests
https://docs.pytest.org/en/doc-warnings/parametrize.html
# content of main.py file
import Json
import pytest
import other_py_files
...
...
...
...
def configfile(conf):
config = open(str(conf) + ".Json",'r')
JsonData = config.read()
obj = Json.loads(JsonData)
globals()['name'] = obj['name']
globals()['Age'] = obj['Age']
globals()['dob'] = obj['dob']
globals()['Reg_No'] = obj['Reg_No']
@pytest.mark.testA
def testA(name,Age,dob,Reg_No,....,some parameters):
...........
...........
@pytest.mark.TESTB
def TESTB(name,other parameters):
..........
..........
这是conftest.py的内容。还有一些其他的 python 文件,我将它们导入到 main.py 文件中。
# content of conftest.py
def pytest_addoption(parser):
parser.addoption("--conf",action="append",default=[],Help="name of the conf file to pass to test functions")
def pytest_generate_tests(MetafunC):
if 'conf' in Metafunc.fixturenames:
Metafunc.parametrize("conf",Metafunc.config.option.conf)
现在我运行 pytest -q --conf="Config_XYZ" -m "testA"
但没有任何反应。我哪里错了?有没有更好的方法来做到这一点?
编辑 1:
Json 内容如下所示。我试图从中获取 Json 内容并通过调用它们的标记将其传递给测试。要打开的 Json 文件和要运行的测试必须通过命令行给出。
{
"name": "abc","Age": "123","dob": "00-00-0000","Reg_No": "12345678"
}
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的Pytest - 如何从命令行传递函数参数全部内容,希望文章能够帮你解决Pytest - 如何从命令行传递函数参数所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。