如何将parser.addoption放在测试模块中,而不是放在conftest.py中?

2024-06-16 10:36:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下几点

conftest.py

def pytest_addoption(parser):
    parser.addoption('--sopt', action='store', default=None, help='Source Data Storage')

my_test.py

@pytest.fixture(scope='module', autouse=True)
def sopt(pytestconfig):
    return pytestconfig.getoption('sopt')


def test_mtest(sopt):
    //pytest code

运行此测试时,它可以正常工作python3 -m pytest --sopt=aaaaa,但在移动时

def pytest_addoption(parser):
    parser.addoption('--sopt', action='store', default=None, help='Source Data Storage')

conftest.pymy_test.py它不工作,并失败,出现以下错误:

ERROR: usage: __main__.py [options] [file_or_dir] [file_or_dir] [...]
__main__.py: error: unrecognized arguments: --sopt=aaaaa

我不希望只有两行代码就有一个单独的文件

有没有办法把parser.addoptionconftest.py放到my_test.py中并使其工作起来?


Tags: storepytestnonedefaultparsersourcepytest
1条回答
网友
1楼 · 发布于 2024-06-16 10:36:43

否。引用^{}钩子文档:

Note:

This function should be implemented only in plugins or conftest.py files situated at the tests root directory due to how pytest discovers plugins during startup.

相关问题 更多 >