简体   繁体   中英

NET 5 MsTest Aborts on Interop tests

I have been fighting a strange MsTest issue on Interop unit tests for hours. The context is Windows11, NET 5, VStudio 2019 and 2022, and Interop with Office Apps Access, Excel, Word, Outlook, and PowerPoint.

All projects and test project.csproj files are set to 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.

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.

  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.

  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. 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.)

  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. The Word test projects are flakey, sometimes showing the issue and sometimes not. The unit tests for Outlook, Access, and Powerpoint are all working fine. (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.

  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. But they seem unrelated and distant to my code.

Special VS2022 Observations

  1. VS2022 does not show the warning messages but still has the aborting problem. 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.

  3. Yet VS2022 still aborts on the tests just like VS2019.

From my Excel unit test project using VS2019:

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". Type library importer encountered a property getter 'Type' on type 'Microsoft.Office.Core.SeriesGradientStopColorFormat' without a valid return 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

From my Excel code project using VS2019:

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". Type library importer encountered a property getter 'Type' on type 'Microsoft.Office.Core.SeriesGradientStopColorFormat' without a valid return 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

From my Excel code project (it calls the Word interop in one or two places) using VS2019:

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". Type library importer encountered a property getter 'Dummy1' on type 'Microsoft.Office.Interop.Word._Document' without a valid return 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

An example of my.csproj files for the Excel test project:

<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?

After many more hours of investigating, it turned out the problem was the Resharper Test Runner for NET 5 projects. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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