And*_*kov 3 python bdd python-behave
我正在尝试使用行为框架、python 通过 BDD 测试来覆盖项目。问题是所有 BDD 材料都使用不真实的玩具示例。我的项目相当大,我遇到了以下问题
所以你有两个问题,我将尝试解决这两个问题:
\n\n在许多功能文件中逐字逐句地使用相同的Given
/ When
/ \是很好的;Then
重要的是如何在步骤定义中实现它们。我会考虑采用两条路线:(1)利用行为的步骤参数;(2) 使用步骤的context
对象来指定您正在运行的功能文件或场景。关于 (2),考虑设置如下场景:
Feature: Buying toys \n\n @checkout_page\n Scenario: Proceed to checkout page\n Given Toys are in the shopping cart\n When I enter code and click submit button <--- Same step\n Then the items will be checked out\n\n @purchase_page\n Scenario: Proceed to purchase page\n Given Toys are in the shopping cart\n When I enter code and click submit button <--- Same step\n Then the items will be purchased \n
Run Code Online (Sandbox Code Playgroud)\n\n从这里,您可以使用Behavior的context
对象来存储标记并在步骤实现中使用它:
在环境.py中:
\n\ndef before_scenario(context, scenario):\n context.current_scenario_tags = scenario.tags # This is a list\n # ... other stuff maybe\n
Run Code Online (Sandbox Code Playgroud)\n\n步骤实施:
\n\n@When("I enter code and click submit button")\ndef step_impl(context):\n # Assuming that the logic to enter the code and set up the clicking action\n # is identical in most regards, you should pull it out of the following for loop\n # and place it here. This may help shorten your step implementations.\n # vvvvvvvvvvvvvvvvvvvvv\n # HERE\n # ^^^^^^^^^^^^^^^^^^^^^\n for tag in context.current_scenario_tags:\n if tag == "checkout_page":\n # Then handle the checkout_page scenario\n break\n elif tag == "purchase_page":\n # Then handle the purchase_page scenario\n break\n
Run Code Online (Sandbox Code Playgroud)\n\n*根据您已经使用的标签,break
假设您已经知道要查找的特定标签,您可能只想执行语句而不是循环遍历其余标签。
这对解决 400 行长的步骤实现的问题没有帮助,但俗话说,“鱼与熊掌不可兼得”。
\n\n上面已经解决了这个问题,但这里有一些关于行为以及它如何从文档中搜索功能文件和步骤文件的事情:
\n\n\n\n\n行为适用于三种类型的文件:
\n\n\n
\n- 由您的业务分析师/发起人/任何人编写的包含您的行为场景的功能文件,以及
\n- \xe2\x80\x9csteps\xe2\x80\x9d 目录,其中包含场景的 Python 步骤实现。
\n- 可选的一些环境控制(在步骤、场景、功能或整个射击比赛之前和之后运行的代码)。
\n
所以feature/
需要一个目录,一个feature/steps/
子目录也是如此。答案是否定的,行为不允许您在feature/steps/
目录中创建分类文件夹或子目录。请记住,在查找与功能文件中的步骤关联的步骤实现时,behaviour 会搜索目录中的所有 Python 文件feature/steps/
,但不会递归到目录的子目录中。拥有一个良好的命名约定肯定会大有帮助!