昆山市住房和建设局网站,东昌网站建设公司,开发公司与城市资产经营公司合作协议,网站设计哪家公司好BDD - Python Behave 用户自定义命令行选项 -D 引言behave -Dbehave -D 应用feature 文件behave.ini 配置文件step 文件执行 引言
日常运行测试用例#xff0c;有时需要自定义命令行参数#xff0c;比如不同环境的对应的配置是不一样的#xff0c;这样就需要传一个环境参数… BDD - Python Behave 用户自定义命令行选项 -D 引言behave -Dbehave -D 应用feature 文件behave.ini 配置文件step 文件执行 引言
日常运行测试用例有时需要自定义命令行参数比如不同环境的对应的配置是不一样的这样就需要传一个环境参数来执行根据执行的环境选择对应的配置文件。今天就来了解一下用户自定义命令行参数选项。
想了解更多 Behave 相关的文章欢迎阅读《Python BDD Behave 系列》持续更新中。
behave -D
执行命令来查看命令行选项
behave -h其中 behave -D 选项是 Behave 命令行工具的一个功能用于从命令行传递用户定义的配置参数。通过 -D 或 --define 选项您可以设置键值对这些键值对将存储在 context.config.userdata 中以供测试脚本中使用可以通过 context.config.userdata 字典来访问这些变量 -D NAMEVALUE, --define NAMEVALUE Define user-specific data for the config.userdata dictionary. Example: -D foobar to store it in config.userdata[“foo”]. 使用方式如下
behave -D key1value1 -D key2value2 ...其中key1value1、key2value2 是您想要传递给 Behave 测试的配置参数。 例如如果您有一个测试需要使用用户名和密码您可以这样传递
behave -D usernamemyuser -D passwordmypassword然后在测试脚本中您可以通过 context.config.userdata 访问这些参数
# 在 Behave 测试脚本中访问配置参数
username context.config.userdata.get(username, )
password context.config.userdata.get(password, )还有一种方式通过配置文件定义 如
[behave.userdata]
env devstep 脚本里可以通过 context.config.userdata[‘env’] 访问
behave -D 应用
下面举个简单粗暴的例子
feature 文件
Feature: Context User Data Exampleuser_data
Scenario: User data scenarioGiven user data is setThen get the user databehave.ini 配置文件
定义了用户自定义参数env dev
# behave.ini
[behave]
pathsBDD/Features/user_data
dry_run false
format my_html
stdout_capture false
outfiles my_report.html[behave.formatters]
my_html behave_html_formatter:HTMLFormatter[behave.userdata]
env devstep 文件
通过 context.config.userdata[‘param_name’] 来访问用户自定义的参数
from behave import *given(user data is set)
def step_user_data_is_set(context):passthen(get the user data)
def then_get_user_data(context):print(fenv:{context.config.userdata[env]})print(fuser:{context.config.userdata[user]})print(fpw:{context.config.userdata[pw]})执行
执行命令 behave -D userzhang -D pw2024 -D userzhang -D pw2024 定义了两个自定义的变量参数
step 实现可以访问到这些参数
PS C:\Automation\Test behave -D userzhang -D pw2024
Feature: Context User Data Example # BDD/Features/user_data/user_data.feature:1user_dataScenario: User data scenario # BDD/Features/user_data/user_data.feature:4Given user data is set # BDD/steps/user_data_steps.py:3Then get the user data # BDD/steps/user_data_steps.py:7
env:dev
user:zhang
pw:20241 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
2 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s