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=VALUE
                        Define user-specific data for the config.userdata dictionary. Example:   
                        -D foo=bar to store it in config.userdata["foo"].
  -e PATTERN, --exclude PATTERN
                        Don't run feature files matching regular expression PATTERN.
  -i PATTERN, --include PATTERN
                        Only 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 PATH
                        Directory in which to store JUnit reports.
  -f FORMAT, --format FORMAT
                        Specify 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 file
                        setting.
  -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 a
                        failure.) This is the default behaviour. This switch is used to
                        override 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 a
                        failure.) This is the default behaviour. This switch is used to
                        override 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 and
                        displayed in the event of a failure. This is the default behaviour.      
                        This switch is used to override a configuration file setting.
  --logging-level LOGGING_LEVEL
                        Specify a level to capture logging at. The default is INFO - capturing   
                        everything.
  --logging-format LOGGING_FORMAT
                        Specify 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_DATEFMT
                        Specify custom date/time format to print statements. Uses the same       
                        format as used by standard logging handlers.
  --logging-filter LOGGING_FILTER
                        Specify 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-handlers
                        Clear 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 FILE
                        Write 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_EXPRESSION
                        Only 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 to
                        override 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.feature

Feature: Shopping Cart and Order Process

  @cart @smoke
  Scenario: Guest user adds items to the cart
    Given the user is on the home page
    When the user adds an item to the cart
    Then the user should see the item in the cart

  @cart @regression
  Scenario: Registered user removes items from the cart
    Given the user is logged in
    And the user has items in the cart
    When the user removes an item from the cart
    Then the user should see the updated cart

  @order @smoke
  Scenario: Guest user places an order
    Given the user is on the home page
    When the user adds an item to the cart
    And the user proceeds to checkout
    And the user completes the order
    Then the user should receive an order confirmation

  @order @regression
  Scenario: Registered user tracks an order
    Given the user is logged in
    And the user has placed an order
    When the user checks the order status
    Then the user should see the current order status

step 文件

创建 calculator_steps.py 文件

# calculator_steps.py

from 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=smoke
    cart
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 @smoke
  Scenario: Guest user adds items to the cart     # BDD/Features/tag_example/shopping_cart.feature:6
    Given the user is on the home page            # BDD/steps/shopping_cart_steps.py:27
    When the user adds an item to the cart        # BDD/steps/shopping_cart_steps.py:40
    Then the user should see the item in the cart # BDD/steps/shopping_cart_steps.py:61

  @cart @regression
  Scenario: Registered user removes items from the cart  # BDD/Features/tag_example/shopping_cart.feature:12
    Given the user is logged in                          # None
    And the user has items in the cart                   # None
    When the user removes an item from the cart          # None
    Then the user should see the updated cart            # None

  @order @smoke
  Scenario: Guest user places an order                 # BDD/Features/tag_example/shopping_cart.feature:19
    Given the user is on the home page                 # None
    When the user adds an item to the cart             # None
    And the user proceeds to checkout                  # None
    And the user completes the order                   # None
    Then the user should receive an order confirmation # None

  @order @regression
  Scenario: Registered user tracks an order           # BDD/Features/tag_example/shopping_cart.feature:27
    Given the user is logged in                       # None
    And the user has placed an order                  # None
    When the user checks the order status             # None
    Then the user should see the current order status # None

1 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://www.mfbz.cn/a/275606.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

《深入理解C++11:C++11新特性解析与应用》笔记四

第四章 新手易学,老兵易用 4.1 右尖括号>的改进 在 C98 中,有一条需要程序员规避的规则:如果在实例化模板的时候出现了连续的两个右尖括号 >,那么它们之间需要一个空格来进行分隔,以避免发生编译时的错误。C98 会将>&g…

【量化】蜘蛛网策略复现

文章目录 蜘蛛网策略研报概述持仓数据整理三大商品交易所的数据统一筛选共有会员清洗数据计算研报要求数据全部代码 策略结果分析无参数策略有参数策略正做反做 MSD技术指标化 蜘蛛网策略 策略来自《东方证券-股指期货趋势交易之蜘蛛网策略——从成交持仓表中捕捉知情投资者行为…

腾讯云轻量应用服务器详细介绍

腾讯云轻量应用服务器开箱即用、运维简单的轻量级云服务器,CPU内存带宽配置高并且价格特别优惠,轻量2核2G3M带宽62元一年、2核2G4M优惠价118元一年,540元三年、2核4G5M带宽218元一年,756元3年、4核8G12M带宽646元15个月等&#xf…

Linux操作系统:LVM与磁盘配额

目录 一、LVM逻辑卷管理基础 1、LVM逻辑卷管理的概念 2、LVM逻辑卷管理的核心组件 3、LVM逻辑卷管理的优势 4、LVM逻辑卷管理的命令 4.1 扫描命令的用法 4.1.1 pvscan 4.1.2 vgscan 4.1.3 lvscan 4.2 查看命令的用法 4.2.1 pvdisplay 4.2.2 vgdisplay 4.2.3 lvdis…

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

目录 279 0-1 背包问题 动态规划 实现 280 0-1 背包问题 动态规划 降维 281 完全背包问题 动态规划 分析 282 完全背包问题 动态规划 实现 283 零钱兑换问题 动态规划 分析 284 零钱兑换问题 动态规划 实现 285 零钱兑换问题 动态规划 降维 286 零钱兑换II 动态规划 分析…

【idea】运行工程时候卡了许久Java Method Breakpoints

老以为是数据库连接不上,此问题概率性小,操作上面不小心打了断点… 应该是打断点的时候,打到了方法上面,去掉哟 Java Method Breakpoints

C++day2作业

把课上strcut的练习&#xff0c;尝试着改成class #include <iostream>using namespace std; class Stu { private:int age;string sex;int hign; public:int soce;void get_information();void set_information(); }; void Stu::set_information() {static Stu s1;cout …

C++ DAY2作业

1.课堂struct练习&#xff0c;用class&#xff1b; #include <iostream>using namespace std;class Stu { private:int age;char sex;int high; public:double score;void set_values(int a,char b,int c,double d);int get_age();char get_sex();int get_high(); }; vo…

「从红月开始」运输公司遭精神污染,小十七救哥哥,事发地陷危机

Hello,小伙伴们&#xff0c;我是拾荒君。 国漫《从红月开始》第3集超前爆料&#xff0c;本次透露陆辛首次被派遣去调查精神污染的任务。他的目的地是四方运输公司&#xff0c;一个主要由亡命之徒组成的组织。他们表面上从事运输业务&#xff0c;但背地里进行偷渡和走私活动。 …

【CSAPP】探究BombLab奥秘:Phase_2的解密与实战

&#x1f4cb; 前言 ​&#x1f308;个人主页&#xff1a;Sarapines Programmer&#x1f525; 系列专栏&#xff1a;《斯坦福大学之CSAPP》⏰诗赋清音&#xff1a;桃花灼灼春风暖&#xff0c;心随乐曲扬徐徐。 苦尽甘来梦未阑&#xff0c;岁月长河任舟游。 ​ &#x1f389;欢迎…

cnPuTTY 0.80.0.1—PuTTY Release 0.80中文版本简单说明~~

2023-12-18 官方发布了PuTTY 0.80本次发布主要是针对Terrapin攻击(CVE-2023-48795)的修改发布。 更多详细的内容请查看PuTTY Change Log。 有关Terrapin攻击可用简单参考&#xff1a;警告&#xff01;&#xff01;&#xff01;Terrapin攻击(CVE-2023-48795)~~~ 为了缓解此漏洞…

【线性代数】通过矩阵乘法得到的线性方程组和原来的线性方程组同解吗?

一、通过矩阵乘法得到的线性方程组和原来的线性方程组同解吗&#xff1f; 如果你进行的矩阵乘法涉及一个线性方程组 Ax b&#xff0c;并且你乘以一个可逆矩阵 M&#xff0c;且产生新的方程组 M(Ax) Mb&#xff0c;那么这两个系统是等价的&#xff1b;它们具有相同的解集。这…

【unity3D-粒子系统】粒子系统主模块-Particle System篇

&#x1f497; 未来的游戏开发程序媛&#xff0c;现在的努力学习菜鸡 &#x1f4a6;本专栏是我关于游戏开发的学习笔记 &#x1f236;本篇是unity的粒子系统主模块-Particle System 基础知识 Particle System 介绍&#xff1a;粒子系统的主模块&#xff0c;是必需的模块&#x…

在线客服系统推荐:提升客户满意度与工作效率的利器

客服系统分为售前和售后&#xff0c;售前客户系统是为了能够及时解决客户在购买产品前的问题&#xff0c;通过客服人员让客户了解产品的功能点是能够满足他们的需求点&#xff0c;从未达到转化的目的。 而售后客户系统主要是提供给购买后的客户强大的产品售后支持&#xff0c;…

PiflowX组件-DataGen

DataGen组件 组件说明 按数据生成规则模拟数据。在开发和演示场景下使用比较方便。具体可以查看Flink官方DataGen connector。 计算引擎 flink 有界性 字段的数据全部生成完成后&#xff0c;source 就结束了。 因此&#xff0c;有界性取决于字段的有界性。 组件分组 co…

SQL Server 索引和视图

CSDN 成就一亿技术人&#xff01; 难度指数&#xff1a;* * * CSDN 成就一亿技术人&#xff01; 目录 1.索引 什么是索引&#xff1f; 索引的作用&#xff1f; 索引的分类 1. 唯一索引 2. 主键索引 3. 聚集索引 4.非聚集索引 5.复合索引 6.全文搜索 索引的创建&am…

Springboot拦截器及统一异常处理

文章目录 一、Java中异常相关概念1、异常类2、异常处理方法3、注意事项4、自定义异常 二、配置全局异常处理1、统一返回体定义2、定义异常处理实现类3、全局异常处理类 三、Springboot拦截器1、定义拦截器2、注册拦截器 四、验证效果 一、Java中异常相关概念 1、异常类 Throw…

C语言—每日选择题—Day64

前言 两天没更新了&#xff0c;作者在复习期末考试&#xff0c;更新一波&#xff0c;祝大家都能顺利通过期末考试&#xff01;&#xff01;&#xff01; 指针相关博客 打响指针的第一枪&#xff1a;指针家族-CSDN博客 深入理解&#xff1a;指针变量的解引用 与 加法运算-CSDN博…

javaWebssh民宿管理系统myeclipse开发mysql数据库MVC模式java编程计算机网页设计

一、源码特点 java ssh民宿管理系统是一套完善的web设计系统&#xff08;系统采用ssh框架进行设计开发&#xff09;&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模 式开发。开发环境为TOMCAT7.0,My…

【高可用】使用Keepalived实现SFTP服务的高可用

使用Keepalived实现SFTP服务的高可用 背景 这个事情的背景是生产环境的数据采集流程时不时会出问题&#xff08;这个也是不可避免的&#xff09;&#xff0c;目前的处理手段是&#xff1a;所有的数据接口服务器&#xff08;也就是存放原始数据等待采集的服务器&#xff09;都…