如何在 Visual Studio 2019 年 运行 使用 .NET 4.6.2 版进行单元测试?

How to run unit tests with .NET version 4.6.2 in Visual Studio 2019?

我正在尝试解决 Visual Studio 中单元测试运行的警告:

[6/7/2019 7:16:21 PM Warning] Test run will use DLL(s) built for framework .NETFramework,Version=v4.5 and platform X64. Following DLL(s) do not match framework/platform settings. MyProject.Tests.dll is built for Framework 4.6.2 and Platform AnyCPU. Go to http://go.microsoft.com/fwlink/?LinkID=236877&clcid=0x409 for more details on managing these settings.

我知道我需要使用与单元测试项目 (4.6.2) 框架匹配的目标框架版本更新 runsettings 文件。我只是想不通魔术弦是什么。

这里是起点:

<!-- Configurations that affect the Test Framework -->
  <RunConfiguration>
    <!-- Path relative to solution directory -->
    <ResultsDirectory>.\TestResults</ResultsDirectory>

    <!-- [x86] | x64  
      - You can also change it from menu Test, Test Settings, Default Processor Architecture -->
    <TargetPlatform>x64</TargetPlatform>

    <!-- Framework35 | [Framework40] | Framework45 -->
    <TargetFrameworkVersion>Framework45</TargetFrameworkVersion> 
    <!--
    TargetFrameworkVersion needs to be updated, but documentation is unclear on how
    cf. https://github.com/Microsoft/vstest-docs/issues/163
    <TargetFrameworkVersion>.NETFramework,Version=v4.6.2</TargetFrameworkVersion>
    -->     
  </RunConfiguration>

我能找到的每个值都会导致以下异常:

[6/7/2019 8:34:20 PM Error] Failed to configure settings for runsettings plugin 'VSTest Run Configuration' as it threw following exception: 'An error occurred while loading the settings. Error: Invalid setting 'RunConfiguration'. Invalid value 'net462' specified for 'TargetFrameworkVersion'..' Please contact the plugin author.

[6/7/2019 8:34:20 PM Diagnostic] Generate test run settings exception:System.Xml.XmlException: An error occurred while loading the settings. Error: Invalid setting 'RunConfiguration'. Invalid value 'net462' specified for 'TargetFrameworkVersion'.. at Microsoft.VisualStudio.TestWindow.VSTest.TestRunSettingsService.ValidateRunConfigurationSettings(XPathNavigator runSettingsNavigator) at Microsoft.VisualStudio.TestWindow.VSTest.TestRunSettingsService.MergeRunSettingsAndFindCompatibleSources(Architecture& platform, FrameworkVersion& framework, String resultsDirectory, String solutionDirectory, IDictionary`2 sourceSettings, IXPathNavigable inputRunSettings, String& incompatibleSourcesWarning) at Microsoft.VisualStudio.TestWindow.VSTest.TestRunSettingsService.AddRunSettings(IXPathNavigable inputRunSettingDocument, IRunSettingsConfigurationInfo configurationInfo, ILogger log) at Microsoft.VisualStudio.TestWindow.Controller.Request.GenerateTestRunSettings(RunSettingConfigurationInfoState infoState)

我尝试过的值:

相似:

我只是删除了 "TargetFrameworkVersion",它对我的​​情况有效。我想 Visual Studio 如果未指定此字段,则会自动选择正确的框架。

<!-- Configurations that affect the Test Framework -->
  <RunConfiguration>
    <!-- Path relative to solution directory -->
    <ResultsDirectory>.\TestResults</ResultsDirectory>

    <!-- [x86] | x64  
      - You can also change it from menu Test, Test Settings, Default Processor Architecture -->
    <TargetPlatform>x64</TargetPlatform>

  </RunConfiguration>

Framework 4.6.1 测试有同样的问题。由于最初的 post,https://developercommunity.visualstudio.com/content/problem/579073/test-discovery-reporting-dlls-do-not-match.html link 引用的页面已更新,并附注该问题已在 7 月 24 日发布的 Visual Studio 16.2 中修复。我升级了,从 .运行settings 文件中删除了整个 RunConfiguration 部分,现在单元测试 运行 没有错误。