Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.Fakes won't run in normal unit test contexts

Tags:

I'm using a simple proof-of-concept Fakes nUnit test:

  [Test]   public void TestFakes()   {       using (var ctx = ShimsContext.Create())       {           System.Fakes.ShimDateTime.NowGet = () => { return new DateTime(2000, 1, 1); };            Assert.That(DateTime.Now.Year, Is.EqualTo(2000));       }   } 

This test runs in the Visual Studio Test Explorer, but doesn't run in:

  • nUnit GUI
  • nUnit console
  • The JetBrains test runner (dotCover OR Resharper)
  • TestDriven.net test runner

In each of these, I receive the following error:

Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException : Failed to resolve profiler path from COR_PROFILER_PATH and COR_PROFILER environment variables

When I reflect into that assembly, it seems like it's looking for Intellitrace, a VS Ultimate-only feature - I only have Premium installed.

Any suggestions on how to work around this (we use the nUnit runner on our build servers, so this is a blocker to using Fakes)

like image 804
pattermeister Avatar asked Oct 15 '13 15:10

pattermeister


People also ask

What does add fakes Assembly do?

When you click on “Add Fakes Assembly” it will create the same assembly again with the Fakes keyword added that we will use for our testing purposes. The Fakes framework uses delegate-based methods for writing code.

What is a shim C#?

What Does Shim Mean? Shim, in C#, is a template class that is derived from a base class with derived classes that inherit the data and behavior of the base class and vary only in the type. The derived class of the shim class reuses the implementation provided by the shim class.

What is Microsoft unit test framework?

Microsoft Native Unit Test Framework—The Microsoft Native Unit Test Framework for C++ is installed as part of the Desktop development with C++ workload. It provides a framework for testing native code.


2 Answers

I don't think you will be able to execute MS Fakes based tests using anything other then MS Test framework.

I believe that the way MS Fakes works causes problems for test runners such as NUnit. Precisely why this is the case, I don't know, since other mocking frameworks such as TypeMock work fine in NUnit, etc. But there is something specific to MS Fakes which make it harder (if not impossible) to run with anything other than MS Test. That's my theory anyway.

Unless the authors of NUnit, xUnit, etc add support for MS Fakes (or there is a crafty workaround), I think you will have to stick with MS Test.

EDIT:

It looks like the latest version of NCrunch v2.5 does work with MS Fakes. I've tried the beta during it's development, and can confirm that MS Fake tests were executed without fail using NCrunch.

like image 173
Jason Evans Avatar answered Oct 01 '22 11:10

Jason Evans


Fakes works only with Visual Studio Test Runner(AKA VStest.Console.exe). Even previous Microsoft MSTest runner doesn't support it.

like image 24
Michael Freidgeim Avatar answered Oct 01 '22 10:10

Michael Freidgeim