BDD - Python Behave 配置文件 behave.ini

BDD - Python Behave 配置文件 behave.ini

  • 引言
  • behave.ini
    • 配置参数的类型
    • 配置项
  • behave.ini 应用
    • feature 文件
    • step 文件
    • 创建 behave.ini
    • 执行 Behave

引言

前面文章 《BDD - Python Behave Runner Script》就是为了每次执行 Behave 时不用手动敲一长串选项,其实还有另外一种实现方式,那就是通过配置文件指定 Behave 的命令行参数和设置。今天就来了解一下 Behave 的配置。

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

behave.ini

behave.ini 文件是 Behave 的配置文件,用于设置 Behave 命令行选项,配置文件必须以标签 “[behave]” 开头。

配置参数的类型

  1. text 文本
    将提供的任何文本分配给配置设置
tags=smoke
  1. bool 布尔值
    为配置设置分配一个布尔值。文本描述当值为 true 时的功能。
    True 值为" 1 “,” yes ", " True “和” on "。
    False 值为“0”、“no”、“False”和“off”。
dry_run = false
  1. sequence 系列文本
    字段在新行上接受一个或多个值,例如 tags 的配置
    tag 带不带 @, Behave 都无所谓能识别到。
tags=@foo,~@bar@zap

相当于 --tags= @foo,~@bar --tags=@zap

配置项

基本上 Behave 的配置项跟 Behave 命令行选项是对应的,但是这里请注意配置项的名字并不是一一对应的,具体可参考官方文档 Behave Configuration

例如:
命令选项 –dry-run,配置项名字却是 dry_run
命令选项 –o 或 --outfile,配置项名字却是 outfiles

查看 Behave 命令行选项
执行命令:behave -h

PS C:\Automation\Test> behave -h
usage: behave [options] [ [DIR|FILE|FILE:LINE] ]+Run a number of feature tests with behave.positional arguments:paths                 Feature directory, file or file location (FILE:LINE).optional arguments:-h, --help            show this help message and exit-c, --no-color        Disable the use of ANSI color escapes.--color               Use ANSI color escapes. This is the default behaviour. This switch is    used to override a configuration file setting.-d, --dry-run         Invokes formatters without executing the steps.-D NAME=VALUE, --define NAME=VALUEDefine user-specific data for the config.userdata dictionary. Example:   -D foo=bar to store it in config.userdata["foo"].-e PATTERN, --exclude PATTERNDon't run feature files matching regular expression PATTERN.-i PATTERN, --include PATTERNOnly run feature files matching regular expression PATTERN.--no-junit            Don't output JUnit-compatible reports.--junit               Output JUnit-compatible reports. When junit is enabled, all stdout and   stderr will be redirected and dumped to the junit report, regardless of  the "--capture" and "--no-capture" options.--junit-directory PATHDirectory in which to store JUnit reports.-f FORMAT, --format FORMATSpecify a formatter. If none is specified the default formatter is       used. Pass "--format help" to get a list of available formatters.        --steps-catalog       Show a catalog of all available step definitions. SAME AS:--format=steps.catalog --dry-run --no-summary -q-k, --no-skipped      Don't print skipped steps (due to tags).--show-skipped        Print skipped steps. This is the default behaviour. This switch is used  to override a configuration file setting.--no-snippets         Don't print snippets for unimplemented steps.--snippets            Print snippets for unimplemented steps. This is the default behaviour.   This switch is used to override a configuration file setting.-m, --no-multiline    Don't print multiline strings and tables under steps.--multiline           Print multiline strings and tables under steps. This is the default      behaviour. This switch is used to override a configuration filesetting.-n NAME, --name NAME  Only execute the feature elements which match part of the given name.    If this option is given more than once, it will match against all the    given names.--no-capture          Don't capture stdout (any stdout output will be printed immediately.)    --capture             Capture stdout (any stdout output will be printed if there is afailure.) This is the default behaviour. This switch is used tooverride a configuration file setting.--no-capture-stderr   Don't capture stderr (any stderr output will be printed immediately.)    --capture-stderr      Capture stderr (any stderr output will be printed if there is afailure.) This is the default behaviour. This switch is used tooverride a configuration file setting.--no-logcapture       Don't capture logging. Logging configuration will be left intact.        --logcapture          Capture logging. All logging during a step will be captured anddisplayed in the event of a failure. This is the default behaviour.      This switch is used to override a configuration file setting.--logging-level LOGGING_LEVELSpecify a level to capture logging at. The default is INFO - capturing   everything.--logging-format LOGGING_FORMATSpecify custom format to print statements. Uses the same format as used  by standard logging handlers. The default is"%(levelname)s:%(name)s:%(message)s".--logging-datefmt LOGGING_DATEFMTSpecify custom date/time format to print statements. Uses the same       format as used by standard logging handlers.--logging-filter LOGGING_FILTERSpecify which statements to filter in/out. By default, everything is     captured. If the output is too verbose, use this option to filter out    needless output. Example: --logging-filter=foo will capture statements   issued ONLY to foo or foo.what.ever.sub but not foobar or other logger.  Specify multiple loggers with comma: filter=foo,bar,baz. If any logger   name is prefixed with a minus, eg filter=-foo, it will be excluded       rather than included.--logging-clear-handlersClear all other logging handlers.--no-summary          Don't display the summary at the end of the run.--summary             Display the summary at the end of the run.-o FILE, --outfile FILEWrite to specified file instead of stdout.-q, --quiet           Alias for --no-snippets --no-source.-s, --no-source       Don't print the file and line of the step definition with the steps.     --show-source         Print the file and line of the step definition with the steps. This is   the default behaviour. This switch is used to override a configuration   file setting.--stage STAGE         Defines the current test stage. The test stage name is used as name      prefix for the environment file and the steps directory (instead of      default path names).--stop                Stop running tests at the first failure.-t TAG_EXPRESSION, --tags TAG_EXPRESSIONOnly execute features or scenarios with tags matching TAG_EXPRESSION.    Pass "--tags-help" for more information.-T, --no-timings      Don't print the time taken for each step.--show-timings        Print the time taken, in seconds, of each step after the step has        completed. This is the default behaviour. This switch is used tooverride a configuration file setting.-v, --verbose         Show the files and features loaded.-w, --wip             Only run scenarios tagged with "wip". Additionally: use the "plain"      formatter, do not capture stdout or logging output and stop at the       first failure.-x, --expand          Expand scenario outline tables in output.--lang LANG           Use keywords for a language other than English.--lang-list           List the languages available for --lang.--lang-help LANG      List the translations accepted for one language.--tags-help           Show help for tag expressions.--version             Show version.

behave.ini 应用

下面通常简单的实例来应用一下 Behave 的配置,项目结构如下:

在这里插入图片描述

feature 文件

创建 shopping_cart.feature,加上一些 tags

# shopping_cart.featureFeature: Shopping Cart and Order Process@cart @smokeScenario: Guest user adds items to the cartGiven the user is on the home pageWhen the user adds an item to the cartThen the user should see the item in the cart@cart @regressionScenario: Registered user removes items from the cartGiven the user is logged inAnd the user has items in the cartWhen the user removes an item from the cartThen the user should see the updated cart@order @smokeScenario: Guest user places an orderGiven the user is on the home pageWhen the user adds an item to the cartAnd the user proceeds to checkoutAnd the user completes the orderThen the user should receive an order confirmation@order @regressionScenario: Registered user tracks an orderGiven the user is logged inAnd the user has placed an orderWhen the user checks the order statusThen the user should see the current order status

step 文件

创建 calculator_steps.py 文件

# calculator_steps.pyfrom behave import given, when, then@given('the calculator is turned on')
def step_calculator_turned_on(context):context.calculator_on = True    @when('I add {num1:d} and {num2:d}')
def step_add_numbers(context, num1, num2):context.result = num1 + num2@then('the result should be {expected_result:d}')
def step_check_result(context, expected_result):assert context.result == expected_result, f"Actual result: {context.result}, Expected result: {expected_result}"

创建 behave.ini

这里将配置文件和 feature, steps 文件夹是同级的, 配置了一些常用的 Behave 命令选项:

  • 配置 feature 的路径:paths=BDD/Features/tag_example
  • disable dry-run 执行步骤:dry_run = false
  • 配置标签为 smoke 且 为 cart 的测试用例:tags=smoke
    cart
  • 配置 format 为自定义的 my_html:format = my_html
  • 配置输出文件为 my_report.html :outfiles = my_report.html
  • 配置自定义输出格式 my_html
 [behave.formatters]
my_html = behave_html_formatter:HTMLFormatter

上面就是配置自定义的 my_html 的输出格式是 behave_html_formatter

behave.formatters 模块是 Behave 框架中用于处理格式化输出的模块,可以自定义。它包含了一些用于定义和管理不同输出格式的类。这些类负责将测试结果以各种方式呈现,比如在终端上显示、生成报告文件等。详情请参考 Behave Formatter

所以整个配置内容如下:

# behave.ini
[behave]
paths=BDD/Features/tag_example
dry_run = false
tags=smokecart
format = my_html
outfiles = my_report.html[behave.formatters]
my_html = behave_html_formatter:HTMLFormatter

执行 Behave

只需执行命令 behave,不用敲那么命令了,只有 BDD/Features/tag_example 文件夹下的 标签为 smoke 且为 cart 的测试用例执行了,并且生成了自定义的 html 测试报告。

PS C:\Automation\Test> behave
Feature: Shopping Cart and Order Process # BDD/Features/tag_example/shopping_cart.feature:3@cart @smokeScenario: Guest user adds items to the cart     # BDD/Features/tag_example/shopping_cart.feature:6Given the user is on the home page            # BDD/steps/shopping_cart_steps.py:27When the user adds an item to the cart        # BDD/steps/shopping_cart_steps.py:40Then the user should see the item in the cart # BDD/steps/shopping_cart_steps.py:61@cart @regressionScenario: Registered user removes items from the cart  # BDD/Features/tag_example/shopping_cart.feature:12Given the user is logged in                          # NoneAnd the user has items in the cart                   # NoneWhen the user removes an item from the cart          # NoneThen the user should see the updated cart            # None@order @smokeScenario: Guest user places an order                 # BDD/Features/tag_example/shopping_cart.feature:19Given the user is on the home page                 # NoneWhen the user adds an item to the cart             # NoneAnd the user proceeds to checkout                  # NoneAnd the user completes the order                   # NoneThen the user should receive an order confirmation # None@order @regressionScenario: Registered user tracks an order           # BDD/Features/tag_example/shopping_cart.feature:27Given the user is logged in                       # NoneAnd the user has placed an order                  # NoneWhen the user checks the order status             # NoneThen the user should see the current order status # None1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 3 skipped
3 steps passed, 0 failed, 13 skipped, 0 undefined
Took 0m0.000s

生成了 html 测试报告:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2660364.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

【王爽老师汇编语言】os和计组必备前置知识-学习记录2

1. 应用程序的组成 从汇编语言角度,一个程序分为: 数据段 堆栈段 代码段 扩展段 应用程序从高级语言的角度比如C语言分段: 数据段 代码段 BSS段 栈、堆 我们可以看到一个可执行程序至少包含:代码段数据段BBS段 一般情况下&…

VirtualBox 网络连接配置

这几天为了确认笔记本电脑的ssd磁盘型号,拆开电脑查看了一下,并且拔出来又装回去了,就是这个插拔的动作,导致原本能好好运行的虚拟机,突然启动报错启动不起来了。看了启动日志,显示启动的时候磁盘数据校验出…

Java多线程技术五——单例模式与多线程

1 概述 本章的知识点非常重要。在单例模式与多线程技术相结合的过程中,我们能发现很多以前从未考虑过的问题。这些不良的程序设计如果应用在商业项目中将会带来非常大的麻烦。本章的案例也充分说明,线程与某些技术相结合中,我们要考虑的事情会…

Spring 是如何解决循环依赖问题的方案

文章目录 Spring 是如何解决循环依赖问题的? Spring 是如何解决循环依赖问题的? 我们都知道,如果在代码中,将两个或多个 Bean 互相之间持有对方的引用就会发生循环依赖。循环的依赖将会导致注入死循环。这是 Spring 发生循环依赖…

Linux安装GitLab教程

Linux安装GitLab教程 1、配置yum源 相当于新建一个文件,通过这个文件来安装gitlab vim /etc/yum.repos.d/gitlab-ce.repo 把这些配置粘进去 [gitlab-ce] nameGitlab CE Repository baseurlhttps://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/ gp…

NVIDIA GeForce Experience下载更新失败怎么办?

下载的文件会在C:\ProgramData\NVIDIA Corporation\Downloader这个目录下, 打开名字很长的文件夹,在被删除前手动安装即可。

12.27_黑马数据结构与算法笔记Java(补1)

目录 266 活动选择问题 分析 267 活动选择问题 贪心 268 分数背包问题 贪心 269 0-1 背包问题 贪心 270 斐波那契 动态规划 271 斐波那契 动态规划 降维 272 Bellman Ford 动态规划 分析 273 Bellman Ford 动态规划 实现1 274 Bellman Ford 动态规划 实现2 275 Leetco…

微信小程序开发系列-06事件

什么是事件 事件是视图层到逻辑层的通讯方式。事件可以将用户的行为反馈到逻辑层进行处理。事件可以绑定在组件上,当达到触发条件时,就会执行逻辑层中对应的事件处理函数。事件对象可以携带额外信息,如 id, dataset, touches。 事件分类 事…

使用cmake配置matplotlibcpp生成VS项目

https://gitee.com/feboreigns/matplotlibcpp 这篇文章需要一些cmake基础,python基础,visualstudio基础 准备环境 注意如果在VS平台使用必须要手动下载python,不能使用conda里面的,比如3.8版本,因为conda里面没有py…

Understanding Deep Image Representations by Inverting Them(2014)

文章目录 AbstractIntroductionContribution -Summary hh Abstract 从SIFT和视觉词袋到卷积神经网络(cnn),图像表示几乎是任何图像理解系统的关键组成部分。然而,我们对它们的了解仍然有限。在本文中,我们通过提出以下问题对表征中包含的视觉…

Mybatis行为配置之Ⅱ—结果相关配置项说明

专栏精选 引入Mybatis Mybatis的快速入门 Mybatis的增删改查扩展功能说明 mapper映射的参数和结果 Mybatis复杂类型的结果映射 Mybatis基于注解的结果映射 Mybatis枚举类型处理和类型处理器 再谈动态SQL 文章目录 专栏精选引言摘要正文autoMappingBehaviorautoMappingU…

关于设计模式、Java基础面试题

前言 之前为了准备面试,收集整理了一些面试题。 本篇文章更新时间2023年12月27日。 最新的内容可以看我的原文:https://www.yuque.com/wfzx/ninzck/cbf0cxkrr6s1kniv 设计模式 单例共有几种写法? 细分起来就有9种:懒汉&#x…

【中小型企业网络实战案例 四】配置OSPF动态路由协议

【中小型企业网络实战案例 三】配置DHCP动态分配地址-CSDN博客 【中小型企业网络实战案例 二】配置网络互连互通-CSDN博客 【中小型企业网络实战案例 一】规划、需求和基本配置_大小企业网络配置实例-CSDN博客 配置OSPF 由于内网互联使用的是静态路由,在链路出…

第八章 JPA和缓存

1.JPA 1.1.创建User实体类 public class User {private Integer uId;private String uName;private Integer uGender;private Integer uAge;private String uLoginname;private String uPassword;private Date uBirth;private String uEmail;private String uAddress; } 1.2…

走进电子技术之光敏电阻、电位器、开关

同学们大家好,今天我们继续学习杨欣的《电子设计从零开始》,这本书从基本原理出发,知识点遍及无线电通讯、仪器设计、三极管电路、集成电路、传感器、数字电路基础、单片机及应用实例,可以说是全面系统地介绍了电子设计所需的知识…

Redis 是如何执行的?

文章目录 命令执行流程步骤一:用户输入一条命令步骤二:客户端先将命令转换成 Redis 协议,然后再通过 socket 连接发送给服务器端步骤三:服务器端接收到命令步骤四:执行前准备步骤五:执行最终命令&#xff0…

牛客网SQL训练5—SQL大厂真题面试

文章目录 一、某音短视频1.各个视频的平均完播率2.平均播放进度大于60%的视频类别3.每类视频近一个月的转发量/率4.每个创作者每月的涨粉率及截止当前的总粉丝量5.国庆期间每类视频点赞量和转发量6.近一个月发布的视频中热度最高的top3视频 二、用户增长场景(某度信…

idea Spring Boot项目使用JPA创建与数据库链接

1.pom.xml文件中添加依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>com.mysql</groupId><artifactId>…

文章智能改写伪原创方法技巧

哈喽&#xff0c;小伙伴们&#xff0c;大家好&#xff0c;我 又回来了。 今天给大家分享一个文章智能改写原创的方法&#xff0c;现在的自媒体平台&#xff0c;做视频流量挺不好做的&#xff0c;除非你有好的方法&#xff0c;文章呢&#xff0c;就相对比较容易点了&#xff0c;…

Graph Transformer2023最新研究成果汇总,附15篇必看论文

图Transformer是一种结合了Transformer模型和图神经网络&#xff08;GNN&#xff09;的框架&#xff0c;用于在图形结构数据上执行预测任务。在图Transformer中&#xff0c;Transformer的自注意力机制被用来学习节点之间的关系&#xff0c;而GNN则被用来生成节点的嵌入表示。通…