用于单元测试的 XML 数据

发布于 2024-08-02 20:57:41 字数 133 浏览 4 评论 0原文

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

是否有办法让单元测试框架加载这些数据作为输入和期望。

我可以预见这会出现异常捕获的问题。对此的任何评论也将受到赞赏。

We are looking to run a large amount of data through some unit tests. We would like to define this in some sort of excel spreadsheet or XML document.

Is there away to get the unit testing framework to load this data as input and expectations.

I can foresee this having issues with exception catching. Any comments on this are appreciated as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

臻嫒无言 2024-08-09 20:57:41

nUnit 装置只是 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);
  }

}

nUnit fixtures are just POCO's so you can really set it up however you like using .NET classes. Because it could be a lot of data I would probably set it up in a TestFixtureSetUp which is run once for the entire suite:

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

}
后知后觉 2024-08-09 20:57:41

我花了几个小时寻找有关如何执行此操作的信息(因为数据中嵌入了一些格式,因此我无法使用 CSV 文件),我设法弄清楚如何将 XML 与 MSTest 一起使用。

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

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

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

xml 文件 TestStrings.xml 如下所示:

<TestStrings>
    <Test find="winter" expect="Now is the winter of our discontent
Made glorious summer by this sun of York;
And all the clouds that lour'd upon our house
In the deep bosom of the ocean buried. "/>
    <Test find="horse" expect="KING RICHARD III 
A horse! a horse! my kingdom for a horse!

CATESBY 
"Withdraw, my lord; I'll help you to a horse.""/>
    <Test find="blah blah" expect="(null)"/>
</TestStrings>

Having just spent a few hours looking for something on how to do this (because the data had some formatting embedded in it so I couldn't use a CSV file) I managed to work out how to use XML with MSTest.

This is a small sample. Hope it helps.

Suppose you have a simple class library:

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

So a unit test that will check out part of this method will look like this:

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

The xml file TestStrings.xml looks like this:

<TestStrings>
    <Test find="winter" expect="Now is the winter of our discontent
Made glorious summer by this sun of York;
And all the clouds that lour'd upon our house
In the deep bosom of the ocean buried. "/>
    <Test find="horse" expect="KING RICHARD III 
A horse! a horse! my kingdom for a horse!

CATESBY 
"Withdraw, my lord; I'll help you to a horse.""/>
    <Test find="blah blah" expect="(null)"/>
</TestStrings>
长亭外,古道边 2024-08-09 20:57:41

MBUnit 可以从 XML 文档或数据库等数据源驱动测试。

MBUnit can drive tests from data sources such as XML documents or databases.

随梦而飞# 2024-08-09 20:57:41

对于 MSTest,请查看 数据源< /a> 属性。

For MSTest, take a look at the DataSource attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文