Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run unit test with multiple datasource?

I can't find a way to run different unit tests within the same test class wich are using different DataSource.

Here's an example of a test class:

  namespace Calc.Tests
  {
     [TestClass]
     public class CalcTests
     {
        private static TestContext Context { get; set; }

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
           Context = context;
        }

        [TestMethod]
        [DeploymentItem("AddedValues.csv")]
        [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"|DataDirectory|\AddedValues.csv", "AddedValues#csv", DataAccessMethod.Sequential)]
        public void Add_UsingValuesWithinCsv()
        {
           Calc calc = new Calc();

           // Arrange
           int firstValue = Convert.ToInt32(Context.DataRow["firstValue"]);
           int secondValue = Convert.ToInt32(Context.DataRow["secondValue"]);
           int expectedResult = Convert.ToInt32(Context.DataRow["expectedResult"]);

           // Act
           int result = calc.Add(firstValue, secondValue);

           // Assert
           Assert.AreEqual<int>(result, expectedResult);
        }

        [TestMethod]
        [DeploymentItem("AddedValues.xml")]
        [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", @"|DataDirectory|\AddedValues.xml", "TestCase", DataAccessMethod.Sequential)]
        public void Add_UsingValuesWithinXml()
        {
           Calc calc = new Calc();

           // Arrange
           int firstValue = Convert.ToInt32(Context.DataRow["firstValue"]);
           int secondValue = Convert.ToInt32(Context.DataRow["secondValue"]);
           int expectedResult = Convert.ToInt32(Context.DataRow["expectedResult"]);

           // Act
           int result = calc.Add(firstValue, secondValue);

           // Assert
           Assert.AreEqual<int>(result, expectedResult);
        }
     }
  }

If I run only the unit test using data from an xml, everything runs fine.

If I run only the unit test using data from the csv, eveything runs fine.

If I run all tests within the test class, the second unit test running ends in error.

Is there a way to make sure the DataSource is reset before every unit test?

I've looked at TestCleanup and TestInitialize but didn't find anything to do so...

like image 757
Doum Avatar asked Nov 02 '22 05:11

Doum


1 Answers

I Got it!

I've misunderstood the use of the TestContext wich i though i had to initialize within my ClassInitialize method.

Because i had my ClassInitialized as a static, i needed to set my TestContext as static too and the TestContext was initialized only once at the ClassInitialize.

According to MSDN documentation my TestContext property needed to be an instance property and MSTest would provided automaticly the TestContext for me. http://msdn.microsoft.com/en-us/library/ms404699(v=vs.80).aspx

Doing so, made my TestContext.DataRow works fine with the DataSource provided as the TestMethod attribute.

Hope this might help someone, someday!

like image 86
Doum Avatar answered Nov 15 '22 05:11

Doum