简体   繁体   English

NUnit没有为我的单元测试项目发布.dll

[英]NUnit is not releasing the .dll for my unit test project

I have the NUnit GUI runner to run tests on my unit test assembly every time that it is built. 我有NUnit GUI运行器,每次构建时都在我的单元测试程序集上运行测试。 The problem is that when I try to build, the .dll in the Debug folder is in use by NUnit, which prevents it from being built, which prevents the automatic test run. 问题是,当我尝试构建时,NUnit正在使用Debug文件夹中的.dll,这阻止了它的构建,这阻止了自动测试运行。 Is there a way around this? 有没有解决的办法?

Whenever you are building your solution. 每当您构建解决方案时。 I suggest you to close the Nunit, which is loaded with your .dll. 我建议你关闭装有你的.dll的Nunit。 After unloading try to build, then your build will succeed. 在卸载后尝试构建,然后您的构建将成功。

As Nunit is using you .dll. 当Nunit正在使用你.dll。 It won't allow your build to be success. 它不会让你的构建成功。

I had a similar problem a while ago and my final solution was to resign from using NUnit GUI runner and just run them directly from code. 我刚才有类似的问题,我的最终解决方案是从使用NUnit GUI运行器辞职,直接从代码运行它们。

In my project I referenced to NUnit libraries needed by those freshly built dlls plus nunit.core and nunit.utils. 在我的项目中,我引用了那些新构建的dll以及nunit.core和nunit.utils所需的NUnit库。 The code itself is very simple: 代码本身很简单:

TestResult ExecuteTests(string testAssemblyPath) {
    CoreExtensions.Host.InitializeService();
    TestPackage testPackage = new TestPackage(testAssemblyPath);
    testPackage.BasePath = Path.GetDirectoryName(testAssemblyPath);

    RemoteTestRunner testRunner = new RemoteTestRunner();
    testRunner.Load(testPackage);

    TestResult testResult = testRunner.Run(new NullListener(), TestFilter.Empty, true, LoggingThreshold.Warn);

    testRunner.Unload();
    CoreExtensions.Host.UnloadService();

    return testResult;
}

The TestResult object is very powerful. TestResult对象非常强大。 Among other things, it contains all the results, subresults, tests themselves etc. In order to analyze them, you can either create a simple parser or use one of the possibilities given by NUnit libraries. 除其他外,它包含所有结果,子结果,测试本身等。为了分析它们,您可以创建一个简单的解析器或使用NUnit库给出的一种可能性。 My favorite is XmlResultWriter but there are others available as well. 我最喜欢的是XmlResultWriter但也有其他人可用。 All of them can be found in nunit.util.dll. 所有这些都可以在nunit.util.dll中找到。

Unfortunately, that would still block the loaded dll from being rebuilt. 不幸的是,这仍然会阻止加载的dll被重建。 I avoided this problem by running this in a separate AppDomain and unloading this domain after the tests are finished running. 我通过在单独的AppDomain中运行它并在测试完成运行后卸载此域来避免此问题。 Then dll is nicely freed and you can do with it whatever you want. 然后dll很好地释放了,无论你想要什么,你都可以使用它。

Can I suggest a slightly different approach but along the same lines as @UvarajGopu.... 我可以建议一个略有不同的方法,但与@UvarajGopu一样....

As long as you are using Visual Studio higher than "Express" version, and if you write your UnitTests in a separate project (typically alongside the project being tested, with a ".UnitTests" suffix), then do this: 只要您使用的Visual Studio高于“Express”版本,并且您将UnitTests编写在单独的项目中(通常与正在测试的项目一起使用“.UnitTests”后缀),请执行以下操作:

  • Set your UnitTests project as being the StartUp project (right-click in Solution Explorer, "Set as Startup Project") 将UnitTests项目设置为StartUp项目(右键单击Solution Explorer,“Set as Startup Project”)
  • In the Project Properties for that project, in the "Debug" tab, choose "Start External Program", and choose the NUnit GUI executable. 在该项目的项目属性中,在“调试”选项卡中,选择“启动外部程序”,然后选择NUnit GUI可执行文件。 Put the name of UnitTests assembly in "Command line arguments". 将UnitTests程序集的名称放在“命令行参数”中。

Now you can simply press F5 (to start debugging) which will build your projects, and launch the NUnit GUI for you. 现在您只需按F5(开始调试)即可构建项目,并为您启动NUnit GUI。 This has the added advantage that if your tests fail, you can add breakpoints, and step through using the debugger (without needing to attach the debugger manually). 这具有额外的优势,如果您的测试失败,您可以添加断点,并逐步使用调试器(无需手动附加调试器)。

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

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