【问题标题】:Is it possible to specify a specific "after feature" hook for each behave feature?是否可以为每个行为特征指定一个特定的“后特征”挂钩?
【发布时间】:2021-02-02 18:28:01
【问题描述】:

我知道,在behave 中,可以使用environment.py 指定before_feature()after_feature() 函数在每个 功能之前或之后执行设置和拆卸代码。

但是,有没有办法指定仅针对特定功能执行的自定义后功能功能?

在我的在线搜索中,我发现一些页面讨论了使用 cucumber-jvm 执行此操作的可能方法,但没有关于使用行为进行此操作的内容:

举个例子,我想做类似的事情:

Feature: My feature

AfterFeature:
  Then Database is reset

# Scenarios

我想到的一件事可能是context 可以在“全局”after_feature() 函数中查看,然后如果该功能的名称与所需名称匹配,则可以运行自定义代码。例如,类似:

# In environment.py

def after_all(context):
    if context.feature.name == 'feature_with_custom_teardown.feature':
        # Do custom teardown

    # Do regular teardown

但是,当我尝试在 after_all() 函数中检查 context 时,由于某种原因,该功能是 None - 但这可能只是意味着我做错了。

【问题讨论】:

    标签: python cucumber bdd python-behave


    【解决方案1】:

    有几种方法可以做到这一点:

    environment.py 中,您可以添加如下内容:

    def after_feature(context, feature):
        if feature.name == 'My feature':
            # Do custom teardown
    

    您还可以使用功能标签而不是名称,如果您知道需要设置/拆卸的功能不止一个,这会更实用。因此,如果您的功能文件被标记为类似

    @database-reset-required
    Feature: My feature
    
    # Scenarios here
    

    那么,对于拆解部分,在environment.py 你可以:

    def after_feature(context, feature):
        if 'database-reset-required' in feature.tags:
            # Do custom teardown
    

    如果您选择feature.name 方法而不是feature.tags 请注意,您需要使用功能文件中定义的*名称*,即这部分Feature: *My feature*,而不是文件名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-28
      • 2022-04-26
      • 2022-09-27
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多