简体   繁体   中英

Designing unit test cases for an application which manipulates XHTML

I am working on an application which would take an XHTML.

<documents>
<document>
    <span class="style1"> This is some text1 </span>
    <span class="style2"> This is some text2 </span>
    <span class="style3"> This is some text2 </span>
</document>
</documents>

The values for class attribute are basically styles. Those styles are mapped to certain actions in database. The actions tells my application what to do with those span tags when my application sees those styles.

Style - Action - ReplaceText

Style1 - Remove - NULL

Style2 - Keep - NULL

Style3 - Replace - H1

The application has the following output :-

<documents>
<document>
     This is some text1 
    <span class="style2"> This is some text2 </span>
    <h1> This is some text2 </h1>
</document>
</documents>

Following is kinda pseudocode I am thinking of:-

foreach(XmlNode documentNode in documentNodes)
{
    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;

   }

  }
 }
}

The input could be quite more complex than what I showed above and the application could be very prone to bugs. S, I wanted to use unit testing so that when one makes any changes to the app, i can run the unit tests and be confident of them being working still. So, I wanted to have a simple table like this with pre-populated data:-


Id - Scenario - Input - ExpectedOutput

and I want to test with my app code with this unit testing data using Visual studio.NET 2010. Could anybody provide me directions on this.

Sorry for this long question. I am a newbie in unit testing and I just wanted to be as much clear as possible. Feel free to ask questions.

I'm assuming the final part of the post is really the question: how to write data-driven tests in .NET.

I like having simple files which are embedded into the unit test assembly (Build action: embedded resource). You can then use Assembly.GetManifestResourceStream to fetch the data. Sometimes I'll have one file for input and one for output; in other cases I can have one single file which contains multiple inputs and outputs. That would certainly be doable for XML:

<tests>
  <test id="FooBar">
     <input>
       ...
     </input>
     <expected-output>
       ...
     </expected-output>
  </test>
<tests>

Then you could just load a single document, then fetch the input and output for a single test; transform it into a standalone document if necessary, then pass it to your production code, and finally compare the output. Admittedly comparing the output can be tricky - it will depend on things like white space compression... but it's usually doable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM