Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Unit Testing - XML Datasource containing multiple tests

I am new to the Unit Testing Framework. Using VS 2010, I use an XML as my datasource.

Suppose my XML would look like this:

<testgroup>
  <test>
    <param1>100</param1>
    <param2>200</param2>
  </test>
  <test>
    <param1>333</param1>
    <param2>222</param2>
  </test>
</testgroup>

So a test group could contain a lot of tests. It wouldn't be efficient to break them out in individual xml files. For simplicity, pretend that param1 is an int and param2 is another int and my test would be to verify that param2 > param1.

Is it possible to write a single TestMethod so that it iterates through the various tests from the XML such that the Unit Test Framework will show a test for each?

I haven't found a solution so far. Perhaps the datasource is not meant to be test data driven this way.

like image 222
Nic Avatar asked Jan 12 '13 00:01

Nic


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.


1 Answers

Using NUnit, you can do the following:

[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);
}
like image 144
Codeman Avatar answered Sep 21 '22 16:09

Codeman