Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于单元测试的XML数据_Xml_Unit Testing_Testing_Nunit_Mstest - Fatal编程技术网

用于单元测试的XML数据

用于单元测试的XML数据,xml,unit-testing,testing,nunit,mstest,Xml,Unit Testing,Testing,Nunit,Mstest,我们希望通过一些单元测试来运行大量数据。我们希望在某种excel电子表格或XML文档中定义这一点 单元测试框架可以加载这些数据作为输入和期望 我可以预见这会有异常捕获的问题。欢迎对此发表任何意见。可以驱动来自XML文档或数据库等数据源的测试。对于MSTest,请查看属性。nUnit fixture只是POCO,因此您可以使用.NET类以任何方式设置它。因为它可能包含大量数据,我可能会在TestFixtureSetUp中进行设置,该设置在整个套件中运行一次: [TestFixture] publi

我们希望通过一些单元测试来运行大量数据。我们希望在某种excel电子表格或XML文档中定义这一点

单元测试框架可以加载这些数据作为输入和期望


我可以预见这会有异常捕获的问题。欢迎对此发表任何意见。

可以驱动来自XML文档或数据库等数据源的测试。

对于MSTest,请查看属性。

nUnit fixture只是POCO,因此您可以使用.NET类以任何方式设置它。因为它可能包含大量数据,我可能会在TestFixtureSetUp中进行设置,该设置在整个套件中运行一次:

[TestFixture]
public class Foo{

  private XmlDocument doc;
  private BarClass bar;

 [TestFixtureSetUp]
  public void FixtureSetUp(){
     doc = new XmlDocument();
     doc.Load("c:\file.xml");
  }

  [SetUp]
  public void SetUp(){
      BarClass = new BarClass();
  }

  [Test]
  public void TestX(){
     Assert.That(BarClass.DoSOmething(doc), Is.Baz);
  }

}

在花了几个小时寻找如何做到这一点(因为数据中嵌入了一些格式,所以我无法使用CSV文件)之后,我设法找到了如何在MSTest中使用XML的方法

这是一个小样本。希望能有帮助

假设您有一个简单的类库:

public class GetStrings
{
    public string RichardIII(string lookup)
    {
        string results;
        switch(lookup)
        {
            case "winter":
                {
                    results =
                        "Now is the winter of our discontent\nMade glorious summer by this sun of York;\nAnd all the clouds that lour'd upon our house\nIn the deep bosom of the ocean buried. ";
                    break;
                }
            case "horse":
                {
                    results =
                        "KING RICHARD III \nA horse! a horse! my kingdom for a horse!\n\nCATESBY \n\"Withdraw, my lord; I'll help you to a horse.\"";
                    break;
                }
                default:
                results = null;
                break;
        }
        return results;
    }
}
因此,将检查此方法一部分的单元测试如下所示:

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\TestStrings.xml", "Test", DataAccessMethod.Sequential), DeploymentItem("GetStringsTest\\TestStrings.xml"), TestMethod]
public void RichardIIITest()
{
    GetStrings target = new GetStrings(); 

    string lookup = TestContext.DataRow["find"].ToString();
    string expected = TestContext.DataRow["expect"].ToString();
    if (expected == "(null)")
        expected = null; 

    string actual = target.RichardIII(lookup);
    Assert.AreEqual(expected, actual);
}
<TestStrings>
    <Test find="winter" expect="Now is the winter of our discontent&#10;Made glorious summer by this sun of York;&#10;And all the clouds that lour'd upon our house&#10;In the deep bosom of the ocean buried. "/>
    <Test find="horse" expect="KING RICHARD III &#10;A horse! a horse! my kingdom for a horse!&#10;&#10;CATESBY &#10;&#34;Withdraw, my lord; I'll help you to a horse.&#34;"/>
    <Test find="blah blah" expect="(null)"/>
</TestStrings>
xml文件TestStrings.xml如下所示:

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\TestStrings.xml", "Test", DataAccessMethod.Sequential), DeploymentItem("GetStringsTest\\TestStrings.xml"), TestMethod]
public void RichardIIITest()
{
    GetStrings target = new GetStrings(); 

    string lookup = TestContext.DataRow["find"].ToString();
    string expected = TestContext.DataRow["expect"].ToString();
    if (expected == "(null)")
        expected = null; 

    string actual = target.RichardIII(lookup);
    Assert.AreEqual(expected, actual);
}
<TestStrings>
    <Test find="winter" expect="Now is the winter of our discontent&#10;Made glorious summer by this sun of York;&#10;And all the clouds that lour'd upon our house&#10;In the deep bosom of the ocean buried. "/>
    <Test find="horse" expect="KING RICHARD III &#10;A horse! a horse! my kingdom for a horse!&#10;&#10;CATESBY &#10;&#34;Withdraw, my lord; I'll help you to a horse.&#34;"/>
    <Test find="blah blah" expect="(null)"/>
</TestStrings>