zoukankan      html  css  js  c++  java
  • pytest十二:cmd命令行参数

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

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

     

    import pytest


    def pytest_addoption(parser):
    parser.addoption(
    '--cmdopt', action='store', default='type1', help='myoption: type1 or type2'
    )

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


    测试用例编写案例

    import pytest

    def test_answer(cmdopt):
    if cmdopt == 'type1':
    print('first')
    elif cmdopt =='type2':
    print('second')
    assert 0

    if __name__=='__main__':
    pytest.main()

     



    带参数启动
    1.如果不带参数执行,那么传默认的 default=”type1”,接下来在命令行带上参数去执行
    > pytest -s test_19_cmd.py --cmdopt=type2
    2.命令行传参数有两种写法,迓有一种分成 2 个参数也可以的,参数和名称用空格隔开
    > pytest -s test_19_cmd.py --cmdopt type2






  • 相关阅读:
    家庭记账本(三)记录页面的完善
    家庭记账本(二)记录界面显示
    家庭记账本(一)页面绘制
    MyBaits增删改查
    2021.01.20
    Maven_依赖与生命周期
    1592. Rearrange Spaces Between Words
    1588. Sum of All Odd Length Subarrays
    933. Number of Recent Calls
    765. Couples Holding Hands
  • 原文地址:https://www.cnblogs.com/zhongyehai/p/9685862.html
Copyright © 2011-2022 走看看