Python,PyTest 是否可以同时添加长参数和短参数? [英] Python, PyTest is it possbile to add both long and short args?

查看:44
本文介绍了Python,PyTest 是否可以同时添加长参数和短参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试将 args 添加到我的 pytest 测试中,我尝试过类似的操作

Hello i'm trying to add args to my pytest tests I've tried something like

#conftest.py content
def pytest_addoption(parser):
    parser.addoption("-t", "--test", action="store")

但这给了我:

AttributeError: 'Argument' 对象没有属性 'dest'

AttributeError: 'Argument' object has no attribute 'dest'

我宁愿不必将 -t 和 --test 作为单独的参数读取.

i rather not have to read -t and --test as separate args.

推荐答案

我不知道您为什么会收到此错误消息.如果我这样做,我会得到:

I'm not sure why you get this error message. If I do the same, I get:

ValueError: lowercase shortoptions reserved

意味着 pytest 将所有小写选项缩写保留给自己,因此不可能做你想做的事.如果要添加缩写,则必须使用大写字母:

meaning that pytest reserves all lower-case option abbreviations to itself, so it is not possible to do what you want. If you want to add an abbreviation, you have to use an upper case one instead:

def pytest_addoption(parser):
    parser.addoption("-T", "--test", action="store")

错误信息

AttributeError: 'Argument' object has no attribute 'dest'

如果您使用无效的缩写(超过一个字母),实际上会显示,例如:

is actually shown if you use an invalid abbreviation (with more than one letter), for example:

def pytest_addoption(parser):
    parser.addoption("-tt", "--test", action="store")

该消息具有误导性 - 实际的错误消息是正确的,但由于 pytest 中的错误处理出现故障,该错误不会传播给调用者,而是您会收到该消息.

The message is misleading - the actual error message is correct, but due to a glitch in the error handling in pytest the error is not propagated to the caller, and you get that message instead.

至于您收到此消息的原因 - 您的测试与您显示的测试不同,或者在您的 pytest 版本中,正确的错误(小写shortoptions reserved)也被错误地传播.

As to why you get this message - either your test was different from the one you have shown, or in your version of pytest the correct error (lowercase shortoptions reserved) is also incorrectly propagated.

这篇关于Python,PyTest 是否可以同时添加长参数和短参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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