具有多个命令行选项的 pytest

Posted

技术标签:

【中文标题】具有多个命令行选项的 pytest【英文标题】:pytest with multiple command line options 【发布时间】:2017-07-04 19:44:01 【问题描述】:

我对 pytest 的一个命令行参数没有任何问题。

当我运行程序时:python -m pytest -q -v --confcutdir=/usr/local/penguin/home/px09/p001 --cmdopt=type1 test-suite.py

我得到了预期的响应:

================================================ FAILURES ================================================
______________________________________________ test_answer _______________________________________________

cmdopt = 'type1'

    def test_answer(cmdopt):
        if cmdopt == "type1":
            print ("first")
        elif cmdopt == "type2":
            print ("second")
>       assert 0 # to see what was printed
E       assert 0

test-suite.py:7: AssertionError
------------------------------------------ Captured stdout call ------------------------------------------
first
======================================== 1 failed in 0.01 seconds ==============

当我尝试多个参数时,我遇到了问题

test_sample.py 的内容

def test_answer(cmdopt):
    if cmdopt == "type1":
        print ("first")
    elif cmdopt == "type2":
        print ("second")
    assert 0 # to see what was printed

def test_answer2(cmdopt2):
    if cmdopt2 == "type1":
        print ("first")
    elif cmdopt2 == "type2":
        print ("second")
    assert 0 # to see what was printed

conftest.py 的内容

import pytest

def pytest_addoption(parser):
    parser.addoption("--cmdopt", action="store", default="type1",
        help="my option: type1 or type2")
    parser.addoption("--cmdopt2", action="store", default="type3",
                     help="my option: type3 or type4")

@pytest.fixture
def cmdopt(request):
    return request.config.getoption("--cmdopt")

def cmdopt2(request):
    return request.config.getoption("--cmdopt2")

____________________________________ 设置 test_answer2 时出错 _____________________________________ 文件 /usr/local/penguin/home/px09/p001/test-suite.py,第 9 行 def test_answer2(cmdopt2):找不到 E 夹具“cmdopt2” > 可用的固定装置:缓存、capfd、capsys、cmdopt、doctest_namespace、monkeypatch、pytestconfig、record_xml_property、 recwarn, tmpdir, tmpdir_factory > 使用 'pytest --fixtures [testpath]' 获取帮助。

/usr/local/penguin/home/px09/p001/test-suite.py:9

================================================ FAILURES ================================================
______________________________________________ test_answer _______________________________________________

cmdopt = 'type1'

    def test_answer(cmdopt):
        if cmdopt == "type1":
            print ("first")
        elif cmdopt == "type2":
            print ("second")
>       assert 0 # to see what was printed
E       assert 0

test-suite.py:7: AssertionError
-

【问题讨论】:

【参考方案1】:

我认为问题很简单。只需在 cmdopt2 函数之前添加 @pytest.fixture 装饰器

@pytest.fixture
def cmdopt(request):
return request.config.getoption("--cmdopt")

@pytest.fixture
def cmdopt2(request):
    return request.config.getoption("--cmdopt2")

【讨论】:

以上是关于具有多个命令行选项的 pytest的主要内容,如果未能解决你的问题,请参考以下文章

pytest基础----常用的命令行选项

pytest-09-参数化parametrize+命令行传参

Pytest权威教程25-配置

pytest文档10-命令行传参

来自命令行的 xfail pytest 测试

python内置库--argparse