C# Nunit - 不发现测试 [无错误消息]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29890768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nunit - doesn't discover tests [no error-message]
提问by user1531921
Unit refuses to dsicover or run my tests in an assembly. This is not the case where Unit produces an error message like "Unable to find test in assembly". It simply doesn't discover that I have tests.
Unit 拒绝在程序集中 dsicover 或运行我的测试。这不是 Unit 产生“无法在程序集中找到测试”之类的错误消息的情况。它根本没有发现我有测试。
I right-click the test-method and the test output shows:
我右键单击测试方法,测试输出显示:
Discover test started ------ Discover test finished: 0 found (0:00:00,0260026)"
发现测试开始 ------ 发现测试完成:0 found (0:00:00,0260026)"
I have tried everything mentioned in this post: NUnit doesn't find tests in assembly
我已经尝试了这篇文章中提到的所有内容: NUnit 在程序集中找不到测试
Upgrading is not a possiblity. The processor architecture settings are correct. My tests are public and have all the correct tags.
升级是不可能的。处理器架构设置正确。我的测试是公开的并且有所有正确的标签。
Here's some code from my project that I simply can't get to even execute. I know that because I have a break-point right at the beginning.
这是我项目中的一些代码,我什至无法执行。我知道这一点,因为我在开始时有一个断点。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace UnitTestProjects
{
[TestFixture]
public class SomeRandomTests
{
[Test]
public void ShouldFail()
{
Assert.AreEqual(1, 0);
}
[Test]
public void ShouldPass()
{
Assert.AreEqual(1,1);
}
}
}
I'm running the internal visual studio test explorer. Unit Framework version 2.5.9.10348, runtime version v2.0.50727. I have Unit Test Adopter installed. I've attempted reinstalling it with no success.
我正在运行内部 Visual Studio 测试资源管理器。单元框架版本 2.5.9.10348,运行时版本 v2.0.50727。我安装了单元测试采用者。我尝试重新安装它但没有成功。
采纳答案by Terje Sandstr?m
You must either install the NUnit VSAdapter vsix extension, or add the adapter as nuget package to your solution.
您必须安装 NUnit VSAdapter vsix 扩展,或者将适配器作为 nuget 包添加到您的解决方案中。
The latest version is the 2.0, and the vsix is available here: https://visualstudiogallery.msdn.microsoft.com/6ab922d0-21c0-4f06-ab5f-4ecd1fe7175d
最新版本是 2.0,vsix 可在此处获得:https://visualstudiogallery.msdn.microsoft.com/6ab922d0-21c0-4f06-ab5f-4ecd1fe7175d
And the nuget package can be found here: http://www.nuget.org/packages/NUnitTestAdapter/
nuget 包可以在这里找到:http://www.nuget.org/packages/NUnitTestAdapter/
More information on these options can be found in this MSDN ALM post http://blogs.msdn.com/b/visualstudioalm/archive/2013/06/11/part-3-unit-testing-with-traits-and-code-coverage-in-visual-studio-2012-using-the-tfs-build-and-the-new-nuget-adapter-approach.aspx, which also points to two earlier posts.
有关这些选项的更多信息可以在此 MSDN ALM 帖子http://blogs.msdn.com/b/visualstudioalm/archive/2013/06/11/part-3-unit-testing-with-traits-and-code 中找到-coverage-in-visual-studio-2012-using-the-tfs-build-and-the-new-nuget-adapter-approach.aspx,这也指向了两个较早的帖子。
If you look in the Output console window under Test, the adapter name and version is displayed there as it run. If it doesn't come up, the adapter is not active.
如果您查看“测试”下的“输出控制台”窗口,适配器名称和版本会在其运行时显示在那里。如果它没有出现,则适配器未处于活动状态。
If you run Resharper, ensure you have the latest 8.2 version, there has been conflicts earlier with the test adapters and resharper.
如果您运行 Resharper,请确保您拥有最新的 8.2 版本,之前与测试适配器和 resharper 存在冲突。
Even if you can't upgrade this project from NUnit 2.5.9 to latest 2.6.4, you can verify the adapters work correctly in a test project using 2.6.4.
I just checked on my own machine with NUnit 2.5.9, and that worked nice with the 2.0 adapter.
即使您无法将此项目从 NUnit 2.5.9 升级到最新的 2.6.4,您也可以在使用 2.6.4 的测试项目中验证适配器是否正常工作。
我刚刚用 NUnit 2.5.9 在我自己的机器上检查过,这与 2.0 适配器配合得很好。
Update:
更新:
For VS2017 you dont install the NUnit VSAdapter vsix extension, instead install the NUnit 3 TestAdapter for Visual Studio 2012 (Update 1) onwards. This works with NuGet package: NUnit 3.9.0.
对于 VS2017,您不安装 NUnit VSAdapter vsix 扩展,而是安装适用于 Visual Studio 2012(更新 1)的 NUnit 3 TestAdapter。这适用于 NuGet 包:NUnit 3.9.0。
Update 2 - June 2019Just released the 2.2 version of the NUnit2 Adapter. It should now work properly with SDK type projects, and with VS 2017 and 2019.
更新 2 - 2019 年 6 月刚刚发布了 NUnit2 适配器的 2.2 版本。它现在应该可以与 SDK 类型的项目以及 VS 2017 和 2019 一起正常工作。
回答by jayson.centeno
Mine is just like the same, but the different was I created my project as VS UnitTest project and not a plain DLL. So I created a DLL and referenced all the nunit assemblies and the test are now discovered.
我的和我一样,但不同的是我将我的项目创建为 VS UnitTest 项目而不是普通的 DLL。所以我创建了一个 DLL 并引用了所有的 nunit 程序集,现在发现了测试。
Hope this helps.
希望这可以帮助。
回答by Main
Problem may be with NUnit30Settings.xml. I had this poroblem. In my case file was empty. I don't know what was the reason. I copied settings from NUnitSettings.xml to NUnit30Settings.xml. This solved my problem. Files with settings are in ...Users\[User]\AppData\Local\NUnit.
问题可能出在 NUnit30Settings.xml 上。我有这个问题。在我的情况下,文件是空的。我不知道是什么原因。我将设置从 NUnitSettings.xml 复制到 NUnit30Settings.xml。这解决了我的问题。带有设置的文件位于 ...Users\[User]\AppData\Local\NUnit。
回答by Herb F
I had this same problem when upgrading from Visual Studio 2013 to VS 2017. In my case, all the tests were written for NUnit 2 (not 3) and worked fine in VS 2013. Once VS2017 was installed, none of the tests could be found.
从 Visual Studio 2013 升级到 VS 2017 时,我遇到了同样的问题。就我而言,所有测试都是为 NUnit 2(而不是 3)编写的,并且在 VS 2013 中运行良好。安装 VS2017 后,找不到任何测试.
The problem is that VS2017 did not automagically pick up the NUnit 2 install on my VM.
问题是 VS2017 没有自动在我的 VM 上安装 NUnit 2。
The solution was to install the Nunit 2 test runner by clicking on the Tools menu, Tools | Extensions and Updates. Next select Online on the right-hand list and then enter "NUnit" in the Search box. You can then add the appropriate NUnit test runner.
解决方案是通过单击“工具”菜单“工具”|“工具”来安装 Nunit 2 测试运行器。扩展和更新。接下来选择右侧列表中的 Online,然后在搜索框中输入“NUnit”。然后,您可以添加适当的 NUnit 测试运行程序。
This probably applies to all test runners except perhaps MSTest.
这可能适用于除 MSTest 之外的所有测试运行器。
回答by aleha
In my case i used TestCaseSource attribute. And passed wrong sourceName.
就我而言,我使用了 TestCaseSource 属性。并传递了错误的 sourceName。
[Test]
[TestCaseSource("TestDataCaseSource")]
public void SomeTest1(string DataName, ITestData testData)
After fixing it, all tests appears in Test Explorer
修复后,所有测试都出现在测试资源管理器中
回答by Payam Khaninejad
For my case VS2017, after installation of Unit Test Adopter 3, I just add an updated version of the DLL to my solution using Nuget Package manager.
对于我的情况 VS2017,在安装 Unit Test Adopter 3 之后,我只是使用 Nuget 包管理器将 DLL 的更新版本添加到我的解决方案中。
回答by siddhardh maguluri
I was also facing same issue.My tests are not discovered. After installing NUnit3TestAdapter dll from nuget package manager resolved my issue.my visual studio version is vs2017.
我也面临同样的问题。我的测试没有被发现。从 nuget 包管理器安装 NUnit3TestAdapter dll 后解决了我的问题。我的 Visual Studio 版本是 vs2017。
回答by Vivek
Few things to check here . 1) Make sure we are referencing the right dlls to the test project. In our case it will be NUnit3testAdapter2) We have to restartthe visual studio so in order for the our new install extensions to take effect .
很少有事情要检查这里。1) 确保我们将正确的 dll 引用到测试项目。在我们的例子中,它将是 NUnit3testAdapter2) 我们必须重新启动 Visual Studio,以便我们的新安装扩展生效。
Even after trying all of this and still it doesn't work Install it as part of ToolExtensions and update and from here install the NunitTest adapter. this should definitely work.
即使在尝试了所有这些之后它仍然不起作用将它安装为工具扩展和更新的一部分,然后从这里安装 NunitTest 适配器。这绝对有效。

