Set Up Test Method with different inputs

前端 未结 3 1291
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-08 01:45

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
          


        
相关标签:
3条回答
  • 2021-02-08 02:07

    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

    0 讨论(0)
  • 2021-02-08 02:18

    If using NUnit parameterized tests is the way to go

    0 讨论(0)
  • 2021-02-08 02:21

    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
    }
    
    0 讨论(0)
提交回复
热议问题