jeudi 3 mars 2016

Running Unit Tests containing Microsoft.Fakes Via Reflection

I am trying to produce my own Test Runner that accesses unit test methods via reflection and runs them inside a separate AppDomain.

object instance = Activator.CreateInstance(targetType);

foreach (MethodInfo method in methods)
{
    method.Invoke(instance, null);
}

This works perfectly until one of the tests uses Microsoft.Fakes at which point when the method attempts to create the ShimsContext it throws this exception:

System.Reflection.TargetInvocationException was unhandled
  HResult=-2146232828
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       ...
  InnerException: 
       HResult=-2146233088
       Message=Failed to resolve profiler path from COR_PROFILER_PATH and COR_PROFILER environment variables.
       Source=Microsoft.QualityTools.Testing.Fakes
       StackTrace:
            at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.IntelliTraceInstrumentationProvider.ResolveProfilerPath()
            at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.IntelliTraceInstrumentationProvider.Initialize()
            at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InitializeUnitTestIsolationInstrumentationProvider()
            at Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.CreateContext()
            at Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()
            at UnitTests.TestRunnerTests.Builders.AppDomainBuilderTests.AppDomainBuilderBuildsAppDomainsWithPermissionStateNone() in c:\VS\UnitTests\TestRunnerTests\Builders\AppDomainBuilderTests.cs:line 130
       InnerException:

While trying to run this test, I am able to successfully call the TestInitialise Method, but it throws this exception during the TestMethod which looks like this...

/// <summary>
/// A test to ensure that the Application domain builder builds an 
/// application domains with permission state none.
/// </summary>
[TestMethod]
public void AppDomainBuilderBuildsAppDomainsWithPermissionStateNone()
{
    // Arrange
    using (ShimsContext.Create())
    {
        bool wasCalledCorrectly = false;

        PermissionState expected = PermissionState.None;

        ShimPermissionSet.ConstructorPermissionState = (permisionSet, actual) =>
        {
            if (expected == actual)
            {
                wasCalledCorrectly = true;
            }
        };

        AppDomainBuilder target = new AppDomainBuilder();

        // Act
        target.Build(this.defaultInput);

        // Assert
        Assert.IsTrue(wasCalledCorrectly);
    }
}

Having looked into the exception details, it appears to be because the tests can't access the Intellisense Profiler via the environment variables. I was wondering if anybody would have any experience in injecting this object into the AppDomain so that the Fakes can be successfully generated?

Or do I need to include the VSTest.ExecutionEngine in the AppDomain and invoke it somehow?

Any help on shining some light on this would be highly appreciated as being able to support Microsoft.Fakes without having to call out to the VSTest.Console.Exe would be highly desirable.

Thanks





Aucun commentaire:

Enregistrer un commentaire