zoukankan      html  css  js  c++  java
  • pytest文档10-命令行传参

    前言

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

    conftest配置参数

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

    # content of 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.测试用例编写案例

    # content of test_sample.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_case1.py"])
    
    

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

    $ pytest -s test_sample.py

    运行结果:

    >pytest -s
    ============================= test session starts =============================
    test_sample.py first
    F
    
    ================================== 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_case1.py:8: AssertionError
    ========================== 1 failed in 0.05 seconds ===========================
    

    带参数启动

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

    $ pytest -s test_sample.py --cmdopt=type2

    test_sample.py second
    F
    
    ================================== FAILURES ===================================
    _________________________________ test_answer _________________________________
    
    cmdopt = 'type2'
    
        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_case1.py:8: AssertionError
    ========================== 1 failed in 0.05 seconds ===========================
    

    2.命令行传参数有两种写法,还有一种分成2个参数也可以的,参数和名称用空格隔开

    $ pytest -s test_case1.py --cmdopt type2

    ---------------------------------pytest结合selenium自动化完整版-------------------------

    全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b

    作者:上海-悠悠 QQ交流群:874033608

    也可以关注下我的个人公众号:yoyoketang

  • 相关阅读:
    IK分词器插件
    倒排索引
    logstash-安装、基本使用、入门
    Anaconda使用-详解
    java之反射
    Java中级路线jdbc第一天
    Java字符串及字符串的常用方法知识点总结
    Java基本类型的类包装知识点总结
    Java Class类知识点总结
    java异常类知识点总结
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/9457473.html
Copyright © 2011-2022 走看看