Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

c# - How to run NUnit from my code

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.

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

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

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

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 );
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...