0
votes

I was wondering what was the best way to run my scenario for many times with different input data using SpecFlow. I have a basic understanding of Scenario Outline&Examples/Tables but I can't figure out on how to meet my goal, so here is what I really need:

I have a step that should fill out the form with data provided over .feature file. Form should accept the data in 'Dictionary <string, string>' format (where key - field name and value - value to be set to that field). First option is to make use of Table(below the step do the following):

    When ...
    |Key |Value|
    |Name|Matt |
    |Age |20   |
    ... 

then create a method that would transform Table variable(parameter) to Dictionary, but here I have to create several scenarios with different data in Value column.

Second option is to make use of Scenario Outline&Examples but I guess it also is not good because what if my form has a good amount of fields and for each one should be a corresponding placeholder.

So, it seems to me that solution i am looking for is somewhere between these two approaches. Hope you can help me with it and please don't be too hard on me, I am a very beginner)
1

1 Answers

1
votes

It sounds like you are looking for a purely data driven test. Scenario outlines are how this is achieved in SpecFlow and behavior driven development. The extent to which you want to drive your test with data makes this test a poor fit for a BDD test. This is why you are having trouble with this — now that being said, there might be a way to meet in the middle. It would involve a scenario outline and some modified steps so the placeholder in the scenario outline references a key in the dictionary. Each step needs to accept this key as an argument and deal with the data accordingling:

When the form is filled in with "<Dictionary Key>"
Then the thing exists for "<Dictionary Key>"

Examples:
    | Dictionary Key |
    | key1           |
    | key2           |

With the the dictionary being a dictionary of dictionaries:

private readonly Dictionary<string, Dictionary<string, object>> testCaseData = new Dictionary<string, Dictionary<string, object>>()
{
    {
        "key1",
        new Dictionary<string, object>()
        {
            { "field1", "value1" },
            { "field2", "value2" }
        }
    }, {
        "key2",
        new Dictionary<string, object>()
        {
            { "field1", "value3" },
            { "field2", "value4" }
        }
    }
}

And your steps:

[When(@"the form is filled in with ""(.*)"""]
public void WhenSomething(string testCaseKey)
{
    var formData = testCaseData[testCaseKey];

    // Enter formData["field1"] in web form
    // Enter formData["field2"] in web form
    // Submit form
}

[Then(@"Then the thing exists for ""(.*)"""]
public void ThenTheThingExistsFor(string testCaseKey)
{
    var expectedData = testCaseData[testCaseKey];

    // Make your assertions
}

This should at least remove the complexity of the data from your feature file, so you can focus the phrasing of each step on the business operation. You also don't necessarily need to hard code the data in C#. I supposed you could save it as a text, CSV or excel spreadsheet, read it, and parse it out into something usable, if that works better.