0
votes

I am trying to execute Nunit test cases writen in c# using a console program. So far i tried different ways to do it but not able to find a perfect way of approach to achieve the same.

Approach 1: Using NUnitTestAdapter package. This package is older and not able to run the test which are created using the latest nunit version 3.2.1. But its working fine with older versions like 2.6.4

Added NUnitTestAdapter package to the solution Then tried the below code

CoreExtensions.Host.InitializeService();
        TestSuiteBuilder builder = new TestSuiteBuilder();
        TestPackage testPackage = new TestPackage(@"path to dll");
        RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
        remoteTestRunner.Load(testPackage);
        TestSuite suite = builder.Build(testPackage);
        TestSuite test = suite.Tests[0] as TestSuite;
        var numberOfTests = ((TestFixture)test.Tests[0]).TestCount;
        int i = 0;
        foreach (TestMethod t in ((TestFixture)test.Tests[0]).Tests)
        {
            Console.WriteLine(t.TestName.Name);
            //TestName testName = ((TestMethod)((TestFixture)test.Tests[0]).Tests[i]).TestName;
            TestFilter filter = new NameFilter(t.TestName);
            TestResult result = test.Run(new NullListener(), filter);
            ResultSummarizer summ = new ResultSummarizer(result);
            NUnit.Core.NUnitFramework.Assert.AreEqual(1, summ.ResultCount);
            i++;
        }

Can i get any updated version of this then this will a fine solution.

Approach 2: Using Nunit3-console

Added the reference to nunit3.console.exe to my project and tried with below code

string path = @"path to dll";
NUnit.ConsoleRunner.Program.Main(new[] { path });

This is throwing an error saying "Either assembly contains no tests or proper test driver has not been found"

Can anybody let me know how can we run the tests with execution log?

1

1 Answers

1
votes

This isn't how NUnit is intended to work.

In the first example, you are using internal classes and methods of NUnit 2.x, which are not documented for general use. They happen to be declared as public because that was our standard practice in the past for all methods that are called by our own tests. Also, your use of the NUnit test adapter makes no sense, as that package is for use in connecting NUnit to the Visual Studio test window. I suspect it is working for you because it pulls in the nunit.core assembly, which is what you are actually using.

In the second example, you call the main directly. You already posted an nunit issue about this not working, so I won't go into detail. I'll just say that running a standalone program by referencing it and calling it's main only works in the simplest of cases. For a program like the console runner, which loads various assemblies dynamically, it won't necessarily work.

NUnit 3 provides a published API for running tests. This is something that folks had asked us to do for many years. It's finally there! Why not use it? Check the nunit 3 docs for info about the Test Engine and it's API. If you want to run tests programatically for some reason, that's the way to go these days.

Of course, you are free to keep trying to make the approach you have taken work and I'll keep commenting as needed. I just want to make it clear for folks who find this post in the future that this is not how NUnit is designed to be used.