Skip to content

networm/NCrunchAdapterForUnity

Repository files navigation

NCrunchAdapterForUnity

Fix NCrunch didn't work in project generated by Unity.

Introduction

When enabled NCrunch in project generated by Unity, after initializing the status in Tests window would become Analysis failed and log message is:

An error occurred while analysing this project after it was built: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Unable to cast object of type 'nCrunch.Module.NUnit3.Integration.NUnit3CallbackProxy' to type 'System.Web.UI.ICallbackEventHandler'.
   at NUnit.Framework.Api.FrameworkController.LoadTestsAction..ctor(FrameworkController controller, Object handler)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeConstructor(Object[] args, SignatureStruct& signature, IntPtr declaringType)
   at System.RuntimeMethodHandle.InvokeConstructor(Object[] args, SignatureStruct signature, RuntimeTypeHandle declaringType)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at nCrunch.Module.NUnit3.Integration.FrameworkController.LoadTests(INUnit3CallbackHandler handler)
   at nCrunch.Module.NUnit3.Integration.NUnit3FrameworkInteractor.<prepareFramework>b__7_0()
   at nCrunch.Common.PerformanceTracking.PerfTracker.TrackActivity(String name, Action activity)
   at nCrunch.Common.PerformanceTracking.PerfTracker.TryTrackActivity(String name, Action activity)
   at nCrunch.Module.NUnit3.Integration.NUnit3FrameworkInteractor.prepareFramework(DynamicProxy[] dynamicProxies)
   at nCrunch.Module.NUnit3.Integration.NUnit3FrameworkInteractor..ctor(ReflectedAssembly assembly, IList`1 referencedAssemblyFilePaths, ComponentUniqueName testComponentUniqueName, DynamicProxy[] dynamicProxies)
   at nCrunch.Module.NUnit3.Integration.NUnit3FrameworkRuntimeEnvironment.FindFrameworkTestsInAssembly(ReflectedAssembly assembly, FilePath assemblyFilePath, IList`1 referencedAssemblyFilePaths, ComponentUniqueName testComponentUniqueName, PlatformType platformType, DynamicProxy[] dynamicProxies)
   at nCrunch.TestExecution.TestFinder..()
   at nCrunch.Common.PerformanceTracking.PerfTracker.TrackActivity(String name, Action activity)
   at nCrunch.TestExecution.TestFinder..()
   at nCrunch.Common.PerformanceTracking.PerfTracker.TrackActivity(String name, Action activity)
   at nCrunch.TestExecution.TestFinder.FindTestsForFrameworks(ReflectedAssembly assembly, FilePath assemblyFilePath, IList`1 referencedAssemblyFilePaths, DescribedTestFrameworkDiscoverer[] describedDiscoverers, ComponentUniqueName testComponentUniqueName, PlatformType platformType, DynamicProxy[] dynamicProxies)
   at nCrunch.TestExecution.RemoteTaskRunner.AnalyseAssembly(DescribedTestFrameworkDiscoverer[] applicableFrameworks, ComponentUniqueName testComponentUniqueName, PerfTracker perfTracker)

The reason is Unity customed NUnit 3.5.0 dll which had critical code that was fundamentally incompatible with the normal NUnit API. NCrunch developer has explaned it at Build failed on Unity project - Build/Test Issues - NCrunch Forum.

The Unity customed NUnit 3.5.0 dll is at path C:\Program Files\Unity\Editor\Data\UnityExtensions\Unity\TestRunner\net35\unity-custom\nunit.framework.dll

Solution

Change reference from Unity customed NUnit dll to NUnit official dll in csharp project.

Environment

  • NCrunch 3.11.0.9
  • Unity 2017.1.1f1
  • Visual Studio Community 2017 15.3.5

These are release versions and update-to-date until 2017/10/04.

Usage

  1. Clone repository into Assets/Plugins/Editor directory.
  2. Click menu Assets > Open C# Project to open Visual Studio.

Job done! You will notice that NCrunch works normally.

Note: External Script Editor should be Visual Studio to make Unity generate project compatible with Visual Studio.

Note: Use this adapter wouldn't break Test Runner in Unity. You could still use Test Runner run Unity related tests at the same time. Reference in project only affects behaviour in Visual Studio.

Inside

  1. Open C# Project then Unity will regenerate project if detect changes, then Visual Studio is opened after that OnGeneratedCSProjectFiles() is called. So if we change project in this callback, Visual Studio will prompt solution has changed and do you want to reload it everytime you Open C# Project. It's not good.
  2. So I change to use method provided by Visual Studio Tools for Unity - ProjectFilesGenerator.ProjectFileGeneration. Change project in this callback will not cause solution prompt reload.
  3. Unity use customed NUnit 3.5.0, so we use the same version to get best compatibility.

Library

NUnit - v3.5.0 - MIT License

License

MIT License

Reference

  1. Build failed on Unity project - Build/Test Issues - NCrunch Forum
  2. Customize Project Files Created by VSTU | Microsoft Docs

About

Fix NCrunch didn't work in project generated by Unity.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages