我有一個以下方法,它採用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