简体   繁体   中英

C# Is it possible to run nunit tests inside ASP.Net?

I have a very simple question.

Is it possible to run Nunit tests inside an asp.net web app?

Here is an example project:

  • MyApp -> ASP.Net app (.net5)
  • MyTests -> Nunit Tests (.net5)

My Asp.net project ( MYApp ) contains all my controllers and such, with a depency on NUnit.Engine and my test project. There is another Test project ( MyTests ), which is just a dummy project.

I want to be able to run in a controller, inside my web app, my tests.

Example controller:

namespace MyApp.Controllers
{
    [Route("api/tests")]
    [ApiController]
    public class TestController: ControllerBase
    {
        // Some helper class to verify everything is working somehow
        private class ReportListener : ITestEventListener
        {
            public void OnTestEvent(string report)
            {
                Console.WriteLine(report);
            }
        }

        [HttpGet]
        public async Task<ActionResult> Trigger()
        {
            try
            {
                using ITestEngine engine = TestEngineActivator.CreateInstance();
                engine.Initialize();
                engine.WorkDirectory = Path.Combine(Directory.GetCurrentDirectory(), "../","MyTests/");
                // Create a simple test package - one assembly, no special settings
                TestPackage package = new TestPackage(@".\bin\Debug\net5.0\MyTests.dll"); //Just for debugging and testing
                
                // Get a runner for the test package
                using ITestRunner runner = engine.GetRunner(package);
                runner.Load();
                
                // Run all the tests in the assembly
                XmlNode testResult = runner.Run(listener: new ReportListener(), TestFilter.Empty);
                var outputs = Enumerable.Empty<string>();
                foreach (XmlNode elem in testResult.SelectNodes("//test-case/output"))
                {
                    outputs = outputs.Append(elem.InnerText);
                }
            }catch(Exception e)
            {

            }
            return Ok();
        }
        
    }
}

But unfortunately all my attemps so far have failed.

Am I missing something? Is Nunit.Engine not made to be run in an asp.net context?

I am building all this in .NET5.0 (company policy)

If needed I can provide an example project

There could be more, but one small thing would explain the failure...

The NUnit engine defaults to running tests in a separate process, which it launches. So, assuming that your code is working correctly as written, a ProcessRunner will be created and the engine will communicate with it, telling it to run your tests.

This could fail in one of two ways:

  1. You may not have permission to create a process.

  2. If you succeed in creating it, the code will definitely not be running in the asp.net context. In that case, it would probably error out and terminate with very little debug information provided.

A simple fix is to add a setting to the test package, telling the engine to run the tests in process.

TestPackage package = new TestPackage(@".\bin\Debug\net5.0\MyTests.dll");
package.AddSetting("ProcessModel", "InProcess");

If you get a second error after doing this, it should at least result in a clearer message and you should be able to debug through the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM