【问题标题】:unit testing in Vs 2010Vs 2010 中的单元测试
【发布时间】:2013-08-12 09:04:08
【问题描述】:

我在 xml 中有两个测试用例。我正在使用 vs 2010 使用 c# 进行单元测试。我创建了一个测试方法,用于在 xml 文件上方读取值。

在这里,我的问题是第一个测试用例是否失败。如何同时运行下一个测试用例。有没有办法在一次运行中有多少测试用例失败或通过。

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

  <Testcases>

【问题讨论】:

  • 为什么要用 XML 定义测试?
  • 它们可能是两个或多个测试用例,因为现在我们在 xml 文件中提供输入数据。

标签: c# asp.net visual-studio-2010


【解决方案1】:
[TestMethod]
public void TestDerpMethod(int a, string b, bool c)
{
    //...test code...
}

你可以像这样做多个测试用例:

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

您也可以使用此方法将此方法与 XML 一起使用:

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

和 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);
}

希望这会有所帮助

参考这个链接

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

还有

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

【讨论】:

    【解决方案2】:

    试试怎么样 TestCaseSource 来自 nunit。

    这样你可以将你的测试指向一个从你的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);
        }
    }
    

    【讨论】:

    • 好建议@CRice.but目前我们正在使用vs 2010的内置单元测试
    猜你喜欢
    • 2011-05-13
    • 1970-01-01
    • 2011-03-22
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    相关资源
    最近更新 更多