Is there a way to use Nunit with Unity in Monodevelop ?

I was pretty thrilled to see that Monodevelop came with a version of Nunit, however I've been hitting a wall, trying to unit-test my scripts...

I'm not sure how to go about this.

I've tried a few things, which were all dead ends, so I won't bother detailing them here.

However my approach was, mostly, trying to build a separate Nunit test project that'd use the UnityEngine dll, but that didn't work.

I'm thinking I should instead try using Nunit from within Unity, but couldn't figure it out so far...

I'm merely trying to find out if there is actually a way to test my unity-specific code using vanilla Nunit, and how I could go about that...

Will appreciate any insight.

Cheers !

When working with NUnit in MonoDevelop, I ran into a bit of weirdness where rebuilds within Unity would delete MonoDevelop’s reference to the nunit.framework dll. The eventual solution was to import the dll directly into the root of the Unity Assets folder. On Windows the dll was located at “Program Files (x86)/Unity/MonoDevelop/Addins/NUnit/”.

You can use NUnit, just likely not to test code that directly depends on UnityEngine.dll - that dll is mostly a shell to act as an interface for the actual engine. You could consider running Unity as a headless server and communicate to it via an API of your own, testing the results of the operations, but that seems more trouble than it's worth.

The approach I use is to remove as much work as possible to classes that do not depend on UnityEngine at all, abstracting the actual system work. For instance, your AI can probably be implemented mostly independent of the engine's functions, with the colliders, raycasting and whatnot acting mostly as a sensory mechanism. You then write unit testing for these standalone classes, and use them on your MonoBehaviors as well.

this is what i found useful it allows you to use nUnit lite.

I'm thinking your only nice option would be to have your unit test solution launch and build your project (via editor batch mode command-line parameters) and then run the built standalone - using Debug.Log and the log files for print/expect.

Listing your previous attempts is always a good idea though. Maybe you were onto something that just needs a bit of tweaking.

It is possible to unit test the vast majority of your code using NUnit outside of Unity from monodevelop, as sought by the original poster.

As Ricardo states this does mean changing the way you depend on UnityEngine.dll. See our post on the matter and a framework that looks like UnityEngine.dll but is testable.

In terms of a monodevelop project structure, the key is to keep your tests in an entirely separate MonoDevelop solution (outside of the Unity project folder) that references the MonoDevelop projects that Unity creates automatically (Assembly-CSharp-vs, etc).

This way, Unity still creates all your MonoDevelop projects for you but won’t obliterate your test project.

In Dec 2013, Unity Released the Unity Test Tools which has both a NUnit runner but also a Integration Test Runner for running Test Scenarios inside of Unity. While you can run the Unit Test Suite Runner in Unity proper you can also just open up MonoDevelop and the tests with the MonoDevelop NUnit runner.

Examples and the runner itself are available in the Unity Asset Store Link.

Wouldn’t you simply Mock any references to the “Unity” interface in the unit tests anyway? That seems like the best option IMO.

Check here for .NET tutorials and other articles that may be of assistance

Check here for programming tutorials and other articles that may be of assistance

I hope this helps.

“You can accomplish anything, as long as you have passion, apply focus, and devote time to trying, failing, succeeding, and repetition.” Buddy James

Unity released a package on the Asset store called Unity Test Tools - these allow the creation and execution of NUnit tests in the editor.

For more info see this blog post: Introduction to Unity Test ToolsRandom Bits

UnityCommunity has a page on using NUnit with Unity.