Unit testing of code written for Unity

Is anyone doing unit testing with Unity code? I’d like to do it, but I just see lots of problems with it due to all the global state… for example:

  • Current scene, as loaded through the globally available Application static members. Lots of ways everywhere to access this global state (FindObjectsOfType etc, even static methods).
  • Static accessors for Camera, Terrain, etc.
  • Physics state, “transitively” available through static methods in Physics class.

Anyone that can give some hints on how to do reasonable amounts of unit testing, while not losing the productivity of Unity completely?

It’s a bit weird really. I really like the idea of unit testing and reusable code… but people still churn out excellent Unity products that (I assume) have completely untestable (automated testing-wise) code. So are unit tests overkill for Unity game code?

Can’t really make up my mind. How do you see it? Can unit testing and Unity be friends?

BTW, found some stuff like this while searching:
http://forum.unity3d.com/viewtopic.php?t=38078

But it doesn’t really address the issue of Unity’s massive global state clashing with the opportunity to instantiate a small portion of your application, and run it isolated.

the only way to do that would be isolated test scenes basically

If anyone is still interested, SharpUnit can be used to run unit tests within Unity3D:

http://www.unifycommunity.com/wiki/index.php?title=SharpUnit

Enjoy!

Does this work with MonoDev, I’m on a Mac don’t really want to use a virtual machine anymore to get Visual Studio.

On thinking this through I realize at least the basic in unity stuff will work for sure if there is a dll included in the package, which there is.

I tried SharpUnit and the other stuff on this subject but nothing really worked for me. SharpUnit is cool but I didn’t want so much stuff written to the console, and I wanted to be able to test multiple scenes etc…

So we wrote TestStar. It might be interesting if you want to try doing more testing. You can also watch an <5min video showing off some of the cool things it does.