10

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.

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

7 Comments

So with this method I will have to read the files from two folder, pair them on basis of their IDs and then save the paired file names in a CSV file which I read by the test case. Is there a more direct method to do it? Like directly passing array to the DataSource?
In your question you didn't mention anything about paring. They seem to be already paired. Just arrays of strings (I suggest the paths), so my solution was to put these arrays (the paired paths) in a scv and read them from there. That way you don't need to duplicate the test case and adding more paired paths without changing the code. If you have extra functionality in your test edit your question.
Btw, my answer is not restricted on just 4 files. If at any time you want to add a new pair you simply edit the csv and add a new row. That's all. The test case will also execute this row
I have updated the question with the details. Sorry for not being clear enough.
@Schaliasos Yes I think your solution will definitely work. Just wondering if there is a more direct method since as the project progresses there will be more added complexity with different type of files for which I have to write different TestClasses.
|
2

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.

1 Comment

That looks the same as the OP's original solution that he's trying to avoid, e.g. "it only gives one single output and stops when any pair of input files fails the test"
0

Simply equate could work Datasource instance=Array. Data source rows in loop and row value as array instance.

1 Comment

Can you please elaborate a little bit? Maybe give some demo code for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.