Python 行为:如何在非玩具项目中组织此框架的文件
我正在尝试使用行为框架python通过BDD测试项目。问题是所有BDD材料都使用了非真实的玩具示例。 我的项目相当大,我遇到了以下问题Python 行为:如何在非玩具项目中组织此框架的文件,python,bdd,python-behave,Python,Bdd,Python Behave,我正在尝试使用行为框架python通过BDD测试项目。问题是所有BDD材料都使用了非真实的玩具示例。 我的项目相当大,我遇到了以下问题 在不同的.feature文件中,我有相同的步骤名称,但它们的实现必须不同。 示例:步骤名称“输入代码并单击提交按钮”可能在网站的许多不同页面上使用。 如何解决这种碰撞 如果您有正在测试的复杂网页,步骤实现文件将快速增长。在几个.feature文件之后,step文件有超过400个代码字符串。根据.feature文件(1对1)划分步骤文件不是一个解决方案,因为某些步
因此,您有两个问题,我将尝试解决这两个问题: 问题1: 当/
出现在许多功能文件中时,可以一字不差地给出相同的;如何在步骤定义中实现它们才是重要的。我认为有两条路线:(1)利用行为;(2) 使用步骤的上下文
对象指定正在运行的功能文件或场景。关于(2),考虑像SO这样设置的场景:
Feature: Buying toys
@checkout_page
Scenario: Proceed to checkout page
Given Toys are in the shopping cart
When I enter code and click submit button <--- Same step
Then the items will be checked out
@purchase_page
Scenario: Proceed to purchase page
Given Toys are in the shopping cart
When I enter code and click submit button <--- Same step
Then the items will be purchased
逐步实施:
@When("I enter code and click submit button")
def step_impl(context):
# Assuming that the logic to enter the code and set up the clicking action
# is identical in most regards, you should pull it out of the following for loop
# and place it here. This may help shorten your step implementations.
# vvvvvvvvvvvvvvvvvvvvv
# HERE
# ^^^^^^^^^^^^^^^^^^^^^
for tag in context.current_scenario_tags:
if tag == "checkout_page":
# Then handle the checkout_page scenario
break
elif tag == "purchase_page":
# Then handle the purchase_page scenario
break
*根据您已经使用的标记,您可能只想执行break
语句,而不是循环遍历其余的标记,前提是您已经知道要查找的特定标记
这无助于解决step实现长达400行的问题,但正如俗话所说,“你不能既有蛋糕又吃蛋糕”
问题2:
上面提到了这一点,但这里有一些关于behave以及它如何从文档中搜索功能文件和step文件的内容:
behave可处理三种类型的文件:
业务分析师/赞助商/任何人编写的功能文件,其中包含您的行为场景,以及
包含场景的Python步骤实现的“步骤”目录
可选的一些环境控制(在步骤、场景、功能或整个过程之前和之后运行的代码
射击比赛)
因此需要feature/
目录,以及feature/steps/
子目录。答案是否定的,behave不允许您在功能/steps/
目录中创建分类文件夹或子目录。请记住,在查找功能文件中与步骤相关联的步骤实现时,behave会搜索feature/steps/
目录中的所有Python文件,但不会递归到该目录的子目录中。有一个好的命名惯例肯定会有很大的帮助
@When("I enter code and click submit button")
def step_impl(context):
# Assuming that the logic to enter the code and set up the clicking action
# is identical in most regards, you should pull it out of the following for loop
# and place it here. This may help shorten your step implementations.
# vvvvvvvvvvvvvvvvvvvvv
# HERE
# ^^^^^^^^^^^^^^^^^^^^^
for tag in context.current_scenario_tags:
if tag == "checkout_page":
# Then handle the checkout_page scenario
break
elif tag == "purchase_page":
# Then handle the purchase_page scenario
break