How to run unit test with multiple data sources?

I cannot find a way to run different unit tests in the same class that uses a different DataSource .

Here is 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 use only unit test, using data from xml, everything works fine.

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

If I run all the tests in the test class, starting the second unit test fails.

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

I looked at TestCleanup and TestInitialize but found nothing ...

+3
2

!

TestContext, ClassInitialize.

ClassInitialized , TestContext , TestContext ClassInitialize.

MSDN TestContext , MSTest TestContext . http://msdn.microsoft.com/en-us/library/ms404699(v=vs.80).aspx

, TestContext.DataRow DataSource, TestMethod.

, - -!

+2

, , TestInitialize/TestCleanUp?

, .

[TestInitialize()]
public void RunsBeforeEveryTest() { }

[TestCleanup()]
public void RunsAfterEveryTest() { }
0

All Articles