7

I am trying to get Microsoft Fakes to work properly with my unit test project (net47) wherein my project file format is using the new NetSDK format.

With Visual Studio 2019, I can add a Fakes Assembly and things seem to work fine, until we try to build/run the tests on our build agent. It seems as though when you compile/build a project the fakes assemblies are generated, but they are done either in parallel or after the build (I'm not sure). This same problem happens on my development machine running Visual Studio 2019 Enterprise.


I noticed that if no FakesAssemblies folder exists, one is created during the build, but the compilation fails because none of the *.Fakes namespaces were discovered. A second compilation/build works because now the FakesAssemblies folder is populated. One thing to note is that I took the dll file for Microsoft Fakes and put it in a NuGet package in our companies private feed, that way I can pull it down as a NuGet package instead of a reference.

SampleUnitTests.csproj:

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

  <PropertyGroup>
    <TargetFramework>net47</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
    <PackageReference Include="Microsoft.QualityTools.Testing.Fakes" Version="16.0.28621.142" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
    <PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
  </ItemGroup>

  <ItemGroup>
    <Fakes Include="Fakes\*.fakes" />
    <Reference Include="FakesAssemblies\*.dll" />
  </ItemGroup>

</Project>
michael
  • 14,844
  • 28
  • 89
  • 177
  • Do you have Visual Studio 2019 on your build agent? – Nicklaus Brain Sep 19 '19 at 14:00
  • Yes. But this also happens on my development machine as well, which also has VS 2019 Enterprise. – michael Sep 20 '19 at 14:12
  • Just to be clear: Your second build *does* work as intended, since the *fakeassemblies* folder then exists, is that correct? – Kjartan Sep 25 '19 at 06:33
  • Yes. After the first build, the fakes are generated, but the build reports back as a failure. Then, subsequently, a second build passes because the source directory now contains the fake assemblies. I *could* run the build twice, but that would increase all build times across all of our builds. Ideally, it would be nice to have a single build command succeed. – michael Sep 27 '19 at 01:48
  • Just wondering why you mentioned "as a NuGet package instead of a reference" and in the csproj file it looks like a reference. Also (not familiar with Flakes package) but there might be post install scripts that are executed after getting the package (https://learn.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference#package-compatibility-issues). – theCuriousOne Oct 08 '19 at 07:42
  • How you perform the build? Could you provide a yaml config file for the build pipeline? Is the build order correct? – user2809176 Oct 08 '19 at 11:31
  • Can you share an entire solution that someone can use to reproduce the problem? – canton7 Oct 09 '19 at 11:54

0 Answers0