开发者

Designing unit tests for XML documents

开发者 https://www.devze.com 2023-01-18 08:11 出处:网络
I have a following method which takes a XDocument, iterates through the nodes and remove/replace nodes based on some condition.

I have a following method which takes a XDocument, iterates through the nodes and remove/replace nodes based on some condition.

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

Now I want to design unit tests in VS 2010 for the above method. I have the sample input and expected output in a store (XML/database) and I am thinking of passing the input data to the functions and matching their output with the expected output. My question is should I write only one [TestMethod] for Format(XDocument) or should I write one each for RemoveNode() and ReplaceNode(). Writing only one [TestMethod] for Format() is easy but I am not sure If this violates the principle of unit testing in which one should test only one thing at a time (and many things are happening in that Format() method). Also, I am not sure If I choose to test Rep开发者_JAVA百科laceNode() and RemoveNode(), how do I write the test methods for them i.e. what data I should pass to those methods. Could anybody give me any directions?

Should the test methods be like the following :-

[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
*/
}


It certainly looks like you're doing two different things based on two different conditions, so I would write two different tests - possibly more (to test different combinations).

As for what data to pass to the methods - you haven't told us anything about the conditions involved. Are they just based on the data you pass to the method, or do they depend on (say) the way the class under test is constructed? Basically provide each test with a simple document (simple enough that both the "before" and "after" will be easy to understand) which lets you check whether the relevant node has been replaced/removed or not.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号