【发布时间】:2016-06-10 06:53:24
【问题描述】:
我正在尝试为失败的测试编写 Nunit 报告分析器/收集器之类的东西,但在尝试反序列化测试报告时卡住了。
Nunit 报告具有以下结构:
<test-results ... >
<test-suite>
<results>
<test-suite> or <test-case> bunch of elements
<failure> // optional
</results>
</test-suite>
</test-results>
所以 test-suite 元素可以具有包含其他测试套件元素的结果集合或包含测试用例元素的结果集合。 由于测试套件具有与测试用例相同的属性,因此可以将其序列化为一种类型的类:
[Serializable()]
public class TestResult
{
[XmlAttribute("name")]
public String Name { get; set; }
[XmlAttribute("executed")]
public String Executed { get; set; }
[XmlAttribute("success")]
public String Success { get; set; }
[XmlElement("failure", IsNullable = true)]
public Failure Failure { get; set; }
[XmlElement("results")]
public Results Results { get; set; }
[XmlAttribute("result")]
public String Result { get; set; }
[XmlAttribute("time")]
public String Time { get; set; }
[XmlAttribute("asserts")]
public String Asserts { get; set; }
}
[Serializable()]
public class TestCase : TestResult
{
}
[Serializable()]
public class TestSuite : TestResult
{
[XmlAttribute("type")]
public String Type { get; set; }
}
Results 类假设有一个测试套件或测试用例列表:
[Serializable()]
public class Results
{
[XmlArray("results")]
[XmlArrayItem("test-case", Type = typeof(TestCase))]
[XmlArrayItem("test-suite", Type = typeof(TestSuite))]
public List<Result> Result { get; set; }
}
这里的TestCase 和TestSuite 是Result 的空子类,因为arrtibutes 和元素是相同的。没有以这种方式从集合中序列化的项目。如果我试图为每个项目指定多个没有专用类型的 ArrauItem-s,则解析器认为这是明确的。
我怎样才能真正用不同但相关的元素来序列化集合?
【问题讨论】:
标签: c# xml serialization