I have a very simple method. And I want to test it for both valid and invalid input data. In invalid data I want to catch the exact exception type that I will be mentioned in my test data file.
So here is my method
public double Divide(double numerator, double denominator)
{
if (denominator == 0)
{
throw new NullReferenceException("Cannot divide by zero.");
}
return numerator / denominator;
}
Now my Data file is
<TestCase>
<Numerator>-2.5</Numerator>
<Denominator>1</Denominator>
<ExpectedResult>-2.5</ExpectedResult>
</TestCase>
<TestCase>
<Numerator>55</Numerator>
<Denominator>5</Denominator>
<ExpectedResult>11</ExpectedResult>
</TestCase>
<TestCase>
<Numerator>5</Numerator>
<Denominator>0</Denominator>
<ExpectedResult>DivideByZeroException</ExpectedResult>
</TestCase>
Now in a single testmethod I want to execute all my test cases.
So here is my test method
[TestMethod]
[DeploymentItem("DDTTestProject\\TestData\\DivisionTest.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
"|DataDirectory|\\DivisionTest.xml",
"TestCase", DataAccessMethod.Sequential)]
public void TestDivide()
{
double numerator = Double.Parse((string)TestContext.DataRow["Numerator"]);
double denominator = Double.Parse((string)TestContext.DataRow["Denominator"]);
object result = TestContext.DataRow["ExpectedResult"];
}
Now I really confused how I can handle all test cases into this method. I shouldn't use [ExpectedException(typeof(DivideByZeroException)]
test method attribute as my first two test cases then will not work. Please help me how I can solve this problem
Thanks in Advance.
Cheers,
Pritam
Read more...