Pytest_Hook函数pytest_addoption(parser):定义自己的命令行参数(14-1)
考虑场景:
- 我们的自动化用例需要支持在不同测试环境运行,有时候在dev环境运行,有时候在test环境运行;
- 有时候需要根据某个参数不同的参数值,执行不同的业务逻辑;
上面的场景我们都可以通过“在命令行中输入参数,然后用例中接收这个参数,通过判断这个参数的值来做不同的逻辑”来实现。那么我们的需求就变为pytest中如何自定义一个命令行参数呢?这时候我们就需要用到pytest的钩子函数:pytest_addoption
在conftest.py文件中定义命令名
新建一个conftest.py文件
然后在conftest.py文件中通过pytest_addoption方法来添加命令行参数,通过定义的fixture来获得参数的值。
- import pytest
- # pytest_addoption(parser) 定义自己的命令行参数的固定写法
- def pytest_addoption(parser):
- # 定义 --env_opt 参数名
- parser.addoption("--env_opt")
- # 定义 --run_level 参数名
- # 参数说明:
- # default:当命令行不调用参数时的默认值
- # help:在帮助中显示的说明
- parser.addoption("--run_level", default=1, help="执行用例的级别", action="store")
- # 获取--env_opt参数值
- @pytest.fixture(scope="session")
- def env_opt(request):
- return request.config.getoption("--env_opt")
- # 获取--run_level参数值
- @pytest.fixture(scope="session")
- def run_level(request):
- return request.config.getoption("run_level")
- @pytest.fixture(scope="session", autouse=True)
- def start_appium_desired(env_opt, run_level):
- print(f"\n{env_opt}")
- print(f"\n{run_level}")
上面conftest.py中新增了两个命令行参数:--env_opt 和 --run_level,然后定义了两个fixture,在测试用例中想要获得参数 --env_opt 和 --run_level 的值,就可以调用 env_opt 或 run_level 函数获取对应的值。
在用例中获取定义命令的值
- class TestDemo:
- def test_001(self, env_opt):
- print(f"\nenv_opt参数值:{env_opt}")
- def test_002(self, run_level):
- print(f"\nrun_level参数值:{run_level}")
调用定义的命令
- import pytest
- import os
- if __name__ == '__main__':
- env = {
- "host": "127.0.0.1",
- "port": "6789"
- }
- pytest.main(["-vs", "--alluredir=./temp", f"--env_opt={env}"])
- os.system("allure generate ./temp -o ./report/ --clean")
执行结果
Pytest_Hook函数pytest_addoption(parser):定义自己的命令行参数(14-1)的更多相关文章
- getopt函数的使用——分析命令行参数
getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...
- 【转】getopt分析命令行参数
(一) 在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如: ./a.out -a1234 -b432 -c -d 程序会根据读取的参数执行相应的操作,在C语言中,这个 ...
- getopt 分析命令行参数 -n -t 1
在Linux中,我们常常用到 ls -l 等等之类带有选项项的命令,下面,让我们用C++来实现该类似的命令. 在实现之前,首先,我们来介绍一下一个重要函数:getopt() 表头文件 #include ...
- getopt(分析命令行参数)
ref:http://vopit.blog.51cto.com/2400931/440453 相关函数表头文件 #include<unistd.h>定义函数 ...
- 3.QT中QCommandLineParser和QCommandLineOption解析命令行参数
1 新建项目 main.cpp #include <QCoreApplication> #include <QCommandLineParser> #include & ...
- python 命令行参数——argparse模块的使用
以下内容主要来自:http://wiki.jikexueyuan.com/project/explore-python/Standard-Modules/argparse.html argparse ...
- [日常] Go语言圣经-命令行参数
1.编译 go build hello.go 2.go get gopl.io/ch1/helloworld 命令,就会从网上获取代码,并放到对应目录中 下载的代码会放在$GOPATH/src/gop ...
- 转载:linux编程,命令行参数输入getopt
下面资料来自百度百科: getopt(分析命令行参数) 相关函数 表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const ...
- golang-flag - 命令行参数解析
flag - 命令行参数解析 在写命令行程序(工具.server)时,对命令参数进行解析是常见的需求.各种语言一般都会提供解析命令行参数的方法或库,以方便程序员使用.如果命令行参数纯粹自己写代码解析, ...
随机推荐
- 【Linux】【Services】【Disks】bftfs
1. 简介 1.1 Btrfs(B-tree,Butter FS,Better FS) 1.2. 遵循GPL,由oracle在2007年研发,支持CoW 1.3. 主要为了替代早期的ext3/ext4 ...
- 【C/C++】编码(腾讯)
假定一种编码的编码范围是a ~ y的25个字母,从1位到4位的编码,如果我们把该编码按字典序排序,形成一个数组如下: a, aa, aaa, aaaa, aaab, aaac, - -, b, ba, ...
- linux基本操作命令2
复制文件 格式: cp [参数] [ 被复制的文件路径] [ 复制的文件路径] -r :递归复制 (需要复制文件夹时使用) 案例:将/root目录下的test文件夹及其内部的文件复制到/tmp中 [ ...
- AtCoder Beginner Contest 173 题解
AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...
- CF1461A String Generation 题解
Content 构造一个仅由 a,b,c 三个字符组成,且最长回文子串长度不超过 \(k\) 的长度为 \(n\) 的字符串. 数据范围:数据组数 \(\leqslant 10\),\(1\leqsl ...
- mysql联合查询更新数据库例子
mysql联合查询更新数据库例子,用户表,部门表,把用户表中的部门属性更新为部门表的主键UPDATE user_table AS utINNER JOIN belongdept AS bd ON bd ...
- UEditor富文本判断是否输入内容
<textarea name="CONTENT" id="CONTENT" maxlength="4000" style=" ...
- 【LeetCode】845. Longest Mountain in Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双数组 参考资料 日期 题目地址:https://l ...
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- 【LeetCode】769. Max Chunks To Make Sorted 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...