Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data driven testing in C# using arrays

I have a test method which takes two XML files as input and compares them. I am using Microsoft.VisualStudio.TestTools.UnitTesting framework on .NET 4.5. I want to modify the test method such that it takes multiple XML files (two at a time in pair), runs the test and gives the results separately.

I tried the following code but it only gives one single output and stops when any pair of input files fails the test.

 string[] source = {file1, file2, file3, file4....};
 string[] target = {fileA, fileB, fileC, fileD....};

 [Test Method]
 public void TestCase01()
 {
      TestLogic testObj = new TestLogic(); //class containing the comparison method
      for (int i = 0; i < source.Length; i++)
      {
            Assert.IsTrue (testObj.VerifyFiles(source[i], target[i]));
      }
 }

Upon doing some research I found out that DataSource attribute can be used. But I do not know how to pass two arrays (or a single two dimensional array) to the DataSource attribute. I would prefer to use Microsoft.VisualStudio.TestTools.UnitTesting for testing and other 3rd party frameworks like NUnit only as a last resort.

Edit: I do not know the number of input files. I used 4 files just as an example. Before passing the files to the TestMethod, I pair them using their IDs. So I first read two set of files from two different folders, pair them based on their ID and then pass the paired files to the test case for testing. The way I am doing it now is that I save the paired file names (source and target) in an array or list and then pass them to the test case. Obviously this method is not working and I am experiencing the problem as mentioned above.

like image 656
Vijay Avatar asked Jul 23 '13 05:07

Vijay


2 Answers

You can use as DataSource a csv file that will have tow columns (one for source and one for target). Then in your test use it as follow:

[TestClass]
public class TestCase
{
    [TestMethod]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "files.csv", "files#csv", DataAccessMethod.Sequential)]
    public void TestCase()
    {
        TestLogic testObj = new TestLogic();

        string source = (string) TestContext.DataRow["source"]; // get the value from the 'source' column
        string target = (string) TestContext.DataRow["target"]; // get the value from the 'target' column

        Assert.IsTrue(testObj.VerifyFiles(source, target));
    }

   public TestContext TestContext{ get; set; }
}

The test will iterate through the rows of the DataSource and will run one time for each row.

Check here for more details.

like image 129
chaliasos Avatar answered Sep 24 '22 14:09

chaliasos


I had a similar problem and at the very end followed the recommendation from this blog post,

We used an array of Anonymous Types to store our set of conditions, and then used LINQ's ForEach() method to loop through the array and run the test for each element.

like image 30
oldbam Avatar answered Sep 24 '22 14:09

oldbam