首页 > 解决方案 > SpecFlow equivalent to parameterized test fixture

问题描述

I’m using SpecFlow to write a set of tests, and I’d like to run each test multiple times, with different input data. I could do this with scenario outlines, but I want to run every scenario in the feature file with the same test cases.

I know I can use the Background to share the setup for one case, but I’m looking for something like a cross between Background and Scenario Outline, where I can supply a table of data to the Background and run the entire feature file once per row.

In NUnit, I’d use a parameterized test fixture to achieve this. Is there any equivalent in SpecFlow?

标签: unit-testingcucumberspecflowgherkin

解决方案


您可以利用 specflow辅助助手创建数据表对象并将其用于Background

Background:
    Given I’ve Entered The Following Information
    | FirstName| LastName|Email      |
    | Abcd1    | Xyz1    |abc1@xyz1.com|
    | Abcd2   | Xyz2     |abc2@xyz2.com|

class Person
{
string FirstName { get; set; }
string LastName { get; set; }
string email { get; set; }
}

用法:

    [Binding]
  [Given(@"I’ve Entered The Following Information")]
    public void UseData(TechTalk.SpecFlow.Table table)
    {
        var enumeratePersons = table.CreateSet<Person>();   
          foreach (Person P in enumeratePersons ){

             log.Info(P.FirstName + " " + P.LastName );
          }
     }

您可能必须使用属性或规范流上下文在绑定之间共享数据。运行时Background,它将为每个场景创建数据对象,但跨绑定使用它是用户的责任


推荐阅读