使用样式表显示来自多个 XML 文件的 xUnit 测试数据



>我使用 xUnit 编写了一个测试,它使用 MemberData 属性通过此类发现包含测试数据的 XML 文件:

internal class XmlDataRetriever
{
    private const String XmlPath = @"....TestCases";
    public static IEnumerable<TestCase[]> Data
    {
        get
        {
            return
                CreateTestCases(
                    Directory.GetFiles(XmlPath, "*.xml", SearchOption.TopDirectoryOnly)
                    .ToReadOnlyCollection());
        }
    }
    private static List<TestCase[]> CreateTestCases(ReadOnlyCollection<String> filePaths)
    {
        return
            filePaths
                .Select(testCaseName =>
                    new TestCase[] { new XmlParser().GetTestCase(testCaseName) })
                .ToList();
    }
}

这里的代码不是那么重要,但它给出了如何发现测试用例的想法。

我想要实现的是在一个文档中查看这些XML测试用例列表的某种方式,最好是在Visual Studio中查看,但我不确定实现此目的的最佳方法。

我已经研究过使用 XSLT,但这只能让我完成一半,因为我仍然需要某种方法来发现测试用例并将它们一起显示。

下面是组合 xml 文件的示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        private const String XmlPath = @"....TestCases";
        static void Main(string[] args)
        {
            string xml = "<Root>";
            foreach (string file in Directory.GetFiles(XmlPath))
            {
                StreamReader reader = new StreamReader(file, Encoding.UTF8);
                //skip identification line
                reader.ReadLine();
                xml += reader.ReadToEnd();
            }
            xml += "</Root>";
        }
    }
}

如果要使用 xml linq,请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        private const String XmlPath = @"....TestCases";
        static void Main(string[] args)
        {
            XElement xRoot = new XElement("Root");
            foreach (string file in Directory.GetFiles(XmlPath))
            {
                XDocument doc = XDocument.Load(file);
                XElement root = doc.Root;
                xRoot.Add(root);
            }
        }
    }

最新更新