2015-12-07 64 views
4

正如标题所示,我希望在场景大纲之前运行某些特定的配置/环境设置步骤。我知道有Background可以为场景执行此操作,但Behave会将场景大纲分割为多个场景,从而为场景大纲中的每个输入运行背景。在场景大纲之前运行一次特定步骤 - Python Behaviour

这不是我想要的。由于某些原因,我无法提供我正在使用的代码,但是我会写一个示例功能文件。

Background: Power up module and connect 
Given the module is powered up 
And I have a valid USB connection 

Scenario Outline: Example 
    When I read the arduino 
    Then I get some <'output'> 

Example: Outputs 
| 'output' | 
| Hi  | 
| No  | 
| Yes  | 

什么在这种情况下会出现的情况是舞动将电源循环,并检查每个输出HiNo USB连接,Yes导致三个电源周期和三个连接检查

我想要的是循规蹈矩地关闭电源一次并检查一次连接,然后运行所有三项测试。

我该怎么做呢?

回答

0

我正面对完全相同的问题。 有一个昂贵的Background,这应该只执行一次Feature。 解决这个问题实际上需要什么,是在Scenario之间存储状态的能力。

我对这个问题的解决方案是使用behave.runner.Context#_root,它贯穿整个运行过程。我知道访问私人会员不是一种好的做法 - 我会很乐意学习更清晰的方式。

# XXX.feature file 
Background: Expensive setup 
    Given we have performed our expensive setup 

# steps/XXX.py file 
@given("we have performed our expensive setup") 
def step_impl(context: Context):  
    if not context._root.get('initialized', False): 
     # expensive_operaion.do() 
     context._root['initialized'] = True 
+0

我相信[TomDotDom's answer](https://stackoverflow.com/a/40923708/6252525)是正确的方法。你可以这样做,但这是'steps/enviroment.py'的意图,相比之下,这种方式感觉有点“黑客”。 – JGC

1

您最好的选择可能是使用before_feature环境钩并且a) 标签的功能和/或b)直接功能名称。

例如:

some.feature

@expensive_setup 
Feature: some name 
    description 
    further description 

    Background: some requirement of this test 
    Given some setup condition that runs before each scenario 
     And some other setup action 

    Scenario: some scenario 
     Given some condition 
     When some action is taken 
     Then some result is expected. 

    Scenario: some other scenario 
     Given some other condition 
     When some action is taken 
     Then some other result is expected. 

步骤/ enviroment.py

def before_feature(context, feature): 
    if 'expensive_setup' in feature.tags: 
     context.excute_steps(''' 
      Given some setup condition that only runs once per feature 
       And some other run once setup action 
     ''') 

替代步骤/ enviroment.py

def before_feature(context, feature): 
    if feature.name == 'some name': 
     context.excute_steps(''' 
      Given some setup condition that only runs once per feature 
       And some other run once setup action 
     ''') 
相关问题