用于单元测试的XML数据
我们希望通过一些单元测试来运行大量数据。我们希望在某种excel电子表格或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
我可以预见这会有异常捕获的问题。欢迎对此发表任何意见。可以驱动来自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 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>
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 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>