views:

25

answers:

1

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 ReplaceNode() 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
*/
}
+2  A: 

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.

Jon Skeet
@Jon, "you haven't told us anything about the conditions involved" - I updated the question with the sample code. You can see more details on http://stackoverflow.com/questions/3792537/designing-unit-test-cases-for-an-application-which-manipulates-xhtml. The "condition" is determined by the data in the XDocument itself.
ydobonmai
@ydobonmai: In that case it's easy: your test cases will basically have different inputs for the different conditions, and different expected outputs.
Jon Skeet
@Jon, does this mean I have ONLY ONE test method using the public method Format() and run the same test method for different inputs?
ydobonmai
@ydobonmai: No, you have several test methods, each of which provides input to Format and checks the output.
Jon Skeet
@Jon, I updated the question with some sample test methods. Is this something you are pointing to or I get it completely wrong.
ydobonmai
@ydobonmai: The test methods wouldn't take XDocument parameters; they'd create the XDocuments themselves (possibly loading from assembly resources).
Jon Skeet
@jon, thanks for the comments. This would help.
ydobonmai