如何使用简短表单向Pytest添加自定义标志

发布于 2025-01-30 05:03:19 字数 1347 浏览 0 评论 0原文

我正在关注如何将自定义参数添加到Pytest中,以便使用自定义标记进行跳过测试。它有效,但我似乎无法自定义使用标志的简短版本。

我已经将这些功能添加到我的conftest.py

def pytest_addoption(parser):
    parser.addoption("-fs", dest='full_suite', action="store_true",
        help="Run the full test suite. Includes tests marked with the mark.full_suite flag")


def pytest_runtest_setup(item):
    if 'full_suite' in item.keywords and not item.config.getoption("-fs"):
        pytest.skip("You need the -fs option to run this test")

然后写下此测试:

@pytest.mark.full_suite
def test_something():
    assert True

如果我使用长选项,例如-fs,而不是<代码> -fs ,它将起作用,DEST将分配给fs。对于简短的选项,显然我需要提供dest关键字。但是我这样做了,我添加了dest ='full_suite。尽管如此,我还是得到了这个错误:

....
 self.option_id = str(option)
  File "/home/jokea/Carbonspace/cs-landsat-sentinel/.python3.8_env/lib/python3.8/site-packages/_pytest/config/argparsing.py", line 326, in __repr__
    args += ["dest: " + repr(self.dest)]
AttributeError: 'Argument' object has no attribute 'dest'

我缺少什么?

I was following this answer about how to add custom arguments to pytest in order to skip tests with the custom marker. It works, but I can't seem to customize it to use a short version of the flag.

I've added these functions to my conftest.py:

def pytest_addoption(parser):
    parser.addoption("-fs", dest='full_suite', action="store_true",
        help="Run the full test suite. Includes tests marked with the mark.full_suite flag")


def pytest_runtest_setup(item):
    if 'full_suite' in item.keywords and not item.config.getoption("-fs"):
        pytest.skip("You need the -fs option to run this test")

And then I write this test:

@pytest.mark.full_suite
def test_something():
    assert True

If I use the long option, for example, --fs, rather than -fs, it will work, and dest will be assigned to fs. With the short option, I'm apparently required to supply the dest keyword. But I did this, I added dest='full_suite. Despite that, I get this error:

....
 self.option_id = str(option)
  File "/home/jokea/Carbonspace/cs-landsat-sentinel/.python3.8_env/lib/python3.8/site-packages/_pytest/config/argparsing.py", line 326, in __repr__
    args += ["dest: " + repr(self.dest)]
AttributeError: 'Argument' object has no attribute 'dest'

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文