testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Possible issue with Microsoft Fakes

Open avivanoff opened this issue 11 months ago • 8 comments

A project that uses MSTest.Sdk/3.7.0 and EnableMicrosoftTestingExtensionsFakes set to true has issues execution tests that use fakes. Partial trace output:

  Message:
Test method TestMethod1 threw exception: 
Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException: Failed to resolve profiler path from COR_PROFILER_PATH and COR_PROFILER environment variables.

  Stack Trace: 
IntelliTraceInstrumentationProvider.ResolveProfilerPath()
IntelliTraceInstrumentationProvider.Initialize()
UnitTestIsolationRuntime.InitializeUnitTestIsolationInstrumentationProvider()
ShimRuntime.CreateContext()

Unfortunately, I was not able to create a project to reproduce the issue.

avivanoff avatar Dec 24 '24 07:12 avivanoff

Some additional observations:

  • Test project targets .NET Framework 4.8.
  • Running EXE directly results in the same error.
  • Running the tests using vstest.console.exe has no issues.
  • Converting back to non-MSTest.Sdk works as expected.

avivanoff avatar Dec 24 '24 07:12 avivanoff

Finally, I was able to create a sample project.

avivanoff avatar Dec 26 '24 19:12 avivanoff

Looking at the sample the <PlatformTarget>AnyCPU</PlatformTarget> portion of the csproj is causing issues.

When specified, the <RuntimeIdentifier> property is not automatically resolved via the .NET SDK. As a result, NuGet will not restore the necessary native Fakes dlls to the bin folder.

Removing <PlatformTarget>AnyCPU</PlatformTarget> or for instance explicitly setting <RuntimeIdentifier>win-x64</RuntimeIdentifier> makes the NuGet native dll files get copied to the bin folder (such as FakesInstrumentationProfiler_x64.config, Microsoft.QualityTools.Testing.Fakes.dll, Microsoft.QualityTools.Testing.Fakes.Instrumentation_x64.dll, Microsoft.Testing.Extensions.Fakes.dll) and the tests run successfully.

The reason why this is working with vstest.console (non-MSTest.SDK) is that it dynamically loads the Fakes libraries from C:\ProgramFiles, whereas the testing platform is designed to be self-contained and all of the libraries needed to run Fakes are expected to be in the bin folder instead.

drognanar avatar Jan 06 '25 12:01 drognanar

@drognanar Thanks for the investigation. Would it be possible to update the Fakes extension doc page?

Evangelink avatar Jan 06 '25 12:01 Evangelink

@evangelink it makes sense to add an FAQ entry as the same issue impacts code coverage, i.e. in this configuration the coverage file generated by running testProject.exe --coverage will be empty, since the code coverage native binaries are not copied into the bin folder.

drognanar avatar Jan 06 '25 15:01 drognanar

@drognanar, @Evangelink, looks like the SDK-style projects have problems with .NET 4.8 targeting. If you change the target framework in the sample project to net8.0 it works.

avivanoff avatar Jan 06 '25 16:01 avivanoff

@drognanar, @Evangelink, removing <PlatformTarget>AnyCPU</PlatformTarget> results in another issue: you cannot build for multiple platforms anymore. The reason is that fake assemblies are generated in FakesAssemblies folder. So if I build x64 first, the generated assemblies will be x64, and subsequent build of ARM64 or x86 will fail.

avivanoff avatar Jan 06 '25 20:01 avivanoff

Ping @drognanar

Youssef1313 avatar Jun 04 '25 02:06 Youssef1313

Ping @drognanar

Evangelink avatar Jul 03 '25 11:07 Evangelink

The .NET 48 issue is a NuGet issue. If adding the code coverage extension adds files to the bin folder, but adding the fakes extension does not, then this would be an issue on the fakes extension nuget package instead.

On the other hand the <PlatformTarget></PlatformTarget> is a feature that was not previously supported as the fakes compilation did not support multi architecture solutions and had one path to the fakes dlls regardless of the csproj configuration. I'm not aware of a workaround, but to create multiple projects.

A fix for <PlatformTarget> is currently in the backlog, but currently there were several issues being worked on having a higher priority.

drognanar avatar Jul 03 '25 11:07 drognanar

Closing this as this bug currently falls under low priority category. Should the priority change or should there be more asks about adding this feature to fakes, we'll be happy to revisit this task.

drognanar avatar Jul 16 '25 09:07 drognanar