Search Unity

Open source Unit testing for Unity

Discussion in 'Assets and Asset Store' started by NathanWarden_old, Sep 13, 2012.

  1. NathanWarden_old

    NathanWarden_old

    Joined:
    Oct 4, 2005
    Posts:
    37
    Hey all,

    Just wanted to share with you all an open source Unit Testing Framework I've been working on. It works within Unity itself by going to the "Window > Unit Testing" menu item.

    NOTE 1: Make sure you wrap your tests in one big #if UNITY_EDITOR or else your game will not build
    NOTE 2: Feel free to contribute :)

    Bitbucket Repo via Mercurial (https://bitbucket.org/NateWardawg/unity-tdd/)

    Get a zip file of the source

    For a quick example of what a Unit Test script might look like:

    Code (csharp):
    1. #if UNITY_EDITOR
    2. using UnitTesting;
    3. using UnityEngine;
    4.  
    5.  
    6. public class ExampleUnitTestingScript
    7. {
    8.     GameObject dummyGameObject;
    9.     const string objectName = "Dummy";
    10.  
    11.  
    12.     [SetUp]
    13.     public void HandleSetup()
    14.     {
    15.         dummyGameObject = new GameObject(objectName);
    16.     }
    17.  
    18.  
    19.     [TearDown]
    20.     public void HandleTearDown()
    21.     {
    22.         GameObject.DestroyImmediate(dummyGameObject);
    23.     }
    24.  
    25.  
    26.     [Test]
    27.     public void DummyGameObjectNameShouldBeDummy()
    28.     {
    29.         Assert.AreEqual(objectName, dummyGameObject.name);
    30.     }
    31.  
    32.  
    33.     [Test]
    34.     public void SlightlyOverPointFiveOfZeroToOneLerpShouldBeApproxPointFive()
    35.     {
    36.         float result = Mathf.Lerp(0.0f, 1.0f, 0.505f);
    37.  
    38.         // If you change the following 0.01f to 0, you're test should fail
    39.         Assert.Approx(0.5f, result, 0.01f);
    40.     }
    41. }
    42. #endif
     
    Last edited: Sep 14, 2012
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Excellent! I look forward to looking at this. Thanks for sharing!
     
  3. NathanWarden_old

    NathanWarden_old

    Joined:
    Oct 4, 2005
    Posts:
    37
    No problemo, I hope you find it useful :)
     
  4. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    Thanks for sharing, is there example on how to do effective test, in a real world scenario.
     
  5. NathanWarden_old

    NathanWarden_old

    Joined:
    Oct 4, 2005
    Posts:
    37
    That's a really good question, the problem is that all of my real world examples are either under NDA or are products that are for sale and you'd need the product to use the tests.

    Most of the run time portions of the AI Behaviors Made Easy system have Unit Tests. Unfortunately, you'd need to buy it in order to even use the tests. If you have a copy, I'd be happy to send you the tests for it :)

    With that said, this system works with anything that Unity supports such as physics, transforms, default and custom components, etc... For instance, in the example above you can use AddComponent and store the component to a class variable in the SetUp method and then do whatever tests you want in your test methods.

    Maybe when I get time, I'll write a good real world example that I can legally provide in the main source code. For now, the current provided example should be a really good start if you're familiar with unit testing and especially if you're familiar with NUnit.
     
  6. spiritxa

    spiritxa

    Joined:
    Sep 16, 2012
    Posts:
    5
    Incredible!! thanks s lot
     
  7. Nezabyte

    Nezabyte

    Joined:
    Aug 21, 2012
    Posts:
    110
    Unit testing rocks so thanks for this! :D
     
  8. NathanWarden_old

    NathanWarden_old

    Joined:
    Oct 4, 2005
    Posts:
    37
    No problemo :)

    I second that... I was a bit skeptical at first, especially from the game development side of things, but once I got using it I can honestly say that there's no reason to shy away from it :)
     
  9. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
  10. blechowski

    blechowski

    Joined:
    Jul 26, 2012
    Posts:
    4
    Thanks for the great framework!

    I have a question:
    Why in file Editor\UnitTestingWindow.cs line 79 do you call testEnumerator.MoveNext() every update, instead all at once?

    Also I sometimes get an exception at that line, because testEnumerator is null.
     
  11. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi blechowski,

    I'm glad you've found this system useful. The reason why it uses an IEnumerator is so that you can see progress, otherwise it'll looked locked up if you have a lot of tests, especially if you need to do things like spawning GameObjects and adding components as these incur a fairly large overhead compared to just raw classes and such. The way to avoid the null reference exception is to make sure no tests are running while scripts are recompiling. I'll try and see if there is a solution to this issue and fix it... unless you know of one and want to fix it and commit it up.

    Thanks again and I'm glad you're enjoying using it :)
    Nathan