如何在 C# 对象中获取 specflow 场景

How to get the specflow scenarios in c# objects

我想读取每个功能文件并为每个场景创建对象。阅读.feature文件后。我应该得到如下对象:

输入

@mytag  
Scenario: Add two numbers  
    Given I have entered 50 into the calculator  
    And I have entered 70 into the calculator  
    When I press add  
    Then the result should be 120 on the screen

预期输出

Scenarios - 给出功能文件中的所有场景。
Scenario.Steps - 给出该场景的所有时间。
Scenario.Examples - 给出所有示例。
Scenarios.Tags - 所有标签

代码

var lines = File.ReadAllText(@"P:\Test.feature");
var scenarios = lines.Split(new string[] { "Scenario: "}, StringSplitOptions.RemoveEmptyEntries);
var scenarioList = new List<Scenario>();
for (int i = 1; i < scenarios.Length; i++)
{
    var ind = scenarios[i].IndexOf("\n");

    var scenario = new Scenario();
    scenario.Name = scenarios[i].Substring(0, ind);
    var toInd=scenarios[i].IndexOf("@");
    if(toInd>1)
        scenario.Steps = scenarios[i].Substring(ind,toInd);
    else
        scenario.Steps = scenarios[i].Substring(ind);
    scenarioList.Add(scenario);
}

Gherkin Parser 应该可以满足您的期望。您可以在 Nuget gallery

获取