我目前正在开发一个有32个单元测试的解决方案。我一直在使用resharper测试运行器-它工作得很好。所有测试都在运行,所有测试都显示正确的测试结果。

但是,在使用Visual Studio测试资源管理器时,测试未运行。

测试资源管理器正在显示所有单元测试,但一旦单击“全部运行”,所有测试都将变成灰色,并且不显示测试运行的结果:

所有测试类都是公共的 所有测试类都声明了[TestClass]属性 所有测试方法都使用[TestMethod]属性 生产力代码和测试项目都是针对。net 3.5的。 我已经尝试清洁构建我的解决方案,和/或删除所有obj, bin,调试和发布文件夹

我很感激任何能解释这种行为的提示。


当前回答

在这篇文章之后,我发现我必须添加一个引用并删除一个包。 我用的是vs 2022,但2019年也一样。发现此页有信息,以解决我的问题。我发现有两种方法可以解决。

我有框架4.8和参考微软。visualstudio . qualitytools . unittestframework

类[TestClass]和[TestMethod]是可见的,因为安装了一个包,但添加这些属性类定义在包和添加的UnitTestFramework引用。删除包并显示它可以运行和调试测试。

这是通过添加两个包来实现的。你可以添加一个包含第二个的包。

包Id是: Microsoft.UnitTestFramework.Extensions MSTest。TestFramework

我相信两个包添加比一个参考汇编更好:

C:\Program Files\Microsoft VisualStudio \2022\Professional\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll

其他回答

对我来说,问题是我的ClassInit()方法没有正确的签名。

MSTest类初始化和清理属性

特别要注意ClassInit()方法上的[ClassInitialize]属性。

工作原理:

[ClassInitialize]
public static void ClassInit(TestContext context)
{

}

没有工作:

[ClassInitialize]
public void ClassInit(TestContext context)
{

}

or

[ClassInitialize]
public static void ClassInit()
{

}

or

[ClassInitialize]
public void ClassInit()
{

}

检查你的项目文件中不同版本的NUnit的引用:

在我的例子中,我已经安装了NUnit和NUnit3TestAdapter的3.11.0版本,但是在项目文件中有对2.6.4版本的旧引用,这些引用没有随着新的安装被删除。

解决方案(建议修复引用问题,请参阅文档): 重新安装NUnit和NUnit3TestAdapter,这修复了我的项目中的引用。

PM> Update-Package NUnit -reinstall
...
PM> Update-Package NUnit3TestAdapter -reinstall

解决方案2(如果重新安装没有修复引用): 卸载并安装NUnit和NUnit3TestAdapter。

PM> Uninstall-Package NUnit
...
PM> Uninstall-Package NUnit3TestAdapter
...
PM> Install-Package NUnit
...
PM> Install-Package NUnit3TestAdapter

我有不同版本的NUnit(3.11.0)和NunitTestAdapter (3.12.0) nuget包。当我将NUnit更新到3.12.0时,Visual Studio运行了测试。

I found that in the project it was not referencing the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly. Instead, it was referencing Microsoft.VisualStudio.TestPlatform.TestFramework and Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions. When I removed those two references and added the reference to the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly the tests that were previously marked with the blue exclamation point suddenly became active and started working.

我解决的问题与这里的任何解决方案都完全不同:

我的每个测试都调用了一个函数,这个函数反过来调用自己,这意味着它不会终止:

public GoogleIntent CreateIntent(BotPath botpath)
{
    return CreateIntent(botpath);
}