BDD - Python Behave Runner Script

2023-12-26 05:28
文章标签 python script bdd runner behave

本文主要是介绍BDD - Python Behave Runner Script,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

BDD - Python Behave Runner Script

  • 引言
  • Runner Script
    • subprocess.run 调用 Behave 命令行
    • 调用 Behave 提供的 API behave_main

引言

通过终端命令执行 Behave 测试用例,有时 IDE 重启了,还得重新敲一遍命令,很是麻烦,说实话我都懒得记那些命令选项,于是我就想是否找个偷懒办法,当然是可以的,我们可以创建一个 Runner 脚本文件,每次运行这个脚本文件就可以了。

想了解更多 Behave 相关的文章,欢迎阅读《Python BDD Behave 系列》,持续更新中。

Runner Script

首先在 features 文件夹同级目录创建一个 runner.py 文件。代码实现有两种,一种利用 subprocess.run 直接执行 Behave 的命令行参数,另外一种就是使用 Behave 提供的API behave_main ,而不是直接调用其命令行工具。

在这里插入图片描述

subprocess.run 调用 Behave 命令行

定义一个 run_behave 方法,用来构建 Behave 命令选项,通过 subprocess.run 执行 Behave 命令。调用 run_behave 方法只需传需要的参数就可以了。

例如:运行 BDD/Features/tag_example 下,标签为 @cart 的测试用例,允许终端 Print 输出,同时生成 html 测试报告,就可以这样调用了
run_behave(feature = “BDD/Features/tag_example”, tags = “cart”, no_capture= True, html_report= True)

import subprocessdef run_behave(feature, tags, no_capture, html_report):behave_command = ["behave"]# Include feature if providedif feature:behave_command.append(feature)# Include tags if providedif tags:behave_command.extend(["--tags", tags])# Include --no-capture optionif no_capture:behave_command.append("--no-capture")  # Include html reportif html_report:behave_command.extend(["-f", "behave_html_formatter:HTMLFormatter","-o", "BDD/output/report.html"])     try:subprocess.run(behave_command, check=True)except subprocess.CalledProcessError as e:print(f"Behave execution failed. Return code: {e.returncode}")# Handle the error or raise an exception if neededif __name__ == "__main__":run_behave(feature = "BDD/Features/tag_example",tags = "cart",no_capture= True,html_report= True)

终端输出,这里有一些 hooks 输出:

PS C:\Automation\Test> & C:/Users/xxx/AppData/Local/Programs/Python/Python39/python.exe c:/Automation/Test/BDD/runner.py
Before all tests
Before feature: Shopping Cart and Order Process
Before tag: cart
Before tag: smoke
Before scenario: Guest user adds items to the cart
Before step: the user is on the home page
After step: the user is on the home page
Before step: the user adds an item to the cart
After step: the user adds an item to the cart
Before step: the user should see the item in the cart
After step: the user should see the item in the cart
After scenario: Guest user adds items to the cart
After tag: cart
After tag: smokeBefore tag: cart
Before tag: regression
Before scenario: Registered user removes items from the cart
Before step: the user is logged in
After step: the user is logged in
Before step: the user has items in the cart
After step: the user has items in the cart
Before step: the user removes an item from the cart
After step: the user removes an item from the cart
Before step: the user should see the updated cart
After step: the user should see the updated cart
After scenario: Registered user removes items from the cart
After tag: cart
After tag: regressionAfter feature: Shopping Cart and Order Process
After all tests
1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 2 skipped
7 steps passed, 0 failed, 9 skipped, 0 undefined
Took 0m0.005s

Html 报告:
在这里插入图片描述

调用 Behave 提供的 API behave_main

定义一个 run_behave 方法,构建 Behave 参数,通过 behave_main.run_behave 传参来执行测试用例。
这里需要注意一下 behave 1.2.6 版本会有异常:AttributeError: 'list' object has no attribute 'version'
据说 behave 1.2.5 没有问题,小伙伴们可以试一下。

# runner.pyfrom behave import __main__ as behave_maindef run_behave(features_path='features', tags=None):# 构建 Behave 参数behave_args = [features_path]# 添加标签过滤if tags:behave_args.extend(['--tags', tags])# 使用 Behave API 运行测试behave_main.run_behave(behave_args)if __name__ == "__main__":# 你可以在这里设置默认的 features_path 和 tagsdefault_features_path = 'BDD/Features/tag_example'default_tags = None# 运行 Behave 测试run_behave(features_path=default_features_path, tags=default_tags)

终端输出:

PS C:\Automation\Test> & C:/Users/xxx/AppData/Local/Programs/Python/Python39/python.exe c:/Automation/Test/BDD/runner.py
Traceback (most recent call last):File "c:\Automation\Test\BDD\runner.py", line 22, in <module>run_behave(features_path=default_features_path, tags=default_tags)File "c:\Automation\Test\BDD\runner.py", line 14, in run_behavebehave_main.run_behave(behave_args)File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\site-packages\behave\__main__.py", line 67, in run_behaveif config.version:
AttributeError: 'list' object has no attribute 'version'
PS C:\Automation\Test> Behave --version
behave 1.2.6

这篇关于BDD - Python Behave Runner Script的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/538260

相关文章

Python将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Python Websockets库的使用指南

《PythonWebsockets库的使用指南》pythonwebsockets库是一个用于创建WebSocket服务器和客户端的Python库,它提供了一种简单的方式来实现实时通信,支持异步和同步... 目录一、WebSocket 简介二、python 的 websockets 库安装三、完整代码示例1.

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专