tags:

views:

99

answers:

2

Hi All,

I have a simple testmethod

public double Divide(double numerator, double denominator)

{ if (denominator == 0) { throw new NullReferenceException("Cannot divide by zero."); } return numerator / denominator;

}

Now my Testcase data file looks like this

<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>

What should be the way to include all these testcases into a single test method. My basic problem is to handle the exception test method. I know I can use [ExpectedException(typeof(DivideByZeroException)] attribute into test method, but in that case this method will not fit for other 2 test csaes.

Could someone please help me how I can accommodate all these test cases into a single method. Thanks in advance

Cheers, Pritam

A: 

You could catch the DivideByZeroException inside you test method and then Assert.Sucess(); (inside the catch block)

Pedro
I can catch DivideByZeroException exception but how I will confirm that it is coming only for my Invalid test case. It may happen developer have written some wrong code and I am getting this exception for valid test cases also. Please help.
Pritam Karmakar
When is a divide by 0 ever a valid test case?
dcp
@dcp when you're making sure your code will throw this exception when divding by zero. @Pritam you can set some context variable informing you that in this specific testcase you want that exception to happen. It can be the actual denominator being equal to zero.
Pedro
A: 

Something like this:

public void TestQuotients() {
    try {
      // do the test which causes divide by 0
      int x = 0;
      int y = 10 / x;

      Assert.Fail("should have gotten exception");
    }
    catch (DivideByZeroException e) {
      // expected behavior
    }

    try {
      // do the next test which causes divide by 0
      int k = 0;
      int t = 100 / k;
      Assert.Fail("should have gotten exception");
    }
    catch (DivideByZeroException e) {
      // expected behavior
    }

    // this test doesn't cause exception
    double x = 100;
    double y = 10;
    Assert.AreEqual(10,x/y,"The quotient is wrong.");
}
dcp
[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"];}
Pritam Karmakar
So there is no need to fetch individual test cases.
Pritam Karmakar
I think you need to do a better job of explaining your requirements. The technique I've given you above should work as far as being able to handle expected exceptions with multiple tests cases in a single method. I'm not clear on what you are expecting here.
dcp