单元测试失败,构建服务器出现“Could not load type 'Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior'”错误。

14

我有一个汇编程序,在我的电脑上可以构建和单元测试,但在构建服务器上测试时失败了。大约一个月以来一直失败。我很可能做错了什么。以下是我从构建服务器收到的响应。

16:29:38     Project "xyz.Utilities.UnitTests.csproj": VSTest
16:29:40     Test run for C:\Bin\Release\net5.0-windows\xyz.Utilities.UnitTests.dll (.NETCoreApp,Version=v5.0)
16:29:40     Microsoft (R) Test Execution Command Line Tool Version 16.11.0
16:29:40     Copyright (c) Microsoft Corporation.  All rights reserved.
16:29:40     
16:29:40     Starting test execution, please wait...
16:29:40     A total of 1 test files matched the specified pattern.
16:29:41     An exception occurred while invoking executor 'executor://mstestadapter/v2': Could not load type 'Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior' from assembly 'Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

16:29:41     Stack trace:
16:29:41        at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.MSTestExecutor.RunTests(IEnumerable`1 sources, IRunContext runContext, IFrameworkHandle frameworkHandle)
16:29:41        at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.RunTestsWithSources.InvokeExecutor(LazyExtension`2 executor, Tuple`2 executorUriExtensionTuple, RunContext runContext, IFrameworkHandle frameworkHandle)
16:29:41        at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Execution.BaseRunTests.RunTestInternalWithExecutors(IEnumerable`1 executorUriExtensionMap, Int64 totalTests)
16:29:41     
16:29:41     
16:29:41     Test Run Failed.
16:29:41     
16:29:41     Build FAILED.
16:29:41         0 Warning(s)
16:29:41         0 Error(s)

我的项目文件非常简单:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net5.0-windows</TargetFramework>
        <IsPackable>false</IsPackable>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
        <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
        <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
        <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
        <OutputPath>$(SolutionDir)\Bin\$(Configuration)</OutputPath>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
        <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
        <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
        <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
        <OutputPath>$(SolutionDir)\Bin\$(Configuration)</OutputPath>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="EasyTcp" Version="4.0.0" />
        <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="coverlet.collector" Version="3.1.0">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
    </ItemGroup>
    <ItemGroup>
      <ProjectReference Include="..\xyz.Utilities\xyz.Utilities.csproj" />
    </ItemGroup>
</Project>

我觉得这可能与构建服务器上的Visual Studio版本有关。我们正在运行TeamCity。有没有人有任何线索?

我在一个net48完整框架解决方案中遇到了同样的情况。如果您已经找到了任何解决方法,请告诉我。这种情况也会在本地运行dotnet test时发生。我们的设置包括调用ClassCleanupBehavior的specflow。 - Jasper
嗯,我以某种方式解决了它...(非常有帮助-我知道,抱歉) 但最终我强制所有解决方案中的项目运行 <TargetFramework>net5.0-windows</TargetFramework> 并在我的TeamCity构建步骤中执行了dotnet.restore.....并删除了$(SolutionDir)Bin$(Configuration)后面的''。 - kfn
好的,谢谢您回复我。 - Jasper
2个回答

26

将你的NuGet软件包升级到最新版本,这应该可以解决你的问题。我认为你需要2.2.8版本的MSTest.TestAdapterMSTest.TestFramework


可能这就是解决方法...嗯,我在失败的单元测试项目中使用了2.2.7版本,在一个正常工作的项目中使用了2.2.8版本... - kfn
我升级为使用2.2.8和Microsoft.net.test.sdk 17.0.0,问题得到了解决。 - kfn
我遇到了完全相同的错误,但更新到2.2.8版本并没有解决问题。看起来自从我升级到VS 2022以来就开始出现这个问题。我将尝试安装最新的VS更新。 - tommyk
2
我们发现,在 csproj 文件中仍然存在对 Microsoft.VisualStudio.TestPlatform.TestFramework* 的引用会导致这些 NuGet 包在 VS 2022 中无法生效。 - Brandon
如果您已经更新了答案中提到的软件包,这可能仍然不足以防止问题的发生。在我的情况下,我还不得不卸载这两个多余的软件包: Microsoft.TestPlatform.ObjectModelMicrosoft.VisualStudio.TestPlatform - undefined

9

我按照建议进行了升级,现在它可以正常工作了。 在此输入图片描述


1
请将此内容移至答案的注释中。 - Steve Dunn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接