Fix NCrunch didn't work in project generated by Unity.
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
Change reference from Unity customed NUnit dll to NUnit official dll in csharp project.
- 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.
- Clone repository into
Assets/Plugins/Editor
directory. - 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.
Open C# Project
then Unity will regenerate project if detect changes, then Visual Studio is opened after thatOnGeneratedCSProjectFiles()
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 youOpen C# Project
. It's not good.- 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. - Unity use customed NUnit 3.5.0, so we use the same version to get best compatibility.
NUnit - v3.5.0 - MIT License