- Home /
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 !
Yep, I've been going 'round this same problem. Looks like if you have to test a method that takes a Vector3, the test has to be inside the same project and you have to reference nunit using assets.
Of course, that means you're compiling unit tests in release code which is ridiculous. Either that, or putting all test code in #ifdef's
Frankly, the idea of testing in Unity seems half-baked to me, but maybe I'm just missing something obvious.
Answer by ljfranklin · Feb 20, 2012 at 06:52 AM
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/”.
Answer by Ricardo · Mar 31, 2010 at 09:06 AM
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.
@Ricardo, could you expand on the steps needed? Although llavigne has a set of steps, I got errors trying to load irrelvnt.dll. Do I need to create a separate Namespace for my non-$$anonymous$$onobehavior .cs files? Thanks.
That's a funny error (assu$$anonymous$$g that's the actual dll name and not some placeholder you typed). Namespaces are not an issue, so I'm a bit at a loss as to what might be going on. I simply have a separate test project, which I have to keep adding to the solution as Unity forgets it when recreating it, and which runs just fine from $$anonymous$$onoDevelop. Can you give us more details Cyclops?
As llavigne mentioned, yes there actually is a irrelevant.dll in a side-directory which I tried adding to NUnit, but it was... irrelevant. What I did that worked was just copy my non-monobehavior scripts to a directory outside the Unity project, and using Visual Studio Express, create a new assembly/dll for NUnit. This certainly works, but means I have two copies of the script files in separate locations. I wasn't sure if there was some workflow that used only the one set of files, but didn't interfere with Unity's project. For instance (continued) -
For instance, can VS Express be told to create more than one assembly from a project, containing only the non-monobehavior files, in a different directory? Not sure if this is clear or not. :)
Ah, I see, you're using VS Express. I'm afraid I've only used this with Unity in $$anonymous$$onoDevelop, and have used NUnit on a full VS version (but not for a Unity project). I didn't have to jump through as many hoops - I merely created an NUnit project for the solution and added a reference to the one with the main codebase. $$anonymous$$onoDevelop supports NUnit projects natively. I'd recommend looking into the NUnit plugin that's floating around, but I don't believe VSExpress supports plugins.
Answer by AngryAnt · Mar 31, 2010 at 09:12 AM
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.
Answer by Ashkan_gc · Jul 10, 2010 at 05:17 PM
this is what i found useful it allows you to use nUnit lite.
Answer by thomasw · Feb 16, 2012 at 09:46 AM
UnityCommunity has a page on using NUnit with Unity.
Note that this is NUnitLite, which appears to be an inactive project.
Your answer
![](https://koobas.hobune.stream/wayback/20220612140118im_/https://answers.unity.com/themes/thub/images/avi.jpg)