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

问题描述 投票:0回答:1

我使用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,但这只能让我走到一半,因为我仍然需要一些方法来发现测试用例并将它们全部显示出来。

c# xml visual-studio xslt xunit
1个回答
1
投票

以下是组合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);
            }

        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.