使用复杂的序列化对象在Visual Studio上进行数据驱动测试 [英] Data Driven Test on Visual Studio using a complex serialized object

查看:111
本文介绍了使用复杂的序列化对象在Visual Studio上进行数据驱动测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我想做的事是否可行。我有一个由xml驱动的单元测试,如下所示:

I wonder if what I want to do is possible. I have a unit test driven by a xml, as follow :

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", 
            "|DataDirectory|\\MyFile.xml", 
            "TestMember", 
            DataAccessMethod.Sequential)]
[DeploymentItem("MyFile.xml")]
[TestMethod]
public void Hello()
{
    ...
}

其中MyFile.xml看起来像这样:

where MyFile.xml looks like this :

<TestMembers>
  <TestMember>
    <Name>Hello</Name>
    <id>1234</id>
    <MyComplexRow>
      <MySerializedInstanceOfClass>
        <BooleanProperty>true</BooleanProperty>
        <IntProperty>8</IntProperty>
        <MySerializedInstanceOfAnotherClass>
          <int>23</int>
          <bool>false</bool>
          <double>8.0</double>
        </MySerializedInstanceOfAnotherClass>
      </MySerializedInstanceOfClass>
    </MyComplexRow>
  </TestMember>
</TestMembers>

现在的问题是访问 TestContext.DataRow [ Name] 工作正常,但我无法访问 TestContext.DataRow [ MyComplexRow] ,我得到了列'MyComplexRow '不属于表TestMember。因为它不是标量,但是具有很多维...

Now the problem is that accessing TestContext.DataRow["Name"] works fine, but I can't access the TestContext.DataRow["MyComplexRow"], I get a Column 'MyComplexRow' does not belong to table TestMember. because it's not a "scalar", but has many dimensions...

因此,有没有我如何欺骗Visual Studio让他知道他正在处理现有类的序列化实例?例如,扩展现有的类。

So, is there any way I could trick Visual Studio to let him know he's dealing with a serialized instance of an existing class ? Like, extending an existing class perhaps.

我知道我可以将它们全部包装在CDATA中,然后反序列化,但是,它并不那么优雅。

I know I could wrap it all up in CDATA and then deserialize it, but, well, it's not as elegant.

谢谢!

推荐答案

我建议使用XUnit XUnit.Extensions 都可以通过nuget来获得。

I suggest using XUnit and XUnit.Extensions, both available through nuget.

装饰测试 [Theory] ​​ [ClassData] 的方法,您可以根据需要指定复杂的源,处理您希望如何反序列化(在这种情况下)您的XML源。然后,您可以将预期结果作为测试方法的参数。您还将遍历对象的行,然后再次由您来定义要作为行处理的对象。

Decorating a test method as a [Theory] and [ClassData] will let you specify a source as complex as you wish, letting you to deal how you wish to deserialize (in this here case) your XML source. You can then inject the expected results as parameters of your test method. You will also loop through "rows" of objects, and again it's up to you to define what you want to treat as a row.

嗯,我不确定这是不是很容易理解,但是您应该看看这篇小博文,其中清楚地举例说明了如何处理它。

Hmm, I'm not certain it's very comprehensible, but you should take a look at this small blog post, it clearly explains with examples how you deal with it.

结论:XUnit是一个很棒的测试框架,可以很好地与Visual Studio测试面板等集成。语法也大致相同。另外,它是免费的。

Conclusion: XUnit is a great testing framework, and will integrate nicely with Visual Studio test panel and such. Syntax is also mostly the same. Plus, it's free.

这篇关于使用复杂的序列化对象在Visual Studio上进行数据驱动测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆