NUnit anyone?

Can I attach NUnit to a Unity project? In the remote chance I can, how?

Build NUnit (or NUnitLite) as a DLL and drop it in your assets folder. Define tests, using it, in .cs files in your assets folder. Look at how the default test runners and report gatherers work, and convert them to suit your needs - e.g. you might want the report to go into a buffer displayed using the Unity GUI, or maybe to Debug.Log, or to a file, or to a remote logging solution like log4net.

It is more difficult if you want to test things in a more Unity-driven fashion - for example, components that need to be updated by Unity for several frames before they yield results. But that’s not really what NUnit is for, and I’m not sure there’s much or any benefit in trying to use it for that.

Thanks George. That sounds involved. What is the normal way people test in Unity?

What do you want to test?

Everything if possible. Maybe it isn’t really necessary in Unity. The scripts seem easy to manage without testing.

Frankly, I expect that most people don’t test much in Unity. A lot of the users seem to have a very casual approach to writing code, often not really caring whether it’s any good so long as it appears to get the job done. So their testing strategy would be to just run their game and see if it seems to work.

If you want to do unit testing, though, then NUnitLite is a good place to start - making the reporting point somewhere else is not as complicated as I might have made it sound. But, out of the box, it won’t help you much with testing interactions between components over the course of several frames. You could make that work using coroutines, perhaps. Have you looked on the Asset Store? Somebody might have already done it.

You might be interested in a unit testing framework that we’re developing. It allows thorough unit testing outside of Unity entirely, see this post.