使用 xunit 或 nunit 作为测试运行框架

发布于 2024-11-15 06:51:13 字数 354 浏览 2 评论 0原文

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

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

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

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

url 列表的填充方式是非常动态的。根据某些情况,可以创建更多 url。所以数据列表不是一个选择。

I'll start with an problem. I have a little library. It goes through a list of urls in text files and create assertions depending on the url returns 200 or not.

The goal is to make a test runner like Reshaprer, Testdriven.net or a CI server like team city or hudson to pick up the assertions as tests.

My question is how do I extend/build upon xunit or nunit to run tests from another source?
Examples would be great.

Update
To make things a bit more clear.
I should be able to look load the dll in the test runner that ships with xunit/nunit and run the test.

How the list of urls is populated is very dynamic. Depending on certain things more url can be created. So a list of data is not an option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗忘曾经 2024-11-22 06:51:13

如果您的意思是如何以编程方式运行 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, '='));
                }
            }
        }
    }
}

If you mean how can you run NUnit tests programmatically (instead of using one of the supplied NUnit runners), then it's actually pretty easy:

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, '='));
                }
            }
        }
    }
}
怼怹恏 2024-11-22 06:51:13

使用 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 的源代码并查看示例运行程序。

With xUnit, all you need is to implement ITestRunner interface, i.e.:

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);
}

For implementation details, grab the source code of xUnit and have a look at the sample runners.

青朷 2024-11-22 06:51:13

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

You can use the TestCaseSourceAttribute in NUnit to run dynamic tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文