I am developing a test using SpecFlow and I am obtaining test arguments via ScenarioContext object.
I want to Serialize an object of type List<Dictionary<string, string>>, to produce the output as below:
"TestCaseArguments":[
{"First Number":-50, "Second Number": 70, "Expected Result":20},
{"First Number":30, "Second Number": -30, "Expected Result":0},
{"First Number":-60, "Second Number": 30, "Expected Result":-30}
],
The count of test arguments such as "First Number", etc. in the following example, is varying in each test case and also the names of keys are not fixed, they change for each test case.
Note: TestCaseArguments is defined as follows inside a class:
public IList<Dictionary<string, string>> TestCaseArguments { get; set; }
In my c# .net code, I am able to get each argument as a key-value pair inside a myList as follows:
List<Dictionary<string, string>> myList = new List<Dictionary<string, string>>();
for (i = 0; i < keyList.Length; i++)
{
myList.Add(new Dictionary<string, string> { [keyList[i]] = valueList[i] });
}
As I iterate over myList and add each item to the TestCaseArguments List, I get something like this below:
"TestCaseArguments":[
{"First Number":-50}, {"Second Number": 70}, {"Expected Result":20},
{"First Number":30}, {"Second Number": -30}, {"Expected Result":0},
{"First Number":-60}, {"Second Number": 30}, {"Expected Result":-30}]
So, that is exactly the problem. As you may have noticed closely, I am getting {"First Number":-50}, {"Second Number": 70}, {"Expected Result":20}, instead of {"First Number":-50, "Second Number": 70, "Expected Result":20}.
I could try changing my JSON structure, however I hope there a simple way to achieve this desired structure with some expert coding advice.