前言

命令行参数是根据命令行选项将不同的值传递给测试函数,比如平常在cmd执行"pytest --html=report.html",这里面的"--html=report.html"就是从命令行传入的参数。对应的参数名称是html,参数值是report.html.

conftest配置参数

1.首先需要在conftest.py添加命令行选项,命令行传入参数"--cmdopt",用例如果需要到从命令行传入的参数,就调用cmdopt函数:

#conftest.py
import pytest
def pytest_addoption(parser):
    parser.addoption(
        "--cmdopt",action="store",default="type1",
        help="my option:type1 or type2"
    )
@pytest.fixture
def cmdopt(request):
    return request.config.getoption("--cmdopt")

2.测试用例编写案例

#test_answers.py
import pytest
def test_answer(cmdopt):
    if cmdopt=="type1":
        print("first")
    elif cmdopt=="type2":
        print("second")
    assert 0 #to see what was printed
if __name__=="__main__":
    pytest.main(["-s","test_answers.py"])#这两种写法,执行结果是一样的

cmd打开,输入指令启动,也可以在pycharm里面右键执行上面代码

pytest -s test_answers.py

运行结果;可以看到结果显示Ffirst,F代表Fail,first就是type1时的输出。

D:\study\xyautotest>pytest -s test_answers.py
============================================================ test session starts ====================================================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfa
ilures-9.1.1, xdist-2.3.0, tep-0.8.9
collected 1 item                                                                                                                             

test_answers.py first
F

================================================================= FAILURES =========================================================
_______________________________________________________________ test_answer1 ______________________________________________________

cmdopt = 'type1'

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

test_answers.py:8: AssertionError
========================================================== short test summary info ==================================================
FAILED test_answers.py::test_answer1 - assert 0
============================================================= 1 failed in 0.26s =====================================================

D:\study\xyautotest>

带参数启动

1.如果不带参数执行,那么传默认的default=“type1”,接下来在命令行带上参数去执行

pytest -s test_answers.py --cmdopt=type2

 运行结果:

D:\study\xyautotest>pytest -s test_answers.py --cmdopt=type2
============================================================ test session starts ====================================================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfa
ilures-9.1.1, xdist-2.3.0, tep-0.8.9
collected 1 item                                                                                                                             

test_answers.py second
F

========================================================== FAILURES ==============================================================
_____________________________________________________________ test_answer1 __________________________________________________________

cmdopt = 'type2'

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

test_answers.py:8: AssertionError
===================================================== short test summary info =====================================================
FAILED test_answers.py::test_answer1 - assert 0
========================================================= 1 failed in 0.25s ======================================================

D:\study\xyautotest>

pytest(十一)--assert断言