kumar
kumar

Reputation: 1127

unit testing in Vs 2010

I am having two test cases in xml.i am using vs 2010 for unit testing using c#.i have created a single testmethod which is used above xml file in order to read the values.

Here,my question is if first test case got fail.how to run the next test case at the same time.Is there any way how many testcases got fail or passed in a single run.

  <Testcases>
     <testcase>
         <id>1</id>
         <name>A</name>
      </testcase>
       <testcase>
         <id>1</id>
         <name>B</name>
      </testcase>

  <Testcases>

Upvotes: 1

Views: 202

Answers (2)

CRice
CRice

Reputation: 12567

What about trying TestCaseSource by nunit.

This way you can point your test to a method which returns the data after reading from your xml.

public class TestCase
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class XmlReader
{
    public static IEnumerable<TestCase> TestCases
    {

        get
        {
            // replace this with reading from your xml file and into this array
            return new[]
                       {
                           new TestCase {Id = 1, Name = "A"},
                           new TestCase {Id = 1, Name = "B"}
                       };
        }
    }
}

[TestFixture]
public class TestClass
{
    [TestCaseSource(typeof(XmlReader), "TestCases")]
    public void SomeTest(TestCase testCase)
    {
        Assert.IsNotNull(testCase);
        Assert.IsNotNull(testCase.Name);
    }
}

Upvotes: 0

backtrack
backtrack

Reputation: 8144

[TestMethod]
public void TestDerpMethod(int a, string b, bool c)
{
    //...test code...
}

You can do multiple test cases like so:

[TestMethod]
[TestCase(12, "12", true)]
[TestCase(15, "15", false)]
public void TestDerpMethod(int a, string b, bool c)
{
    //...test code...
}

You can also use this method with XML using this method:

<Rows>
    <Row>
        <A1>1</A1>
        <A2>1</A2>
        <Result>2</Result>
    </Row>
    <Row>
        <A1>1</A1>
        <A2>2</A2>
        <Result>3</Result>
    </Row>
    <Row>
        <A1>1</A1>
        <A2>-1</A2>
        <Result>1</Result>
    </Row>
</Rows>

and the C#:

[TestMethod]
[DeploymentItem("ProjectName\\SumTestData.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
                   "|DataDirectory|\\SumTestData.xml",
                   "Row",
                    DataAccessMethod.Sequential)]
public void SumTest()
{
    int a1 = Int32.Parse((string)TestContext.DataRow["A1"]);
    int a2 = Int32.Parse((string)TestContext.DataRow["A2"]);
    int result = Int32.Parse((string)TestContext.DataRow["Result"]);
    ExecSumTest(a1, a2, result);
}

private static void ExecSumTest(int a1, int a2, int result)
{
    Assert.AreEqual(a1 + a2, result);
}

hope this will be help ful

refer this link

http://sylvester-lee.blogspot.in/2012/09/data-driven-unit-testing-with-xml.html

and also

http://social.msdn.microsoft.com/Forums/vstudio/en-US/7f6a739a-9b12-4e8d-ad52-cdc0ca7a2752/using-xml-datasource-in-unit-test

Upvotes: 1

Related Questions