前言

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

    来自:http://www.cnblogs.com/tibetwolf/articles/1785744.html ------------------------------------------ ...

  2. POJ 3253 Fence Repair(哈夫曼编码)

    题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...

  3. (五)Spring 对事务的支持

    第一节:事务简介 满足一下四个条件: 第一:原子性: 第二:一致性: 第三:隔离性: 第四:持久性: ------------------------------------------------- ...

  4. Linux命令之dig命令实例讲解

    1.查看域名的A记录 # dig yahoo.com; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6_3.2 <<> ...

  5. IEEEXtreme 10.0 - Goldbach's Second Conjecture

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Goldbach's Second Conjecture 题目来源 第10届IEEE极限编程大赛 https ...

  6. Python中列表的各种方法

    列表是Python中一种常用的存储信息的方式,所以要熟练掌握列表的各种方法: 首先我们定义一个列表(name),然后练习里面的各种方法: >>> name = ["Sora ...

  7. c# 递归异步获取本地驱动器下所有文件

    //获取所有驱动器 string[] drives = Environment.GetLogicalDrives(); foreach (string driver in drives) { Cons ...

  8. 在 Ubuntu 系统安装 Redi laravel 5.2 引入第三方类

    composer 安装类依赖包 很受用 也很方便 但是要是一个有一定规模的公司技术团队 因为要照顾大局 还是引入类好些 下面是引入类的方法 1.首先在app目录下创建一个新的文件夹,命名Tools(可 ...

  9. Java线程池使用和常用参数(待续)

    线程池怎么实现的,核心参数讲一讲? Executors是线程池的工厂类,通过调用它的静态方法如下: Executors.newCachedThreadPool(); Executors.newFixe ...

  10. Implementing x / 6 Using Only Bit Manipulations

    This is an interesting question from one of the lab assignments in Introduction to Computer Systems, ...