当来自命令行的 运行 时,mstest v2 忽略了 ExpectedException

ExpectedException ignored by mstest v2 when running from the command line

我正在尝试使用新的 "mstest v2" 框架,但是当 运行ning 从命令行使用 mstest.exe 进行测试时 运行ning 遇到了问题。具体来说,ExpectedExceptionAttribute 和 DeploymentItemAttribute 不起作用。

这是我的样本测试:

[TestMethod]
[ExpectedException(typeof(NotImplementedException))]
public void ExpectedExceptionShouldWork()
{
    throw new NotImplementedException(
       "This exception should not cause the test to fail");
}

我很确定这是从 Microsoft.VisualStudio.TestPlatform.TestFramework 装配而不是来自 Microsoft.VisualStudio.QualityTools.UnitTestFramework - 然后 mstest.exe 无法识别它们。

我们 运行 mstest.exe 在我们的构建服务器 (Teamcity) 中测试失败,即使它们在 IDE 中成功。 Mstest.exe 在使用 运行 在本地使用

时也会失败
"C:\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\MSTest.exe" /testcontainer:bin\Debug\Dummy.Test.dll

当我的项目引用 Microsoft.VisualStudio.QualityTools.UnitTestFramework 时,它在命令行和 IDE.

中工作

如果我随后添加对 MSTest.TestFramework v 1.1.13 的 nuget 引用,我会收到编译错误:

Error   CS0433  
The type 'TestClassAttribute' exists in both 
'Microsoft.VisualStudio.QualityTools.UnitTestFramework, 
Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 
'Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, 
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

然后当我删除对 Microsoft.VisualStudio.QualityTools.UnitTestFramework 的引用时,它会在 IDE 中编译和工作 - 但不是从命令行。测试是 运行,但由于抛出异常而失败。

我们真的很想使用 mstest v2 中的新东西,例如 [DataTestMethod],但我们不能让构建服务器上的构建失败。

有什么解决方法吗? 不只是关于 [ExpectedException]? [AssemblyInitialize] 等其他属性似乎也被忽略了?

请使用 vstest.console.exe 到 运行 在 MSTest v2 中编写的单元测试。

mstest.exe 仅支持 mstest v1 测试。