Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Testing assertions being logged (LogAssert.Expect behaviour)

Discussion in 'Testing & Automation' started by Steedalion, Oct 22, 2021.

  1. Steedalion

    Steedalion

    Joined:
    Jul 6, 2020
    Posts:
    51
    I am trying to log assertions but cannot figure out what I am doing wrong.

    Code (CSharp):
    1.         [UnityTest] //works
    2.         public IEnumerator LogMessage()
    3.         {
    4.             LogAssert.Expect(LogType.Log, "message");
    5.             Debug.Log("message");
    6.             yield return null;
    7.         }
    8.  
    9.         [UnityTest] //works
    10.         public IEnumerator LogError()
    11.         {
    12.             LogAssert.Expect(LogType.Error, "error");
    13.             Debug.LogError("error");
    14.             yield return null;
    15.         }
    16.  
    17.         [UnityTest] //not working
    18.         public IEnumerator LogException()
    19.         {
    20.             LogAssert.Expect(LogType.Exception, "");
    21.             Debug.LogException(new Exception(""));
    22.             yield return null;
    23.         }
    Also see
    https://docs.unity3d.com/Packages/c...rk@1.1/manual/reference-custom-assertion.html
    https://docs.unity3d.com/2018.2/Documentation/ScriptReference/TestTools.LogAssert.Expect.html
    https://docs.unity3d.com/2019.1/Documentation/ScriptReference/TestTools.LogAssert.Expect.html
     
    Last edited: Oct 25, 2021
  2. surnameforename

    surnameforename

    Joined:
    Sep 9, 2017
    Posts:
    2
    I have just been looking into this and this is what I found works:

    Code (CSharp):
    1.         [UnityTest]
    2.         public IEnumerator ExampleExceptionTest()
    3.         {
    4.             LogAssert.Expect(LogType.Exception, "Exception");
    5.             Debug.LogException(new Exception(""));
    6.             yield return null;
    7.         }
    The
    LogAssert.Expect
    needs to have the correct string, or it fails. This can be a matter of trial and error - I've ended up copy-pasting them from the Unity Console.

    Also if the execption is e.g. of type
    ArgumentOutOfRangeException
    the exception message is over two lines. In this case the line break characters I have found to work are
    \r\n