12

I want to test the following method in C# for all code paths.

public int foo (int x)
{
    if(x == 1)
        return 1;
    if(x==2)
        return 2;
    else
        return 0;
}

I've seen this pex unit testing where multiple inputs are tested. How can I create a unit test that accepts multiple inputs?

[TestMethod()] //some setup here??
    public void fooTest()
    {
         //some assert
    }

I want to avoid creating a test method for each input. I am working with Visual Studio 2010/2012 and .Net 4.0

Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
Gobliins
  • 3,848
  • 16
  • 67
  • 122
  • 1
    possible duplicate of [How to run a test method with multiple parameters in MSTest?](http://stackoverflow.com/questions/9021881/how-to-run-a-test-method-with-multiple-parameters-in-mstest) – whosrdaddy Jan 03 '13 at 14:00

3 Answers3

23

You can use XML, Database, or CSV datasources MS Test. Create FooTestData.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Rows>
  <Row><Data>1</Data></Row>
  <Row><Data>2</Data></Row>
</Rows>

And set it as datasource for your test:

[TestMethod]
[DeploymentItem("ProjectName\\FooTestData.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
                   "|DataDirectory|\\FooTestData.xml", "Row",
                    DataAccessMethod.Sequential)]
public void FooTest()
{
    int x = Int32.Parse((string)TestContext.DataRow["Data"]);
    // some assert
}

BTW with NUnit framework it's match easier - you can use TestCase attribute to provide test data:

[TestCase(1)]
[TestCase(2)]
public void FooTest(int x)
{
   // some assert
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Ok in the body i would then have multiple asserts? – Gobliins Jan 03 '13 at 12:39
  • @Gobliins why not? There is no limit for asserts count – Sergey Berezovskiy Jan 03 '13 at 12:39
  • 2
    @Gobliins Again, using NUnit you can use the named parameter Result and the tests may be simplified further, dropping the Asserts `[TestCase(1, Result=1)]` `[TestCase(2, Result=2)]` – mhoff Jan 03 '13 at 20:02
  • When the function parameter is a byte array instead of an int, how would the [TestCase(..., Result)] look like? – Gobliins Jan 04 '13 at 13:09
  • 1
    @Gobliins `[TestCase(new byte[] { 1, 2 }, Result = 3)]` – Sergey Berezovskiy Jan 04 '13 at 13:44
  • @lazyberezovsky sorry and in the xml for data driven test do ihave to parse the values standing in the xml field? (seems like nunit don´t like my 64bit vs2010 or some assemblies) – Gobliins Jan 04 '13 at 14:48
  • 1
    @Gobliins try to reinstall nunit from nuget (on my 64bit system I also had that issue once). And with mstest you should parse values manually - `((string)TestContext.DataRow["Data"]).Split(',').Select(s => Int32.Parse(s))` – Sergey Berezovskiy Jan 04 '13 at 14:51
  • @lazyberezovsky thx again for the help, now Nunit runs but when using two times [TestCase(new byte[]{1,2], Result = ...)] with result (first test: true, second test: false) then all my testcases await the result from the first testcase, is this a bug? – Gobliins Jan 04 '13 at 18:34
  • @Gobliins I think it could be a bug of test runner you use - try to run tests from NUnit gui or something like testdriven.net. All should work like a charm – Sergey Berezovskiy Jan 04 '13 at 19:50
8

If using NUnit parameterized tests is the way to go

mhoff
  • 403
  • 4
  • 11
3

In MS Test you can create data driven tests that accept different inputs for the same test method.

Here's a blog post on it: http://toddmeinershagen.blogspot.ca/2011/02/creating-data-driven-tests-in-ms-test.html

Mike Parkhill
  • 5,511
  • 1
  • 28
  • 38