Search Unity

MonoBehaviourTest<T0> ??

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

  1. techmage

    techmage

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

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    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,133
    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:
    540
    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.
     
  6. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    2.5 years later: Still zero documentation on this apart from 3 incoherent sentences that are meaningless in English. The official Unity Test Framework has even less (the entry for it is blank here: https://docs.unity3d.com/Packages/com.unity.test-framework@1.1/api/UnityEngine.TestTools.html)

    Anyone figured out WTF this is actually meant to do, and how it works, and what the rules are? Any info AT ALL about the specification and purpose?

    The name suggests its for mocking MB's. The example in the docs is absurd and would never exist in a real project (I'm not sure it's even compilable! Given that tests are required to live in a separate assembly, which means: not in the same classfile?)
     
    roointan and Jelling like this.
  7. Katoha

    Katoha

    Joined:
    Feb 17, 2020
    Posts:
    5
    I know this is a bit old but i managed to get this to work with the following test.
    Code (CSharp):
    1. public class ExperienceBarControllerTest
    2.     {
    3.         [UnityTest]
    4.         public IEnumerator TestExperiencBarFillsCorrectly()
    5.         {
    6.             yield return new MonoBehaviourTest<ExperienceBarTest>();
    7.         }
    8.  
    9.         public class ExperienceBarTest : MonoBehaviour, IMonoBehaviourTest
    10.         {
    11.             // Need to set testFinished to true after assertions.
    12.             private bool testFinished = false;
    13.  
    14.             private void Awake()
    15.             {
    16.                 // Instantiate monobehaviour under test with addComponent
    17.                 var controller = gameObject.AddComponent<ExperienceBarController>();
    18.                 // Add any serializable field references to prevent null pointer exceoptions.
    19.                 controller.experienceRenderer = new GameObject();
    20.                 controller.experienceRenderer.AddComponent<ExperienceItemRendererController>();
    21.                 controller.container = new GameObject();
    22.  
    23.                 // Method under test.
    24.                 controller.setExperience(222);
    25.  
    26.                 // Assertions that the monobehaviour updated correctly.
    27.                 var itemRenderers = controller.container.GetComponentsInChildren<ExperienceItemRendererController>();
    28.                 Assert.AreEqual(10, itemRenderers.Length);
    29.                 Assert.AreEqual(1, itemRenderers[0].getExperience());
    30.                 Assert.AreEqual(1, itemRenderers[1].getExperience());
    31.                 Assert.AreEqual(0.22f, itemRenderers[2].getExperience());
    32.                 Assert.AreEqual(0, itemRenderers[3].getExperience());
    33.                 Assert.AreEqual(0, itemRenderers[4].getExperience());
    34.                 Assert.AreEqual(0, itemRenderers[5].getExperience());
    35.                 Assert.AreEqual(0, itemRenderers[6].getExperience());
    36.                 Assert.AreEqual(0, itemRenderers[7].getExperience());
    37.                 Assert.AreEqual(0, itemRenderers[8].getExperience());
    38.                 Assert.AreEqual(0, itemRenderers[9].getExperience());
    39.  
    40.                 // Tests finished
    41.                 testFinished = true;
    42.             }
    43.  
    44.             // Required to tell test suite we are finished.
    45.             public bool IsTestFinished
    46.             {
    47.                 get { return testFinished; }
    48.             }
    49.         }
    50.     }
     
    roointan and Jelling like this.
  8. zerofruit

    zerofruit

    Joined:
    Jun 8, 2022
    Posts:
    1