简体   繁体   English

NET 5 MsTest 在互操作测试中中止

[英]NET 5 MsTest Aborts on Interop tests

I have been fighting a strange MsTest issue on Interop unit tests for hours.几个小时以来,我一直在与 Interop 单元测试中的一个奇怪的 MsTest 问题作斗争。 The context is Windows11, NET 5, VStudio 2019 and 2022, and Interop with Office Apps Access, Excel, Word, Outlook, and PowerPoint.上下文是 Windows11、NET 5、VStudio 2019 和 2022,以及与 Office Apps Access 互操作、Excel、Word、Outlook 和 PowerPoint。

All projects and test project.csproj files are set to TFM net5.0-windows7.0.所有项目和测试 project.csproj 文件都设置为 TFM net5.0-windows7.0。 Some projects import Microsoft.Office.Core , and all projects import their respective Interop libraries such as Microsoft.Office.Interop.Excel , etc.一些项目导入Microsoft.Office.Core ,所有项目导入各自的互操作库,例如Microsoft.Office.Interop.Excel等。

The problem is that tests for some of the apps work fine, whereas for others the test framework aborts (with no exceptions or error messages that I can see or find) immediately after clicking the test button beside the test method name.问题是某些应用程序的测试工作正常,而对于其他应用程序,测试框架在单击测试方法名称旁边的测试按钮后立即中止(没有我可以看到或找到的异常或错误消息)。

Observed things:观察到的东西:

  1. The test framework reports the test as inconclusive when it aborts.测试框架在中止时将测试报告为不确定。 (I say "abort," but the test just finishes instantly and inconclusively without crashes or error messages. I don't think the test ever gets run. Sometimes the little white debug circles show up at the breakpoints as if the debugger is going to stop there soon, but it never gets there.) (我说“中止”,但测试只是立即完成且不确定,没有崩溃或错误消息。我认为测试永远不会运行。有时白色的小调试圆圈出现在断点处,就好像调试器要很快就停在那里,但它永远不会到达那里。)

  2. Sometimes (not always) the unit test window shows 'Interop' in using Microsoft.Office.Interop.Excel in red as if it can't find the COM assembly.有时(并非总是)单元测试 window 在using Microsoft.Office.Interop.Excel时以红色显示“互操作”,就好像它找不到 COM 程序集一样。

  3. Sometimes (not always) the unit test code will show 'Application' or 'ActiveWindow' or 'Range' (or some other second-level object name) in red in Excel.Application or var myrange = sheet.Range["A9","C9"] as if it can't find the definitions.有时(并非总是)单元测试代码会在Excel.Applicationvar myrange = sheet.Range["A9","C9"]好像找不到定义。

  4. For example, I tried to set a debug breakpoint on lines 2 and 3 on this little test, but the breakpoints are never reached.例如,我尝试在这个小测试的第 2 行和第 3 行设置调试断点,但从未到达断点。 The test framework aborts/skips instead.测试框架会中止/跳过。

     [TestMethod] public void Simple() { var foo = 2; }
  5. The same project file contents and packages are used for all five office apps (except they import their own Interop libraries for Excel, Office, Outlook, etc.)所有五个办公应用程序都使用相同的项目文件内容和包(除了它们为 Excel、Office、Outlook 等导入自己的互操作库之外)

  6. The problem has only occurred on three of the apps, and for some reason went away on two of the apps.该问题仅出现在三个应用程序上,并且由于某种原因在两个应用程序上消失了。 I am working on Excel now, which is why my examples are for Excel.我现在正在研究 Excel,这就是为什么我的示例适用于 Excel。 The Word test projects are flakey, sometimes showing the issue and sometimes not. Word 测试项目很不稳定,有时会显示问题,有时不会。 The unit tests for Outlook, Access, and Powerpoint are all working fine. Outlook、Access 和 Powerpoint 的单元测试工作正常。 (If they exhibited the issue previously, it has gone away.) (如果他们以前展示过这个问题,它已经消失了。)

  7. All the code, tests, etc. have worked flawlessly on NET Framework 4.8 for almost two years.所有代码、测试等都在 NET Framework 4.8 上完美运行了将近两年。

  8. The only compilation warnings that I get for these Office projects --- on VS2019 only -- are like the ones shown below from my Excel project.我为这些 Office 项目获得的唯一编译警告(仅在 VS2019 上)类似于我的 Excel 项目中显示的以下警告。 But they seem unrelated and distant to my code.但它们似乎与我的代码无关且遥远。

Special VS2022 Observations特殊 VS2022 观察

  1. VS2022 does not show the warning messages but still has the aborting problem. VS2022 不显示警告消息,但仍然存在中止问题。 It looks like a completely clean "Build successful" report.它看起来像一个完全干净的“构建成功”报告。

  2. VS2022 does not show the red 'Interop' or 'Range' or 'Application' unrecognized object messages as described above.如上所述,VS2022 不会显示红色的“互操作”或“范围”或“应用程序”无法识别的 object 消息。

  3. Yet VS2022 still aborts on the tests just like VS2019.然而 VS2022 仍然像 VS2019 一样中止测试。

From my Excel unit test project using VS2019:从我使用 VS2019 的 Excel 单元测试项目:

Severity Code Description Project File Line Suppression State Warning MSB3305 Processing COM reference "Microsoft.Office.Core" from path "C:\Program Files\Microsoft Office\Root\VFS\ProgramFilesCommonX86\Microsoft Shared\OFFICE16\MSO.DLL".严重性代码 描述 项目文件行抑制 State 警告 MSB3305 处理 COM 从路径“C:\Program Files\Microsoft Office\Root\VFS\ProgramFilesCommonX86\Microsoft Shared\OFFICE16\MSO”引用“Microsoft.Office.Core” Type library importer encountered a property getter 'Type' on type 'Microsoft.Office.Core.SeriesGradientStopColorFormat' without a valid return type.类型库导入器遇到类型“Microsoft.Office.Core.SeriesGradientStopColorFormat”的属性获取器“Type”,但没有有效的返回类型。 The importer will attempt to import this property as a method instead.导入器将尝试将此属性作为方法导入。 HspoExcelTests C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2827 HspoExcelTests C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2827

From my Excel code project using VS2019:从我使用 VS2019 的 Excel 代码项目:

Severity Code Description Project File Line Suppression State Warning MSB3305 Processing COM reference "Microsoft.Office.Core" from path "C:\Program Files\Microsoft Office\Root\VFS\ProgramFilesCommonX86\Microsoft Shared\OFFICE16\MSO.DLL".严重性代码 描述 项目文件行抑制 State 警告 MSB3305 处理 COM 从路径“C:\Program Files\Microsoft Office\Root\VFS\ProgramFilesCommonX86\Microsoft Shared\OFFICE16\MSO”引用“Microsoft.Office.Core” Type library importer encountered a property getter 'Type' on type 'Microsoft.Office.Core.SeriesGradientStopColorFormat' without a valid return type.类型库导入器遇到类型“Microsoft.Office.Core.SeriesGradientStopColorFormat”的属性获取器“Type”,但没有有效的返回类型。 The importer will attempt to import this property as a method instead.导入器将尝试将此属性作为方法导入。 HspoExcel C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2827 HspoExcel C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2827

From my Excel code project (it calls the Word interop in one or two places) using VS2019:从我使用 VS2019 的 Excel 代码项目(它在一两个地方调用 Word 互操作):

Severity Code Description Project File Line Suppression State Warning MSB3305 Processing COM reference "Microsoft.Office.Interop.Word" from path "C:\Program Files\Microsoft Office\Root\Office16\MSWORD.OLB".严重性代码 描述 项目文件行抑制 State 警告 MSB3305 处理 COM 从路径“C:\Program Files\Microsoft Office\Root\Office16\MSWORD.OLB”引用“Microsoft.Office.Interop.Word”。 Type library importer encountered a property getter 'Dummy1' on type 'Microsoft.Office.Interop.Word._Document' without a valid return type.类型库导入程序在类型“Microsoft.Office.Interop.Word._Document”上遇到了一个属性获取器“Dummy1”,但没有有效的返回类型。 The importer will attempt to import this property as a method instead.导入器将尝试将此属性作为方法导入。 HspoExcel C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2827 HspoExcel C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2827

An example of my.csproj files for the Excel test project: Excel 测试项目的 my.csproj 文件示例:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net5.0-windows7.0</TargetFramework>
        <IsPackable>false</IsPackable>
    </PropertyGroup>

    <ItemGroup>
      <COMReference Include="Microsoft.Office.Core">
        <WrapperTool>tlbimp</WrapperTool>
        <VersionMinor>8</VersionMinor>
        <VersionMajor>2</VersionMajor>
        <Guid>2df8d04c-5bfa-101b-bde5-00aa0044de52</Guid>
        <Lcid>0</Lcid>
        <Isolated>false</Isolated>
        <EmbedInteropTypes>true</EmbedInteropTypes>
      </COMReference>
      <COMReference Include="Microsoft.Office.Interop.Excel">
        <WrapperTool>tlbimp</WrapperTool>
        <VersionMinor>9</VersionMinor>
        <VersionMajor>1</VersionMajor>
        <Guid>00020813-0000-0000-c000-000000000046</Guid>
        <Lcid>0</Lcid>
        <Isolated>false</Isolated>
        <EmbedInteropTypes>true</EmbedInteropTypes>
      </COMReference>
    </ItemGroup>

    <ItemGroup>
      <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
      <PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
      <PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
    </ItemGroup>


    <ItemGroup>
      <ProjectReference Include="..\HspoExcel\HspoExcel.csproj" />
    </ItemGroup>

    <ItemGroup>
        <Reference Include="HsUtils">
            <HintPath>..\..\..\Holding\core.plt\HsUtils.dll</HintPath>
        </Reference>
        .. other normal libraries (2) that are referenced
    </ItemGroup>

</Project>

Does anyone have any idea what is going on and what I might do to fix the issue and get more reliability on the NET 5 platform?有谁知道发生了什么以及我可以做些什么来解决这个问题并在 NET 5 平台上获得更高的可靠性?

After many more hours of investigating, it turned out the problem was the Resharper Test Runner for NET 5 projects.经过更多小时的调查,结果发现问题出在 NET 5 项目的 Resharper 测试运行器上。 The problem applied to multiple kinds of projects (Interop and not).该问题适用于多种项目(互操作和非互操作)。

In all cases, the Microsoft VS Test Explorer could 1) display the code with no red words indicating missing object types and 2) run the test code properly.在所有情况下,Microsoft VS 测试资源管理器都可以 1) 显示没有红色字样的代码,指示缺少 object 类型和 2) 正确运行测试代码。

My lesson was to learn to try to switch test runners sooner the next time around rather than waste so much time on an infrastructure problem that could be bypassed easily by using a different test runner.我的教训是学会尝试在下一次更快地切换测试运行器,而不是将大量时间浪费在可以通过使用不同的测试运行器轻松绕过的基础设施问题上。 Hopefully, this story will save someone some time if they have the same issues with NET 5 and Resharper.希望这个故事能够为那些在 NET 5 和 Resharper 上遇到同样问题的人节省一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM