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...
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With