前言

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

  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. 解决 main(int argc, char** argv)这种情况下通过命令行传参,改为main函数里面给定参数。

    本文是原创文章,未经允许,请勿转载. 原来程序中是通过运行exe,然后加上参数,然程序运行起来的.也就是main(int argc, char** argv)这里是通过argv参数是从命令行 传过来的 ...

随机推荐

  1. C语言中常见的图形打印总结

    直角三角形(靠右直立) 示例实现代码如下: int main(){ int n; int i,j; cin >> n; if(n<= 0){ cout << " ...

  2. GoCN每日新闻(2019-10-27)

    GoCN每日新闻(2019-10-27) 1. Golab(意大利GopherCon)2019见闻 http://fedepaol.github.io/blog/2019/10/23/golab-20 ...

  3. 使用 Python 和 Flask 设计 RESTful API

    近些年来 REST (REpresentational State Transfer) 已经变成了 web services 和 web APIs 的标配. 在本文中我将向你展示如何简单地使用 Pyt ...

  4. Apache Kylin - 大数据下的OLAP解决方案

    OLAPCube是一种典型的多维数据分析技术,Cube本身可以认为是不同维度数据组成的dataset,一个OLAP Cube 可以拥有多个维度(Dimension),以及多个事实(Factor Mea ...

  5. 咏南跨平台中间件REST API

    主旨 1)为了中间件支持跨操作系统部署,客户端支持跨操作系统.跨设备.跨开发语言,特制订本REST API规约. 2)所有接口均支持HTTP GET\POST调用. 3)调用示例为DELPHI代码,其 ...

  6. Refused to execute script from '...' because its MIME type ('') is not executable, and strict MIME type checking is enabled.

    写在前面 部署项目到weblogic上启动首页访问空白, 浏览器控制台报如题错误. web.xml中把响应头添加防止攻击的报文过滤器禁用就行了(仅仅是为了启动), 以下为转载内容, 可以根据需要自行测 ...

  7. 在 RPA10.X 运行异常,RPA9 却正常的问题处理

    一.现象 RPA10.X 在一些极少数 win7/win10 环境中一运行就崩溃或无运行结果. 二.原因 出现上述现象已经确定是OPenGL 驱动兼容性造成的. 三.解决方法 方法一 只要把流程的管理 ...

  8. 【JavaScript】JavaScript中的ActiveXObject对象

    JavaScript中ActiveXObject对象是启用并返回 Automation 对象的引用.    使用方法: newObj = new ActiveXObject( servername.t ...

  9. java.lang.Exception: No tests found matching

    java.lang.Exception: No tests found matching 需要导入junit.jar 和 hamcrest.jar

  10. http 请求方式解析

    表单中<form></form>, 如果使用method="get"方式提交,则不会指定请求体编码方式.默认请求参数使用?拼接到url之后.如:http:/ ...