简体   繁体   中英

ExpectedException ignored by mstest v2 when running from the command line

I am trying to use the new "mstest v2" framework, but are running into problems when running tests with mstest.exe from the command line. Specifically, ExpectedExceptionAttribute and DeploymentItemAttribute are not working.

Here is my sample test:

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

I am pretty sure it is caused by the attributes being picked up from the Microsoft.VisualStudio.TestPlatform.TestFramework assembly instead of from Microsoft.VisualStudio.QualityTools.UnitTestFramework - then mstest.exe doesn't recognize them.

We run mstest.exe in our build server (Teamcity) and tests fail there, even though they succeed in the IDE. Mstest.exe also fails when running them locally with

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\MSTest.exe" /testcontainer:bin\Debug\Dummy.Test.dll

When my project references Microsoft.VisualStudio.QualityTools.UnitTestFramework, it works on the command line and in the IDE.

If I then add a nuget reference to MSTest.TestFramework v 1.1.13, I get a compile error:

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'

When I then remove the reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework, it compiles and works in the IDE - but not from the command line. The test is run, but fails due to the exception being thrown.

We would really like to use new stuff from mstest v2 such as [DataTestMethod], but we cannot have builds failing on the build server.

Are there any workarounds? It's not just about [ExpectedException]? Other attributes such as [AssemblyInitialize] seems to be ignored as well?

Please use vstest.console.exe to run the unit tests authored in MSTest v2.

mstest.exe only supports the mstest v1 tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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