行为:如何在非玩具项目中组织该框架的文件

And*_*kov 3 python bdd python-behave

我正在尝试使用行为框架、python 通过 BDD 测试来覆盖项目。问题是所有 BDD 材料都使用不真实的玩具示例。我的项目相当大,我遇到了以下问题

  1. 在不同的 .feature 文件中,我具有相同的步骤名称,但它们的实现必须不同。示例:步骤名称“输入代码并单击提交按钮”可以在网站上的许多不同页面上使用。如何解决这种碰撞?
  2. 如果您有复杂的网页进行测试,步骤实现文件会快速增长。在几个 .feature 文件之后,步骤文件就有超过 400 多个代码字符串。根据 .feature 文件划分步骤文件(1 对 1)不是一个解决方案,因为某些步骤必须在 .feature 文件之间共享,并且如何找到特定步骤变得不明显。是否有可能按级别划分步骤实现(功能级别、少数功能级别的目录、项目范围级别...)

nat*_*323 5

所以你有两个问题,我将尝试解决这两个问题:

\n\n

问题一:

\n\n

在许多功能文件中逐字逐句地使用相同的Given/ When/ \是很好的;Then重要的是如何在步骤定义中实现它们。我会考虑采用两条路线:(1)利用行为的步骤参数;(2) 使用步骤的context对象来指定您正在运行的功能文件或场景。关于 (2),考虑设置如下场景:

\n\n
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对象来存储标记并在步骤实现中使用它:

\n\n

在环境.py中:

\n\n
def 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假设您已经知道要查找的特定标签,您可能只想执行语句而不是循环遍历其余标签。

\n\n

这对解决 400 行长的步骤实现的问题没有帮助,但俗话说,“鱼与熊掌不可兼得”。

\n\n

问题2:

\n\n

上面已经解决了这个问题,但这里有一些关于行为以及它如何从文档中搜索功能文件和步骤文件的事情:

\n\n
\n

行为适用于三种类型的文件:

\n\n
    \n
  1. 由您的业务分析师/发起人/任何人编写的包含您的行为场景的功能文件,以及
  2. \n
  3. \xe2\x80\x9csteps\xe2\x80\x9d 目录,其中包含场景的 Python 步骤实现。
  4. \n
  5. 可选的一些环境控制(在步骤、场景、功能或整个射击比赛之前和之后运行的代码)。
  6. \n
\n
\n\n

所以feature/需要一个目录,一个feature/steps/子目录也是如此。答案是否定的,行为不允许您在feature/steps/目录中创建分类文件夹或子目录。请记住,在查找与功能文件中的步骤关联的步骤实现时,behaviour 会搜索目录中的所有 Python 文件feature/steps/,但不会递归到目录的子目录中。拥有一个良好的命名约定肯定会大有帮助!

\n