当前位置: 首页 > news >正文

[Pytest][Part 5]单条测试和用例集测试

目录

pytest 运行方式

命令行运行

主函数运行

通过pytest.ini来运行

封装主函数运行方式


这里完成需求3:可以运行单条test case,也可以根据case list 来运行list里面的case

这一部分比较简单,将pytest的运行命令封装一下就可以完成需求。

pytest 运行方式

pytest有3中运行方式:

1.命令行运行

2.主函数运行

3.配置文件pytest.ini运行

命令行运行

在cmd窗口运行命令如下,分三种运行方式

#运行当前目录下的所有测试case
pytest -vs#执行测试模块运行,运行test_sample.py下的所有case
pytest -vs test_sample.py#执行指定case
pytest -vs test_cases/test_sample.py::TestSample::test_addition

主函数运行

python的运行一般会有一个主函数来作为整个脚本的入口函数,pytest也有这样的一个入口函数,接收一个数组作为main函数的参数,也有三种运行方式

#运行执行测试caseif __name__ == "__main__":pytest.main(['-vs','test_cases/test_sample.py::TestSample::test_addition'])#执行当前目录下的所有caseif __name__ == "__main__":pytest.main(['-vs'])#执行指定模块的caseif __name__ == "__main__":pytest.main(['-vs','test_cases/test_sample.py'])

通过pytest.ini来运行

如果pytest.ini文件存在,那么pytest就会根据pytest.ini中的配置来运行测试

pytest.ini是pytest的核心配置文件,一般放在项目的根目录下。

addopts =  -vs                         #测试参数
testpaths = ./test_cases          #测试目录              
python_files = test_*.py           #执行所有以test_开头的文件
python_classes = Test*            #执行所有以Test开头的类
python_functions = test*          #执行所有以test开头的case

 上面是pytest默认的配置规则,当然也可以修改上面的配置文件来改变pytest默认的执行规则,比如可以修改成如下:

addopts =  -vs -n 2                        #测试参数,多个参数需要用空格隔开
testpaths = ./test_cases          #测试目录              
python_files = aaa_*.py           #执行所有以aaa_开头的文件
python_classes = Testbbb*            #执行所有以Testbbb开头的类
python_functions = testccc*          #执行所有以testccc开头的case

封装主函数运行方式

这里采用主函数的方式来运行测试脚本。

在上一篇文章中,我们把从命令行或者是配置文件中读取的测试配置都填充填ConfigTest中了,因此,现在我们要做的就是:

1. 从ConfigTest中读取到测试信息

2. 生成pytest的测试参数并传递给main()

def run_pytest(config):"""执行测试并生成报告"""# 默认参数pytest_args = ["-sv"]if config.single_test:pytest_args.extend([config.case_list])elif config.list_test:case_list = functionApi.read_case_from_list_file()pytest_args.extend(case_list)# 设置重复次数if int(config.repeat) > 1:pytest_args.extend(["--count", str(config.repeat)])# 执行测试exit_code = pytest.main(pytest_args)return exit_code

其中当运行测试集的时候,调用了read_case_from_list_file()来从测试集中读取到需要运行的case

    def read_case_from_list_file(self):test_list = "test_list.txt"# get path of test_list.txtfile_path = os.path.join(os.getcwd(), test_list)# print("os.path.abspath:", os.path.abspath(__file__))  #获取当前文件的绝对路径# print("os.getcwd()", os.getcwd())                     #获取调用该函数的项目路径# print("file path :", file_path)case_list = []try:with open(file_path) as file_handle:lines = file_handle.readlines()for line in lines:case_list.append(line.strip())except FileNotFoundError as e:logger.error(f"file not found:{e}")except PermissionError as e:logger.error(f"No permission to read the file:{e}")except Exception as e:logger.error(f"file not found: {e}")return case_list


文章转载自:
http://withal.sxnf.com.cn
http://explain.sxnf.com.cn
http://unprofitable.sxnf.com.cn
http://bagarre.sxnf.com.cn
http://gumming.sxnf.com.cn
http://nook.sxnf.com.cn
http://levirate.sxnf.com.cn
http://antipsychotic.sxnf.com.cn
http://reason.sxnf.com.cn
http://palpebral.sxnf.com.cn
http://subepidermal.sxnf.com.cn
http://libau.sxnf.com.cn
http://dissimulator.sxnf.com.cn
http://edbiz.sxnf.com.cn
http://unrepressed.sxnf.com.cn
http://skyway.sxnf.com.cn
http://hurried.sxnf.com.cn
http://pogonotomy.sxnf.com.cn
http://blinker.sxnf.com.cn
http://frae.sxnf.com.cn
http://sextuple.sxnf.com.cn
http://potamology.sxnf.com.cn
http://intrastate.sxnf.com.cn
http://anthroposere.sxnf.com.cn
http://conjecturable.sxnf.com.cn
http://pongee.sxnf.com.cn
http://screenload.sxnf.com.cn
http://inconsequent.sxnf.com.cn
http://scoundrelly.sxnf.com.cn
http://forcibly.sxnf.com.cn
http://www.dtcms.com/a/280762.html

相关文章:

  • 工业喷涂机器人的革新:艾利特协作机器人引领人机交互新纪元
  • 基于强化学习的智能体设计与实现:以CartPole平衡任务为例
  • 物联网系统中“时序数据库(TSDB)”和“关系型数据库(RDBMS)”
  • GD32VW553-IOT LED呼吸灯项目
  • 软考高级网络规划设计师2009-2024历年真题合集下载
  • AWS中国区资源成本优化全面指南:从理论到实践
  • 板凳-------Mysql cookbook学习 (十一--------11)
  • QT——QComboBox组合框控件
  • Filter(过滤器)
  • Kruskal重构树
  • AutoSQT 2025 第二届汽车软件质量与测试峰会开启报名!
  • wkhtmltopdf导出pdf调试参数
  • 无法判断项目进度中的关键路径,如何进行关键路径分析
  • 创客匠人:创始人 IP 的破局思维,重构知识变现的深层逻辑
  • 基于redis的分布式锁 lua脚本解决原子性
  • easy-ui中的相对路径和绝对路径问题
  • ARM GCC内联汇编
  • 如何轻松将音乐从安卓设备传输到安卓设备
  • 用vue自定义指令设置页面权限
  • Android 事件机制详解
  • day053-初识docker与基础命令
  • 广东省省考备考(第四十六天7.15)——判断推理:位置规律(听课后强化训练)
  • SD-WAN 技术在新能源行业的应用与优选方案分析
  • 【华为机试】3. 无重复字符的最长子串
  • 光米投影 多余之物的本思
  • 怎么样才能入门深度学习?
  • element plus使用插槽方式自定义el-form-item的label
  • NW917NW921美光固态闪存NW946NW952
  • 1.2 vue2(组合式API)的语法结构以及外部暴露
  • Vue框架之模板语法(插值表达式、指令系统、事件处理和表单绑定)全面解析