我有一个以下方法,它采用XDocument,遍历节点并根据某些条件删除/替换节点。为XML文档设计单元测试
public static void Format(XDocument xDocument)
{
foreach(XmlNode documentNode in xDocument)
{
XmlNode[] spanNodes =documentNode.SelectNodes("//span") ;
foreach(XmlNode spanNode in spanNodes)
{
if(spanNode .Attributes["class"]!=null
&& !string.IsNullOrEmpty(spanNode.Attributes["class"].value)))
{
string styleName = spanNode.Attributes["class"].value;
string styleActionMapping = GetActionMappingForStyle (styleName);
switch (styleActionMapping)
{
case StyleActionMapping.Remove
RemoveSpanNode(spanNode);
break;
case StyleActionMapping.ReplaceWith
ReplaceSpanNode(spanNode);
break;
case StyleActionMapping.Keep
break;
}
}
}
}
}
现在我想在VS 2010中为上述方法设计单元测试。我有一个商店(XML /数据库)中的示例输入和期望的输出,我正在考虑将输入数据传递给函数,并将它们的输出与预期输出进行匹配。我的问题是我应该写格式(XDocument)只有一个[TestMethod],或者我应该为RemoveNode()和ReplaceNode()写一个。为Format()编写只有一个[TestMethod]很容易,但我不确定如果这违反了单元测试的原则,即单次测试只有一件事(并且在Format()方法中发生了许多事情)。另外,我不确定如果我选择测试ReplaceNode()和RemoveNode(),我该如何为它们编写测试方法,即应该将哪些数据传递给这些方法。有人可以给我任何指示吗?
如果测试方法是这样的: -
[TestMethod]
CheckExpectedOutput_OnRemove(XDocument document)
{
/*
1) document has data only for remove
2) call Format() and get the output
3) Check the output in the step 2 and match with the expected output
*/
}
[TestMethod]
CheckExpectedOutput_OnReplace(XDocument document)
{
/*
1) document has data only for replace
2) call Format() and get the output
3) Check the output in the step 2 and match with the expected output
*/
}
@Jon,“你没有告诉我们任何有关的条件” - 我用示例代码更新了问题。你可以在http://stackoverflow.com/questions/3792537/designing-unit-test-cases-for-an-application-which-manipulates-xhtml上看到更多的细节。 “条件”由XDocument本身的数据决定。 – 2010-09-30 12:56:19
@ydobonmai:在这种情况下很容易:您的测试用例基本上会针对不同的条件和不同的预期输出具有不同的输入。 – 2010-09-30 13:08:27
@Jon,这是否意味着我只有一个使用公共方法Format()的测试方法,并针对不同的输入运行相同的测试方法? – 2010-09-30 13:17:12