I have been programming mainly in Unity3D over the last fortnight, and thought it time to write some unit tests for my work. I found reference to a few testing frameworks online. The most promising appeared to be SharpUnit, and it is also released under a fairly relaxed license. However, I had to make a few changed to get it running, so this post details that work.
Unity3D compiles scripts in a set order (as described
Thus if you have C# code that references a UnityScript class there can
be a problem - although the reverse is not such an issue. When testing
UnityScript with SharpUnit this means that the C#
class can be an issue. It references the
TestCase classes, which I
want to write in UnityScript. To do this I put the SharpUnit code in a
folder under the
Plugins top-level folder (the
folder would have also worked). This means the SharpUnit code is
compiled first. Then my tests went in a new folder together with a
UnityScript rewrite of
Unity3D_TestRunner, as shown below. This
UnityScript TestRunner script is then attached to the TestRunner
GameObject (in a special testing scene) rather than the C# version as
described in the SharpUnit instructions.
Other possible solutions to this problem would be to just write my code
in C# or to change SharpUnit to load
TestCase classes dynamically at
runtime. Rewriting the TestRunner seemed easiest.
The other problem encountered is that SharpUnit uses the C# custom
[UnitTest] to identify its test methods. The equivalent in
UnityScript is a script directive (see the relevant section
@UnitTest needs to be placed at the top of each test function.
After that change I could run UnityScript tests with SharpUnit. An example test is below: