BDD - Python Behave 配置文件 behave.ini

news2024/11/26 22:33:55

BDD - Python Behave 配置文件 behave.ini

  • 引言
  • behave.ini
    • 配置参数的类型
    • 配置项
  • behave.ini 应用
    • feature 文件
    • step 文件
    • 创建 behave.ini
    • 执行 Behave
    • 查看配置默认值 behave -v

引言

前面文章 《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 测试报告:

在这里插入图片描述

查看配置默认值 behave -v

如果您想知道 behave 从哪里获得其配置默认值,您可以使用 “-v” 命令行参数,它会告诉您。

PS C:\Automation\Test> behave -v
Loading config defaults from "./behave.ini"
Using defaults:
          color False
  show_snippets True
   show_skipped True
        dry_run False
    show_source True
   show_timings True
 stdout_capture True
 stderr_capture True
    log_capture True
 logging_format %(levelname)s:%(name)s:%(message)s
  logging_level 20
  steps_catalog False
        summary True
          junit False
          stage None
       userdata {}
 default_format pretty
   default_tags
scenario_outline_annotation_schema {name} -- @{row.id} {examples.name}
         format ['my_html']
       outfiles ['my_report.html']
          paths ['BDD\\Features\\tag_example']
           tags ['smoke', 'cart']
more_formatters {'my_html': 'behave_html_formatter:HTMLFormatter'}
Supplied path: "BDD\Features\tag_example"
Trying base directory: C:\Automation\Test\BDD\Features\tag_example
Trying base directory: C:\Automation\Test\BDD\Features
Trying base directory: C:\Automation\Test\BDD
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

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

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

相关文章

node 项目中 __dirname / __filename 是什么,为什么有时候不能用?

__dirname 是 Node.js 中的一个特殊变量,表示当前执行脚本所在的目录的绝对路径。 __filename 同理,是 Node.js 中的一个特殊变量,表示当前执行脚本的绝对路径,包括文件名。 在 Node.js 中,__dirname / __filename是…

C语言 linux文件操作(二)

文章目录 一、获取文件长度二、追加写入三、覆盖写入四、文件创建函数creat 一、获取文件长度 通过lseek函数,除了操作定位文件指针,还可以获取到文件大小,注意这里是文件大小,单位是字节。例如在file1文件中事先写入"你好世…

Vue - v-for 指令详解

1. 渲染列表 首先&#xff0c;让我们看看如何使用 v-for 渲染一个简单的列表。在 Vue.js 中&#xff0c;我们可以使用 v-for 来遍历数组&#xff0c;并根据数组中的每个元素渲染相应的内容。 <template><div><!-- 使用 v-for 渲染列表 --><ul><li…

计算机与人工智能:共创智能时代的新篇章

计算机与人工智能&#xff1a;共创智能时代的新篇章 在这个科技日新月异的时代&#xff0c;计算机与人工智能&#xff08;AI&#xff09;的结合正以前所未有的速度改变着世界。它们在各自的领域内飞速发展&#xff0c;而当这两者相遇时&#xff0c;它们产生了巨大的能量&#x…

ClickHouse基础知识(六):ClickHouse的副本配置

副本的目的主要是保障数据的高可用性&#xff0c;即使一台 ClickHouse 节点宕机&#xff0c;那么也可以 从其他服务器获得相同的数据。 1. 副本写入流程 2. 配置步骤 ➢ 启动 zookeeper 集群 ➢ 在hadoop101的/etc/clickhouse-server/config.d目录下创建一个名为metrika.xml…

can通信的验收码和屏蔽码怎么计算

方法一&#xff1a;直接用canTest软件并按下图计算&#xff08;借助工具&#xff0c;比较方便&#xff09; 方法二&#xff1a;明白规则

探索Apache Commons Imaging处理图像

第1章&#xff1a;引言 大家好&#xff0c;我是小黑&#xff0c;咱们今天来聊聊图像处理。在这个数字化日益增长的时代&#xff0c;图像处理已经成为了一个不可或缺的技能。不论是社交媒体上的照片编辑&#xff0c;还是专业领域的图像分析&#xff0c;图像处理无处不在。而作为…

RAD Studio 12 安装激活说明及常见问题

目录 RAD Studio 安装说明 RAD Studio 最新的修补程序更新 RAD Studio 产品相关信息 Embarcadero 产品在线注册步骤 单机版授权产品注册注意事项 Embarcadero 产品离线注册步骤 Embarcadero 产品安装次数查询 Embarcadero 序号注册次数限制 EDN账号 - 查询授权序号、下…

idea 插件开发之 HelloWorld

前言 本文使用的 idea 2023.3 版本进行插件入门开发&#xff0c;首先要说明的是 idea 2023 版本及以后的 idea&#xff0c;对插件开发进行了一定程度的变动&#xff1a; 1、创建项目时不再支持 maven 选项 2、必须是 jdk17 及以后版本&#xff08;点击查看官网版本对应关系&…

react 之 美团案例

1.案例展示 2.环境搭建 克隆项目到本地&#xff08;内置了基础静态组件和模版&#xff09; git clone http://git.itcast.cn/heimaqianduan/redux-meituan.git 安装所有依赖 npm i 启动mock服务&#xff08;内置了json-server&#xff09; npm run serve 启动前端服务 npm…

苹果手机打开Microsoft Outlook日历ics文件方法

作为一名经常需要处理各种日程安排的苹果用户&#xff0c;我深知ics文件的重要性。ics文件通常来自于我们日常使用的日历应用&#xff0c;比如Microsoft Outlook&#xff0c;是日程信息的标准格式。但很多时候&#xff0c;当我们尝试打开这些ics文件时&#xff0c;却会遇到种种…

re:Invent 2023技术上新|Amazon DynamoDB与OpenSearch Service的Zero-ETL集成

Amazon DynamoDB 与 Amazon OpenSearch Service 的 Zero-ETL 集成已正式上线&#xff0c;该服务允许您通过自动复制和转换您的 DynamoDB 数据来搜索数据&#xff0c;而无需自定义代码或基础设施。这种 Zero-ETL 集成减少了运营负担和成本&#xff0c;使您能够专注于应用程序。这…

mysql查询出json格式字段中的值

一、使用场景 由于一些特殊数据使用json格式保存到表数据种中了&#xff0c;在查询的时候需要查询出这条数据中json格式中的某个字段 比如&#xff1a;需要将下列字符串中的“nationality”字段单独查询出来 json格式是一个对象 结果&#xff1a; json格式是一个集合 查询结…

2023量子科技十大进展 | 光子盒年度系列

量子力学是20世纪初成熟的理论&#xff0c;一个多世纪以来一直令科学家们感到惊讶、好奇和困惑。尽管该理论具有反直觉的性质&#xff0c;但它却以优异的成绩通过了实验测试&#xff0c;不断揭示出一个与我们的日常经验相去甚远的世界。 时至今日&#xff0c;科学家们仍然忙于操…

CRM诞生到现在历经了哪些发展阶段?CRM系统的五个关键节点

CRM管理系统从被发明到现在&#xff0c;历经多次迭代已经成为一个相对成熟的系统。企业可以靠它管理客户信息&#xff0c;提升盈利能力。今天就来介绍一下CRM的发展历程。 一、CRM系统的雏形 广义上的CRM系统其实可以追溯到古希腊时期。当时的商人靠书写记录自己与客户和合作…

FileZilla的使用主动模式与被动模式

&#x1f3ac; 艳艳耶✌️&#xff1a;个人主页 &#x1f525; 个人专栏 &#xff1a;《产品经理如何画泳道图&流程图》 ⛺️ 越努力 &#xff0c;越幸运 目录 一、FileZilla简介 1、FileZilla是什么&#xff1f; 2、FileZilla的应用场景 二、FileZilla的安装 1、下…

参数归一化-实现时间格式化

文章目录 需求分析具体实现完整源码 不知道大家有没有尝试封装过一个时间格式化的函数啊&#xff0c;在之前我封装的时候&#xff0c;开始是觉得手到擒来&#xff0c;但是实践之后发现写非常的shi啊&#xff0c;大量的分支判断&#xff0c;哪怕是映射起到的作用也只是稍微好一点…

【小程序】如何获取特定页面的小程序码

一、进入到小程序管理后台&#xff0c;进入后点击上方的“工具”》“生成小程序码” 小程序管理后台 二、进入开发者工具&#xff0c;打开对应的小程序项目&#xff0c;复制底部小程序特定页面的路径 三、粘贴到对应位置的文本框&#xff0c;点击确定即可

NCNN环境部署及yolov5pt转ncnn模型转换推理

该内容还未完整&#xff0c;笔记内容&#xff0c;持续补充。 〇开发环境版本 vs2022 cmake3.21.1 ncnn20231027发行版 yolov5s v6.2 vunlkan1.2.198.1 Protobuf3.20.0 Opencv3.4.1 一、模型转换 yolov5s v6.2训练的pt模型&#xff0c;直接导出tourchscript&#xff0c…

鸿蒙APP的代码规范

鸿蒙APP的代码规范是为了确保代码质量、可读性和可维护性而定义的一系列规则和标准。以下是一些建议的鸿蒙APP代码规范&#xff0c;希望对大家有所帮助。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎交流合作。 1. 代码风格&#xff1a; 采用…