I have been using MSTest's [TestMethod] and similar attributes to setup unit tests for many, many years... work just fine for my needs.
I recently upgraded one of my class libs to .NET Standard 2.0, which required me to upgrade my project files to the new format. And I similarly updated my test project file for that library so that I could multi-target the tests (both net462 and net5.0-windows). That seemed to work great as it builds just fine and the TestExplorer window shows my 19 Test Methods just as it used to, but now repeated twice, once for each target. Perfect. So, I press "Run All" and it changes them all to little clock icons just like it used to, but then... nothing... it never runs them. Just turns them back to "not run" blue. Putting a breakpoint at the front of the tests' initialization show they never run at all.
Here's the text output it gives:
========== Starting test run ==========
[MSTest][Discovery][C:\tcc\Development\Common\Test\CommonTest\bin\Debug\net462\CommonTest.dll] Failed to discover tests from assembly C:\tcc\Development\Common\Test\CommonTest\bin\Debug\net462\CommonTest.dll. Reason:Could not load type 'Microsoft.VisualStudio.TestTools.UnitTesting.ExecutionScope' from assembly 'Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. No test matches the given testcase filter
FullyQualifiedName=CommonTest.SerializableHashtableTest.TestEmptySerialization|FullyQualifiedName=CommonTest.SerializableHashtableTest.TestNestedSerialization|FullyQualifiedName=CommonTest.SerializableHashtableTest.TestSimpleSerialization|FullyQualifiedNam...
in C:\tcc\Development\Common\Test\CommonTest\bin\Debug\net462\CommonTest.dll An exception occurred while invoking executor 'executor://mstestadapter/v2': Could not load type 'Microsoft.VisualStudio.TestTools.UnitTesting.ExecutionScope' from assembly 'Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Stack trace: at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.MSTestExecutor.RunTests(IEnumerable1 sources, IRunContext runContext, IFrameworkHandle frameworkHandle) at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.RunTestsWithSources.InvokeExecutor(LazyExtension
2 executor, Tuple2 executorUriExtensionTuple, RunContext runContext, IFrameworkHandle frameworkHandle) at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.BaseRunTests.RunTestInternalWithExecutors(IEnumerable
1 executorUriExtensionMap, Int64 totalTests)========== Test run finished: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in 46 ms ==========
In response to that, I added NuGet package "Microsoft.VisualStudio.TestPlatform" and later "Microsoft.TestPlatform" that is actually from Microsoft, restarted VS, rebuilt, and re-ran the tests, but still nothing. I have these 4 NuGet packages installed at this point (not sure which, if any, were actually needed/helpful):
Microsoft.TestPlatform (16.11.0) (also tried Microsoft.VisualStudio.TestPlatform)
Microsoft.NET.Test.Sdk (16.11.0)
Microsoft.UnitTestFramework.Extensions (2.0.0)
MSTest.TestAdapter (2.2.7)
Visual Studio 2019 (16.11.2)
This old StackOverflow thread: Visual Studio 15.8.1 not running MS unit tests suggested a setting under the Test Options, but that setting is no longer a thing in Visual Studio 2019.
I don't understand the "didn't discover any tests" part of that... it clearly found all 19 of my TestMethod's, and displays them hierarchically organized just like it always did.
Any other suggestions? (I have hundreds of these unit tests, so rewriting them in some other test framework like NUnit or xUnit is not too appealing.)
UPDATE #1:
Per Zdeněk's project file, I added MSTest.TestFramework package and it started running the net5.0-windows targeted tests; but for the net462 targeted tests, it is still doing the return from clock icon to blue "Not Run" icon. So, then I tried targeting it for JUST net462... still won't run it. Is there a different package needed to run .NET Framework targeted tests with new-style project files?
Here's my Project file (with the single-targeted alternatives that I've tried commented out):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net462;net5.0-windows</TargetFrameworks>
<!-- <TargetFramework>net462</TargetFramework> -->
<!-- <TargetFramework>net5.0-windows</TargetFramework> -->
<UseWindowsForms>true</UseWindowsForms>
<Title>CommonTest</Title>
<Description>Unit Test code for Tcc.Common library.</Description>
<Product>CommonTest</Product>
<Company>Targeted Convergence Corporation</Company>
<Copyright>Copyright © Targeted Convergence Corporation 2005-2021</Copyright>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<InformationalVersion>1.0.0.0</InformationalVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.12" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Source\tcc.common\Tcc.Common.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Image\huge.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Image\logo.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Image\logo.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Image\pano.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Image\small.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
UPDATE #2:
As requested by Zdeněk, dotnet test appears to find the tests for both targets:
C:\tcc\Development\Common\Test\CommonTest>dotnet test --list-tests
Determining projects to restore...
Restored C:\tcc\Development\Common\Source\tcc.common\Tcc.Common.csproj (in 438 ms).
Restored C:\tcc\Development\Common\Test\CommonTest\CommonTest.csproj (in 672 ms).
You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
Tcc.Common -> C:\tcc\Development\Common\Source\tcc.common\bin\Debug\netstandard2.0\Tcc.Common.dll
CommonTest -> C:\tcc\Development\Common\Test\CommonTest\bin\Debug\net5.0-windows\CommonTest.dll
CommonTest -> C:\tcc\Development\Common\Test\CommonTest\bin\Debug\net462\CommonTest.dll
Test run for C:\tcc\Development\Common\Test\CommonTest\bin\Debug\net462\CommonTest.dll (.NETFramework,Version=v4.6.2)
Microsoft (R) Test Execution Command Line Tool Version 17.0.0-preview-20210715-01
Copyright (c) Microsoft Corporation. All rights reserved.
The following Tests are available:
TestGenSmallEmfFile
TestComposeMetafiles
TestImageFormatSizes
TestMetafileContainingBitmap
TestImageFromStream
TestEmptySerialization
TestSimpleSerialization
TestNestedSerialization
TestSpeeds
TestIPAddresses
Test run for C:\tcc\Development\Common\Test\CommonTest\bin\Debug\net5.0-windows\CommonTest.dll (.NETCoreApp,Version=v5.0)
Microsoft (R) Test Execution Command Line Tool Version 17.0.0-preview-20210715-01
Copyright (c) Microsoft Corporation. All rights reserved.
The following Tests are available:
TestGenSmallEmfFile
TestComposeMetafiles
TestImageFormatSizes
TestMetafileContainingBitmap
TestImageFromStream
TestEmptySerialization
TestSimpleSerialization
TestNestedSerialization
TestSpeeds
TestIPAddresses
C:\tcc\Development\Common\Test\CommonTest>dotnet --version
6.0.100-preview.7.21379.14
C:\tcc\Development\Common\Test\CommonTest>dotnet --list-sdks
2.1.402 [C:\Program Files\dotnet\sdk]
2.1.526 [C:\Program Files\dotnet\sdk]
5.0.400 [C:\Program Files\dotnet\sdk]
6.0.100-preview.7.21379.14 [C:\Program Files\dotnet\sdk]
UPDATE #3:
So, after getting the MSTest.TestFramework package in there, the VS Output window under "Tests" shows the following Exception. I tried adding package System.Collections.Concurrent to my CommonTest project, but that did not help... same output. It is like the Visual Studio developers forgot to add that package to their build. Here's the Tests output with the exception and call stack:
========== Starting test discovery ==========
========== Test discovery finished: 20 Tests found in 2.1 sec ==========
========== Starting test run ==========
An exception occurred while invoking executor 'executor://mstestadapter/v2': Exception has been thrown by the target of an invocation.
Stack trace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstanceFromInternal(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)
at System.AppDomain.CreateInstanceFrom(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities.AppDomainUtilities.CreateInstance(AppDomain appDomain, Type type, Object[] arguments)
at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestSourceHost.CreateInstanceForType(Type type, Object[] args)
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestExecutionManager.ExecuteTestsInSource(IEnumerable`1 tests, IRunContext runContext, IFrameworkHandle frameworkHandle, String source, Boolean isDeploymentDone)
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestExecutionManager.ExecuteTests(IEnumerable`1 tests, IRunContext runContext, IFrameworkHandle frameworkHandle, Boolean isDeploymentDone)
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestExecutionManager.RunTests(IEnumerable`1 tests, IRunContext runContext, IFrameworkHandle frameworkHandle, TestRunCancellationToken runCancellationToken)
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.MSTestExecutor.RunTests(IEnumerable`1 tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.RunTestsWithTests.InvokeExecutor(LazyExtension`2 executor, Tuple`2 executorUri, RunContext runContext, IFrameworkHandle frameworkHandle)
at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.BaseRunTests.<>c__DisplayClass48_0.<RunTestInternalWithExecutors>b__0()
at Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.PlatformThread.<>c__DisplayClass0_0.<Run>b__0()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.PlatformThread.Run(Action action, PlatformApartmentState apartmentState, Boolean waitForCompletion)
at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.BaseRunTests.TryToRunInSTAThread(Action action, Boolean waitForCompletion)
at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.BaseRunTests.RunTestInternalWithExecutors(IEnumerable`1 executorUriExtensionMap, Int64 totalTests)
Inner exception: Could not load file or assembly 'System.Collections.Concurrent, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Stack trace:
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TypeCache..ctor(ReflectHelper reflectionHelper)
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.UnitTestRunner..ctor(MSTestSettings settings)
========== Test run finished: 10 Tests (10 Passed, 0 Failed, 0 Skipped) run in 43.8 sec ==========
========== Starting test run ==========
An exception occurred while invoking executor 'executor://mstestadapter/v2': Exception has been thrown by the target of an invocation.
Stack trace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstanceFromInternal(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)
at System.AppDomain.CreateInstanceFrom(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities.AppDomainUtilities.CreateInstance(AppDomain appDomain, Type type, Object[] arguments)
at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestSourceHost.CreateInstanceForType(Type type, Object[] args)
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestExecutionManager.ExecuteTestsInSource(IEnumerable`1 tests, IRunContext runContext, IFrameworkHandle frameworkHandle, String source, Boolean isDeploymentDone)
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestExecutionManager.ExecuteTests(IEnumerable`1 tests, IRunContext runContext, IFrameworkHandle frameworkHandle, Boolean isDeploymentDone)
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestExecutionManager.RunTests(IEnumerable`1 tests, IRunContext runContext, IFrameworkHandle frameworkHandle, TestRunCancellationToken runCancellationToken)
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.MSTestExecutor.RunTests(IEnumerable`1 tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.RunTestsWithTests.InvokeExecutor(LazyExtension`2 executor, Tuple`2 executorUri, RunContext runContext, IFrameworkHandle frameworkHandle)
at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.BaseRunTests.<>c__DisplayClass48_0.<RunTestInternalWithExecutors>b__0()
at Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.PlatformThread.<>c__DisplayClass0_0.<Run>b__0()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.PlatformThread.Run(Action action, PlatformApartmentState apartmentState, Boolean waitForCompletion)
at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.BaseRunTests.TryToRunInSTAThread(Action action, Boolean waitForCompletion)
at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.BaseRunTests.RunTestInternalWithExecutors(IEnumerable`1 executorUriExtensionMap, Int64 totalTests)
Inner exception: Could not load file or assembly 'System.Collections.Concurrent, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Stack trace:
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TypeCache..ctor(ReflectHelper reflectionHelper)
at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.UnitTestRunner..ctor(MSTestSettings settings)
========== Test run finished: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in 2.8 sec ==========
NOTE: Running these tests via 'dotnet test' works fine... all run... all pass... under both net462 and net5.0-windows.