pytest_命令行传参
前言
命令行参数是根据命令行选项将不同的值传递给测试函数,比如平常在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_命令行传参的更多相关文章
- 再探命令行传参之c与python
继上一次java命令行传参 python sys模块包括了一组非常实用的服务,内含很多函数方法和变量,用来处理Python运行时配置以及资源,从而可以与前当程序之外的系统环境交互,如:python解释 ...
- Java小tips之命令行传参
在命令行运行主函数时,后缀字符串,则会储存在args[]数组中,这种方法可以在程序运行时,借助Main函数传参 主类书写不规范见谅 ```java public class hello{ public ...
- Java方法:命令行传参,重载,可变参数,递归
Java方法:System.out.println()//系统类.out对象.输出方法Java方法是语句的集合,他们在一起执行一个功能方法是解决一类问题的步骤的有序组合方法包含于类或对象中方法在程序中 ...
- Day006 命令行传参
命令行传参 有时候你希望运行一个程序时候再传递给它消息,这要靠传递命令行参数给main()函数实现. 使用方法 写测试代码. public static void main(String[] args ...
- Java基础系列(30)- 命令行传参
命令行传参 有时候你希望运行一个程序的时候再传递给它消息.这就要靠传递命令行参数main()函数实现 package method; public class CommandLine { public ...
- 命令行传参是否只能针对main方法
先上结论 命令行传参只能针对main方法而言,而且格式严格 这里在原有主类test基础上创建了另一个类,名字为testCMD,但里面创建的方法并不是main方法,在cmd内编译后进行命令行传参,结果如 ...
- 09 Java的方法 方法的重载 命令行传参
3.方法的重载 重载就是在一个类中,有相同的函数名称,单形参不同的函数. 方法的重载的规则: 方法名称必须相同. 参数列表必须不同(个数不同.或类型不同.参数排序顺序不同等). 方法的返回类型可以相同 ...
- 命令行传参——JavaSE基础
命令行传参 可以在运行一个程序时再传递给它消息,可以依靠命令行传参给mian()函数实现 public class CommandLine(){ public static void main(Str ...
- 解决 main(int argc, char** argv)这种情况下通过命令行传参,改为main函数里面给定参数。
本文是原创文章,未经允许,请勿转载. 原来程序中是通过运行exe,然后加上参数,然程序运行起来的.也就是main(int argc, char** argv)这里是通过argv参数是从命令行 传过来的 ...
随机推荐
- 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛
XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...
- GoCN每日新闻(2019-11-01)
GoCN每日新闻(2019-11-01) GoCN每日新闻(2019-11-01) 1. Rob Pike 认为 Go 成功的 5 个因素 https://changelog.com/posts/5- ...
- GEE windows 环境配置
参照GEE开发大神的一篇文章GEE学习笔记 三十五:windows下配置本地开发环境 - 无形的风的文章 - 知乎. 按他的顺序下来还是出了点问题,再整理一下自己遇到的问题. 1.安装Google的 ...
- SpringBoot:使用feign调用restful服务时地址栏传参
1.服务提供者(controller层) @GetMapping("/user/{id}") public ApiResult getById(@PathVariable(&quo ...
- Linux 查看文件权限命令ls -l 输出信息每列所代表的含义
- C++ vector使用实例
C++ vector #include <iostream> #include <vector> #include <string> #include <al ...
- layui flow loading占位图实现方法
如果流图片要加载失败, 就会显示找不到图片的裂痕 代码如下: <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- django学习问题集
case 1: python manage.py migrate时报错:django.core.exceptions.ImproperlyConfigured: Error loading MySQL ...
- javafx这些学会后,开发就不难了,往tablecloumn列中添加按钮,修改javafx中tableview中tablecell中的值,修改完回车表示保存到内存中
javafx开发过程中遇见难题,往tablecloumn列中添加按钮 想了很久的方法,也配有办法判断每行中有数据的地方添加按钮set bank_caozuo.setCellFactory((col)- ...
- Transaction-Mybatis源码
github地址:https://github.com/dchack/Mybatis-source-code-learn (欢迎star) TransactionFactory 官方文档: 在 MyB ...