简体   繁体   English

C# 是否可以在 ASP.Net 中运行 nunit 测试?

[英]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?是否可以在 asp.net web 应用程序中运行 Nunit 测试?

Here is an example project:这是一个示例项目:

  • MyApp -> ASP.Net app (.net5) MyApp -> ASP.Net 应用程序 (.net5)
  • MyTests -> Nunit Tests (.net5) MyTests -> Nunit 测试 (.net5)

My Asp.net project ( MYApp ) contains all my controllers and such, with a depency on NUnit.Engine and my test project.我的 Asp.net 项目( MYApp )包含我所有的控制器等,依赖于NUnit.Engine和我的测试项目。 There is another Test project ( MyTests ), which is just a dummy project.还有另一个测试项目( MyTests ),它只是一个虚拟项目。

I want to be able to run in a controller, inside my web app, my tests.我希望能够在 controller 中运行,在我的 web 应用程序中,我的测试。

Example controller:示例 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? Nunit.Engine 不是在 asp.net 上下文中运行吗?

I am building all this in .NET5.0 (company policy)我在 .NET5.0 中构建所有这些(公司政策)

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. NUnit 引擎默认在它启动的单独进程中运行测试。 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.因此,假设您的代码按照编写的方式正常工作,将创建一个 ProcessRunner 并且引擎将与其通信,告诉它运行您的测试。

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.如果创建成功,代码肯定不会在 asp.net 上下文中运行。 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.一个简单的修复方法是向测试 package 添加一个设置,告诉引擎运行正在进行的测试。

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.如果您在执行此操作后遇到第二个错误,它至少应该会产生更清晰的消息,并且您应该能够通过代码进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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