22

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)

pattermeister
  • 3,132
  • 2
  • 25
  • 27
  • 2
    Did you try setting the environment variables it seems to be missing? – Daniel Hilgarth Oct 15 '13 at 15:25
  • @DanielHilgarth no, because I don't know what I could set it to (setting it to arbitrary strings does not help, FWIW) – pattermeister Oct 15 '13 at 15:26
  • Install Visual Studio 2012 Update 2. You can then use MS Fakes without having to have Ultimate edition, Premium edition will be enough. http://support.microsoft.com/kb/2797912 http://blogs.msdn.com/b/visualstudioalm/archive/2013/02/08/february-ctp-for-visual-studio-update-2.aspx#fakes – Jason Evans Oct 15 '13 at 15:32
  • @JasonEvans good spot! However, I seem to be running 2012 Update 3, which is (allegedly) a rollup of all previous Updates. Yet the pain persists. I might open a Feedback item to MS on the offchance this is a bug. Thanks. – pattermeister Oct 15 '13 at 15:37

3 Answers3

15

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.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • 4
    I've accepted your answer (thanks :-) and found the reason: [Shims require special instrumentation when a process starts. Without this instrumentation, all attempts to shim any method or property throw ShimNotSupportedException. In Update 2, we moved the instrumentation check forward, so that it happens at the time ShimsContext is created. We made this change to simplify troubleshooting of Shims instrumentation problems and the ShimNotSupportedException they may produce.](http://connect.microsoft.com/VisualStudio/feedback/details/785411/cannot-debug-unit-tests-with-shims-in-vs2012-w-update2) – pattermeister Oct 15 '13 at 15:48
5

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

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
1

I believe your answer is to use one of following commands:

VSTest.Console.exe UnitTests.dll /UseVsixExtensions

VSTest.Console.exe UnitTests.dll /TestAdapterPath:%LocalAppData%\Microsoft\VisualStudio\12.0\Extensions\<nunit test adapter installation folder>

These commands will run fine on dev machines. In case you want to run unit tests on build server, copy nunit test adapter folder to build server and mention that path.

zendu
  • 1,108
  • 1
  • 14
  • 36