0

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(LazyExtension2 executor, Tuple2 executorUriExtensionTuple, RunContext runContext, IFrameworkHandle frameworkHandle) at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.BaseRunTests.RunTestInternalWithExecutors(IEnumerable1 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.

10
  • Could you please also specify the versions of the testing NuGets? Commented Sep 4, 2021 at 6:09
  • 2
    What I'd do is create a new MSTest project, ideally 4.6.2-targeted and see if it works. If it does, try to add .net 5. If it still works, check out the differences with your actual testing project. If it does not work somewhere along the road, you have a minimal repro. It is quite hard to guess the issue from a complicated, non-reproducible scenario that you are facing. Commented Sep 4, 2021 at 6:23
  • Version numbers added. Trying it not multi-targeted is a good idea... just tried it with only net462... same behavior (but with just the 19, rather than 2*19). Seems there's some default filtering that filters away everything?!? Seems stupid, but then a lot of this new stuff seems so. Thanks. Commented Sep 4, 2021 at 7:46
  • Also tried it with VS 2022 Preview 3... same behavior. --sigh-- The nice thing about MS Test was it just worked as-is, built into the default VS. Commented Sep 4, 2021 at 13:03
  • 1
    Well if you run dotnet test does it run the tests, including net462? If I'm not mistaken it should show a summary. Or there should be some flag to do it, check the docs. Also, you have posted text output of tests of some sort. It mentions not being able to load types which should now be repaired as you fixed the package references in your project, so I was wondering if that output changed as a result. Commented Sep 6, 2021 at 19:42

2 Answers 2

2

I have just created a .NET Core 3.1 MS Test project, changed it to .NET Framework 4.6.1, fought for a while with VS which I attribute to using VS 2022 Preview and ReSharper, added .NET 5.0 and it works.

csproj:

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net461;net5-windows</TargetFrameworks> <IsPackable>false</IsPackable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" /> <PackageReference Include="MSTest.TestAdapter" Version="2.2.3" /> <PackageReference Include="MSTest.TestFramework" Version="2.2.3" /> <PackageReference Include="coverlet.collector" Version="3.0.2" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Dummy.Netstandard20.Library\Dummy.Netstandard20.Library.csproj" /> </ItemGroup> </Project> 

Unit tests:

using Dummy.Netstandard20.Library; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTestProject2 { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { new Class1(); // Comes from Dummy.Netstandard20.Library } } } 

If this does not work for you, it would be great if you opened a new question and detail exactly what your minimal reproducible csproj, unit test and output log look like. Also your .NET CLI and SDK versions would be great (dotnet --version, dotnet --list-sdks).

There could also be issues with discovery. Nobody can tell unless you provide something reproducible.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for that... it allowed me to see that I needed to add package MSTest.TestFramework instead of Microsoft.TestPlatform and Microsoft.UnitTestFramework.Extensions that others have recommended. That got it running all the tests under the net5.0-windows target... but the tests under the net462 target still go to a clock and then just back to blue "Not Run" symbol. Any ideas on how to fix that? (Yes, I'll try whittling this down to something I can post; but that won't be easy... 10 years of unit tests here.)
NOTE: I tried targeting just net462... same "Not Run" result... so, it is not the multi-targeting, but rather trying to target .NET Framework from a new-style project file? I added my .csproj project file to my post above, in case you can see some mistake I am making.
That would be symptomatic of test doscovery issue. Do your tests discover when you use dotnet test --list-tests?
Yes, the tests are discovered for both targets... see the output of that and the other dotnet commands you mentioned in UPDATE #2 above.
P.S. I have VS 2017, VS 2019, and VS 2022 preview 3 all installed. I primarily use VS 2019 for production code. I am experimenting with VS 2022... in particular when trying to convert things to .NET 5 and soon .NET 6.
1

I just ran into the same issue, with the same System.Collections.Concurrent errror in the Test logs, and solved it by changing the test project's target framework from net462 to net48. I know .NET Framework 4.7.x had quite a few fixes to how binding redirects work, so that's probably the explanation.

1 Comment

net471 also works, I just confirmed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.