前言

命令行参数是根据命令行选项将不同的值传递给测试函数,比如平常在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

pytest文档10-命令行传参的更多相关文章

  1. 再探命令行传参之c与python

    继上一次java命令行传参 python sys模块包括了一组非常实用的服务,内含很多函数方法和变量,用来处理Python运行时配置以及资源,从而可以与前当程序之外的系统环境交互,如:python解释 ...

  2. Java小tips之命令行传参

    在命令行运行主函数时,后缀字符串,则会储存在args[]数组中,这种方法可以在程序运行时,借助Main函数传参 主类书写不规范见谅 ```java public class hello{ public ...

  3. Java方法:命令行传参,重载,可变参数,递归

    Java方法:System.out.println()//系统类.out对象.输出方法Java方法是语句的集合,他们在一起执行一个功能方法是解决一类问题的步骤的有序组合方法包含于类或对象中方法在程序中 ...

  4. Day006 命令行传参

    命令行传参 有时候你希望运行一个程序时候再传递给它消息,这要靠传递命令行参数给main()函数实现. 使用方法 写测试代码. public static void main(String[] args ...

  5. Java基础系列(30)- 命令行传参

    命令行传参 有时候你希望运行一个程序的时候再传递给它消息.这就要靠传递命令行参数main()函数实现 package method; public class CommandLine { public ...

  6. 命令行传参是否只能针对main方法

    先上结论 命令行传参只能针对main方法而言,而且格式严格 这里在原有主类test基础上创建了另一个类,名字为testCMD,但里面创建的方法并不是main方法,在cmd内编译后进行命令行传参,结果如 ...

  7. 09 Java的方法 方法的重载 命令行传参

    3.方法的重载 重载就是在一个类中,有相同的函数名称,单形参不同的函数. 方法的重载的规则: 方法名称必须相同. 参数列表必须不同(个数不同.或类型不同.参数排序顺序不同等). 方法的返回类型可以相同 ...

  8. 命令行传参——JavaSE基础

    命令行传参 可以在运行一个程序时再传递给它消息,可以依靠命令行传参给mian()函数实现 public class CommandLine(){ public static void main(Str ...

  9. pytest_命令行传参

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

随机推荐

  1. 百度地图sdk定位和遇到的坑

    封装定位服务类: import android.content.Context; import com.baidu.location.BDAbstractLocationListener; impor ...

  2. CircleIndicator

    dependencies { compile 'com.nineoldandroids:library:2.4.+' compile 'me.relex:circleindicator:1.0.0@a ...

  3. 得分(UVa1585)

    题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  4. awk练习总结

    >>> >>>awk是个优秀文本处理工具,可以说是一门程序设计语言.下面是awk内置变量. 一.内置变量表 属性 说明 $0 当前记录(作为单个变量) $1~$n ...

  5. Oracle 子查询和组函数练习

    SELECT * FROM emp; SELECT * FROM dept; 1.查询公司员工工资的最大值,最小值,平均值和总和. SELECT MAX(sal) AS 工资最大值, MIN(sal) ...

  6. HadoopMR-Spark-HBase-Hive

     YARN资源调度: 三种 FIFO 大任务独占 一堆小任务独占 capacity 弹性分配 :计算任务较少时候可以利用全部的计算资源,当队列的任务多的时候会按照比例进行资源平衡. 容量保证:保证队 ...

  7. Java String、StringBuilder和StringBuffer

    转载: Java String.StringBuilder和StringBuffer 概览 在Android/Java开发中,用来处理字符串常用的类有3种: String.StringBuilder. ...

  8. CodeForces 805C Find Amir

    直觉. 先走$1$走到$n$,然后从$n$走到$2$,然后从$2$走到$n-1$,然后从$n-1$走到$3$.一次花费为$0$,一次花费为$1$. #include <cstdio> #i ...

  9. react篇章-React 组件-向组件传递参数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  10. 「WC2010」重建计划(长链剖分/点分治)

    「WC2010」重建计划(长链剖分/点分治) 题目描述 有一棵大小为 \(n\) 的树,给定 \(L, R\) ,要求找到一条长度在 \([L, R]\) 的路径,并且路径上边权的平均值最大 \(1 ...