【问题标题】:Using xunit or nunit as test runner framework使用 xunit 或 nunit 作为测试运行框架
【发布时间】:2011-09-13 05:10:40
【问题描述】:

我将从一个问题开始。我有一个小图书馆。它遍历文本文件中的 url 列表并根据 url 返回 200 或不创建断言。

我们的目标是制作像 Reshaprer、Testdriven.net 这样的测试运行程序或像 team city 或 hudson 这样的 CI 服务器来获取断言作为测试。

我的问题是如何扩展/构建 xunit 或 nunit 以从其他来源运行测试? 例子会很棒。

更新 为了让事情更清楚一点。 我应该能够在 xunit/nunit 附带的测试运行器中加载 dll 并运行测试。

如何填充 url 列表是非常动态的。根据某些事情,可以创建更多 url。所以数据列表不是一种选择。

【问题讨论】:

    标签: .net nunit xunit


    【解决方案1】:

    如果您的意思是如何以编程方式运行 NUnit 测试(而不是使用提供的 NUnit 运行器之一),那么它实际上非常简单:

    using System;
    using NUnit.Core;
    using NUnit.Framework;
    using MyProject.Tests;  /* assuming MyTestFixture is defined in this project/assembly */
    
    namespace TestRunner.DemoApp {
        class Program {
    
        static void Main(string[] args) {
            var builder = new TestSuiteBuilder();
            var testAssemblyLocation = typeof(MyTestFixture).Assembly.Location;
            var package = new TestPackage(testAssemblyLocation);
            var suite = builder.Build(package);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Running tests from " + testAssemblyLocation;
            Console.WriteLine("Testing against " + ConfigurationManager.AppSettings["HostNameSuffix"]);
            var result = suite.Run(new NullListener(), TestFilter.Empty);
            switch (result.ResultState) {
                case ResultState.Success:
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Pass.");
                    break;
                case ResultState.Error:
                case ResultState.Failure:
                    Console.ForegroundColor = ConsoleColor.Red;
                    DrawResults(result.Results, r => r.ResultState == ResultState.Error || r.ResultState == ResultState.Failure);
                    break;
            }
    
            static void DrawResults(IList results, Func<TestResult, bool> include) {
                foreach (var obj in results) {
                    var result = obj as TestResult;
                    if (result == null) continue;
                    if (result.Results != null && result.Results.Count > 0) {
                        DrawResults(result.Results, include);
                    } else if (include(result)) {
                        Console.WriteLine(result.Name);
                        Console.WriteLine(result.Message);
                        Console.WriteLine(result.ResultState);
                        Console.WriteLine(result.StackTrace);
                        Console.WriteLine(String.Empty.PadLeft(Console.WindowWidth, '='));
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 不是我想要的。我不需要构建测试运行器。我需要在 nunits 测试运行器中运行测试。
    【解决方案2】:

    使用xUnit,你只需要实现ITestRunner接口,即:

    public class MyTestRunner : ITestRunner
    {
        // Methods
        public MyTestRunner();
        public static TestRunState RunAssembly(TestRunner runner);
        public static TestRunState RunClass(TestRunner runner, Type type);
        public static TestRunState RunClassWithInnerTypes(TestRunner runner, Type type);
        public static TestRunState RunMethod(TestRunner runner, MethodInfo method);
        TestRunState ITestRunner.RunAssembly(ITestListener listener, Assembly assembly);
        TestRunState ITestRunner.RunMember(ITestListener listener, Assembly assembly, MemberInfo member);
        TestRunState ITestRunner.RunNamespace(ITestListener listener, Assembly assembly, string ns);
    }
    

    有关实现细节,请获取 xUnit 的源代码并查看示例运行器。

    【讨论】:

      【解决方案3】:

      您可以在 NUnit 中使用TestCaseSourceAttribute 来运行动态测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-05
        • 1970-01-01
        • 1970-01-01
        • 2018-04-23
        • 2015-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多