2014-10-19 41 views
2

在AfterScenario的方法,我想从表“示例”中的行方案大纲中,它获得的值,然后搜索数据库如何获取场景大纲示例表?

我知道,这可以通过使用Context.Scenario.Current达到特定值...

Context.Scenario.Current[key]=value; 

...但由于某些原因,我希望能够得到它以更简单的方式

这样的:

ScenarioContext.Current.Examples(); 

-----------情景--------------------------------

Scenario Outline: Create a Matter 

Given I create matter "<matterName>" 

Examples: 
| matterName | 
| TAXABLE | 

----------后台-----------------------------------

[AfterScenario()] 
    public void After() 
    { 
     string table = ScenarioContext.Current.Examples(); 
    } 
+2

什么是你的问题? – 2014-10-20 07:56:41

回答

0

所以,如果你看一下代码ScenarioContext你可以看到它继承SpecflowContext这本身就是一个Dictionary<string, object>。这意味着您可以简单地使用Values来获取值的集合,但我不知道它们是否为Examples

+0

ScenarioContext单例实例看起来是一个零长度集合。 – 2016-02-08 15:50:56

0

我想出的最佳解决方案是通过保留我自己的静态单例对象来推断示例,然后计算相同场景运行的次数。

MyContext.Current.Counts[ScenarioContext.Current.ScenarioInfo.Title]++; 

当然,这不,如果你不同时运行所有测试或以随机顺序运行它们工作得很好。将示例本身放在桌子上会更理想,但如果将我的技术与使用ScenarioStepContext结合使用,则可以从渲染的步骤定义文本本身中提取示例表的参数。

功能

Scenario Outline: The system shall do something! 
    Given some input <input> 
    When something happens 
    Then something should have happened 
Examples: 
| input | 
| 1  | 
| 2  | 
| 3  | 

SpecFlow挂钩

[BeforeStep] 
public void BeforeStep() 
{ 
    var text = ScenarioStepContext.Current.StepInfo.Text; 
    var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType; 

    if (text.StartsWith("some input ") && stepType == StepDefinitionType.Given) 
    { 
     var input = text.Split(' ').Last(); 
    } 
} 
相关问题