为操作 XHTML 的应用程序设计单元测试用例

为操作 XHTML 的应用程序设计单元测试用例

问题描述:

我正在开发一个需要 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>

class 属性的值基本上是样式.这些样式映射到数据库中的某些操作.当我的应用程序看到这些样式时,这些操作会告诉我的应用程序如何处理这些 span 标签.

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.

Style1 - 删除 - NULL

Style1 - Remove - NULL

Style2 - 保持 - NULL

Style2 - Keep - NULL

样式 3 - 替换 - H1

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;

   }

  }
 }
}

输入可能比我上面显示的要复杂得多,并且应用程序可能很容易出现错误.S,我想使用单元测试,以便在对应用程序进行任何更改时,我可以运行单元测试并确信它们仍然可以正常工作.所以,我想要一个这样的简单表格,其中包含预先填充的数据:-

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:-

并且我想使用 Visual Studio.NET 2010 使用此单元测试数据测试我的应用程序代码.任何人都可以向我提供这方面的指导.

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.

我认为帖子的最后一部分是真正的问题:如何在 .NET 中编写数据驱动的测试.

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

我喜欢将简单的文件嵌入到单元测试程序集中(构建操作:嵌入资源).然后您可以使用 Assembly.GetManifestResourceStream 获取数据.有时我会有一个文件用于输入,一个文件用于输出;在其他情况下,我可以有一个包含多个输入和输出的文件.这对于 XML 肯定是可行的:

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.