【发布时间】:2021-09-04 04:41:49
【问题描述】:
多年来,我一直在使用 MSTest 的 [TestMethod] 和类似属性来设置单元测试……可以很好地满足我的需求。
我最近将我的一个类库升级到 .NET Standard 2.0,这要求我将我的项目文件升级到新格式。我同样更新了该库的测试项目文件,以便我可以多目标测试(net462 和 net5.0-windows)。这似乎工作得很好,因为它构建得很好,并且 TestExplorer 窗口像以前一样显示了我的 19 种测试方法,但现在重复了两次,每个目标一次。完美的。因此,我按下“全部运行”,它会将它们全部更改为小时钟图标,就像以前一样,但是……什么都没有……它永远不会运行它们。只需将它们转回“不运行”蓝色即可。在测试初始化的前面放置一个断点表明它们根本不会运行。
这是它给出的文本输出:
========== 开始测试运行==========
[MSTest][Discovery][C:\tcc\Development\Common\Test\CommonTest\bin\Debug\net462\CommonTest.dll] 无法从程序集 C:\tcc\Development\Common\Test\ 中发现测试CommonTest\bin\Debug\net462\CommonTest.dll。原因:无法从程序集“Microsoft.VisualStudio.TestPlatform.TestFramework,版本=14.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a”加载类型“Microsoft.VisualStudio.TestTools.UnitTesting.ExecutionScope”。 没有测试与 C:\tcc\Development\Common\Test\CommonTest\bin\Debug\net462\CommonTest.dll 中给定的测试用例过滤器
FullyQualifiedName=CommonTest.SerializableHashtableTest.TestEmptySerialization|FullyQualifiedName=CommonTest.SerializableHashtableTest.TestNestedSerialization|FullyQualifiedName=CommonTest.SerializableHashtableTest.TestSimpleSerialization|FullyQualifiedNam...匹配 调用执行程序“executor://mstestadapter/v2”时发生异常:无法从程序集“Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture= 加载类型“Microsoft.VisualStudio.TestTools.UnitTesting.ExecutionScope”中立,PublicKeyToken=b03f5f7f11d50a3a'。 堆栈跟踪: 在 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)========== 测试运行完成:0 次测试(0 次通过,0 次失败,0 次跳过)在 46 毫秒内运行 ==========
为此,我添加了 NuGet 包“Microsoft.VisualStudio.TestPlatform”和后来实际上来自 Microsoft 的“Microsoft.TestPlatform”,重新启动 VS,重建并重新运行测试,但仍然没有。我现在安装了这 4 个 NuGet 包(不确定哪些,如果有的话,实际上需要/有帮助):
Microsoft.TestPlatform (16.11.0)(也尝试过 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)
这个旧的 StackOverflow 线程:Visual Studio 15.8.1 not running MS unit tests 建议在测试选项下进行设置,但该设置在 Visual Studio 2019 中不再存在。
我不明白其中的“没有发现任何测试”部分......它清楚地找到了我的所有 19 个 TestMethod,并像往常一样按层次组织显示它们。
还有其他建议吗? (我有数百个这样的单元测试,所以在 NUnit 或 xUnit 等其他测试框架中重写它们并不太吸引人。)
更新 #1:
根据 Zdeněk 的项目文件,我添加了 MSTest.TestFramework 包,它开始运行 net5.0-windows 目标测试;但是对于 net462 目标测试,它仍然是从时钟图标返回到蓝色的“未运行”图标。所以,然后我尝试将它定位为 JUST net462 ......仍然不会运行它。是否需要不同的包来运行具有新型项目文件的 .NET Framework 目标测试?
这是我的项目文件(我尝试过的单目标替代方案已被注释掉):
<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>
更新 #2:
根据 Zdeněk 的要求,dotnet test 似乎可以找到两个目标的测试:
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]
更新 #3:
因此,在获得 MSTest.TestFramework 包后,“测试”下的 VS 输出窗口显示以下异常。我尝试将包 System.Collections.Concurrent 添加到我的 CommonTest 项目中,但这并没有帮助...相同的输出。就像 Visual Studio 开发人员忘记将该包添加到他们的构建中一样。这是带有异常和调用堆栈的测试输出:
========== 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 ==========
注意:在 net462 和 net5.0-windows 下,通过 'dotnet test' 运行这些测试工作正常...全部运行...全部通过...。
【问题讨论】:
-
能否请您指定测试 NuGet 的版本?
-
我要做的是创建一个新的 MSTest 项目,最好以 4.6.2 为目标,看看它是否有效。如果是,请尝试添加 .net 5。如果仍然有效,请检查与您的实际测试项目的差异。如果它在沿路的某个地方不起作用,那么您的复制量就很小。从您所面临的复杂、不可重现的场景中很难猜出问题所在。
-
添加了版本号。尝试它而不是多目标是一个好主意......只是用net462尝试过......同样的行为(但只有19,而不是2 * 19)。似乎有一些默认过滤可以过滤掉所有内容?!?看起来很愚蠢,但是很多新东西似乎都是如此。谢谢。
-
还尝试使用 VS 2022 Preview 3... 相同的行为。 --sigh-- MS Test 的好处是它按原样工作,内置在默认的 VS 中。
-
如果你运行
dotnet test,它会运行测试,包括net462吗?如果我没记错的话,它应该显示一个摘要。或者应该有一些标志来做,检查文档。此外,您还发布了某种测试的文本输出。它提到无法加载现在应该修复的类型,因为您修复了项目中的包引用,所以我想知道该输出是否因此而改变。
标签: c# .net visual-studio unit-testing mstest