Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Resolved PlayMode - Validate an exception throwed in a start function

Discussion in 'Testing & Automation' started by davebr15, Aug 3, 2019.

  1. davebr15

    davebr15

    Joined:
    Jun 20, 2019
    Posts:
    2
    Hello,

    Is there a way to catch an exception throwed by the start function of a MonoBehaviour for testing in PlayMode?

    Since I need to "yield" to execute the start method of the tested monobehaviour, I cannot use try-catch neither Assert.Throws.

    Code (CSharp):
    1.  
    2. [UnityTest]
    3. public IEnumerator Test() {
    4.     Exemple exemple = new GameObject().AddComponent<Example>()
    5.     exemple.SomeValue = "value"
    6.     return yield null //execute start method of the monobehaviour
    7.     // Catch the exception ?
    8. }

    Thanks
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,506
    Not sure it will work, but if you are expecting an exception you might be able to "expect" it using the LogAssert class.
     
    Steedalion likes this.
  3. davebr15

    davebr15

    Joined:
    Jun 20, 2019
    Posts:
    2
    Thanks, it works since I expect the exception !
     
    liortal likes this.
unityunity