首页 > 技术文章 > python-pytest学习(七)-命令行传参

zhaocbbb 2020-05-05 10:25 原文

一、前言

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

  对应的参数名称是html,参数值是report.html

二、conftest配置参数

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

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

if __name__ == "__main__":
    pytest.main(["-s","test_case1.py"])

运行结果

 

 

 

 

三、带参数启动

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

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

运行结果:

 

参考文章:https://www.jianshu.com/p/983412a7c2ae

推荐阅读