如何从我的代码运行NUnit [英] How to run NUnit from my code

查看:59
本文介绍了如何从我的代码运行NUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用NUnit在我的插件中运行单元测试,但是它需要在我的应用程序上下文中运行。为了解决这个问题,我试图开发一个运行NUnit的插件,该插件又将在应用程序的上下文中执行我的测试。

I'd like to use NUnit to run unit tests in my plug-in, but it needs to be run in the context of my application. To solve this, I was trying to develop a plug-in that runs NUnit, which in turn will execute my tests in the application's context.

我没有找到一个有关此主题的特定文档,因此我在这里和那里挖掘了一段信息,然后得出了以下代码(与我在StackOverflow中找到的代码类似):

I didn't find a specific documentation on this subject so I dug a piece of information here and there and I came out with the following piece of code (which is similar to one I found here in StackOverflow):

    public static void Main()
    {
        SimpleTestRunner runner = new SimpleTestRunner();
        TestPackage package = new TestPackage( "Test" );
        string loc = Assembly.GetExecutingAssembly().Location
        package.Assemblies.Add( loc );
        if( runner.Load(package) )
        {
            TestResult result = runner.Run( new NullListener() );
        }
    }

结果变量说没有TestFixture,尽管我肯定知道它在那里。实际上,我的测试文件包含两个测试。

The result variable says "has no TestFixture" although I know for sure it is there. In fact my test file contains two test.

使用我发现的另一种方法,该方法由以下代码总结:

Using another approach I found, which is summarized by the following code:

TestSuiteBuilder builder = new TestSuiteBuilder();
TestSuite testSuite = builder.Build( package );

// Run tests
TestResult result = testSuite.Run( new NullListener(), NUnit.Core.TestFilter.Empty );

我看到只有1个测试的nunit数据结构,并且有相同的错误。

I saw nunit data structures with only 1 test and I had the same error.

为了完整起见,我使用的是nunit的最新版本,即2.5.5.10112。

For sake of completeness, I am using the latest version of nunit, which is 2.5.5.10112.

有人知道我的意思吗?失踪了吗?
一个示例代码将不胜感激。
我的测试课程是:

Does anyone know what I'm missing? A sample code would be appreciated. My test class is:

[TestFixture]
public class UnitTests
{
    public UnitTests()
    {
    }

    [Test]
    public void TestEqual()
    {
        long a = 10;
        long b = 10;
        Assert.AreEqual( a, b );
    }

    [Test]
    public void TestNotEqual()
    {
        long a = 10;
        long b = 11;
        Assert.AreNotEqual( a, b );
    }
}


推荐答案

下面的代码有助于解决问题

The below code help to resolve the issue

public static void Main()
{
    CoreExtensions.Host.InitializeService();
    SimpleTestRunner runner = new SimpleTestRunner();
    TestPackage package = new TestPackage( "Test" );
    string loc = Assembly.GetExecutingAssembly().Location;
    package.Assemblies.Add( loc );
    if( runner.Load(package) )
    {
        TestResult result = runner.Run( new NullListener(),
            TestFilter.Empty, false, LoggingThreshold.Off );
    }
}

这篇关于如何从我的代码运行NUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆