Search Unity

MonoBehaviourTest<T0> ??

Discussion in 'Scripting' started by techmage, May 6, 2017.

  1. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,068
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,702
    It's for unit testing MonoBehaviours.

    It appears to be new (this is the first I'm hearing of it). But it's api appears to be you start you unit test as a coroutine, and you use this as a yield instruction to run a test on said MonoBehaviour (passed in as the generic type T0).
     
  3. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,068
    has anyone used this? Could someone post a code sample?
     
  4. littlstarsunny

    littlstarsunny

    Joined:
    Apr 12, 2017
    Posts:
    7
    @techmage

    Snippet is from:
    https://docs.google.com/document/d/13zXykcNGMKl7gL0zbh3RSx4lT11qBnOV0JrKAKVxhJk/edit#

    Code (CSharp):
    1. [UnityTest]
    2. public IEnumerator MonoBehaviourTest_Works()
    3. {
    4.     yield return new MonoBehaviourTest<MyMonoBehaviourTest>();
    5. }
    6. public class MyMonoBehaviourTest : MonoBehaviour, IMonoBehaviourTest
    7. {
    8.     private int frameCount;
    9.     public bool IsTestFinished
    10.     {
    11.         get { return frameCount > 10; }
    12.     }
    13.      void Update()
    14.      {
    15.         frameCount++;
    16.      }
    17. }
    18.  
    This will create a new instance of the monobehaviour and wait for IsTestFinished to return true.

    @Tomek-Paszek
    I'm looking into testing a reference to a monobehaviour instead of creating a new instance. Does anyone know how this can be done?

    Thanks
     
  5. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    459
    Is there any another examples?
    Is it ok to use [UnityTest] inside Monobehaviour. i see that those methods marked by this attribute appeared in test runner, but I don't understand how it work. e.g. Is Monobehaviour instantiated everytime for each test method. and so on.