Search Unity

Can I replace/upgrade Unity's NUnit?

Discussion in 'Scripting' started by 5argon, Aug 17, 2017.

  1. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    What I want is Assert.ThrowsAsync so I can use it with the new .NET 4.6. However Unity's NUnit does not have it.

    I tried replacing nunit.framework.dll in Unity app folder inside Managed folder, delete Library folder of my game, let it rebuild, reimport all, etc. but still Assert.ThrowsAsync is not available. Any way to replace the shipped library?
     
    crekri, morhun_EP and EirikWahl like this.
  2. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    I made my own ThrowsAssert, just in case anyone needs it. You can await on it.

    Code (CSharp):
    1. public static class E7Assert
    2. {
    3.     public static async Task ThrowsAsync<T>(Task asyncMethod) where T : Exception
    4.     {
    5.         await ThrowsAsync<T>(asyncMethod,"");
    6.     }
    7.  
    8.     public static async Task ThrowsAsync<T>(Task asyncMethod,string message) where T : Exception
    9.     {
    10.         try
    11.         {
    12.             await asyncMethod; //Should throw..
    13.         }
    14.         catch(T)
    15.         {
    16.             //Ok! Swallow the exception.
    17.             return;
    18.         }
    19.         catch(Exception e)
    20.         {
    21.             if(message != "")
    22.             {
    23.                 Assert.That(e, Is.TypeOf<T>(), message + " " + e.ToString()); //of course this fail because it goes through the first catch..
    24.             }
    25.             else
    26.             {
    27.                 Assert.That(e, Is.TypeOf<T>(), e.ToString());
    28.             }
    29.             throw; //probably unreachable
    30.         }
    31.         Assert.Fail("Expected an exception of type " + typeof(T).FullName + " but no exception was thrown."  );
    32.     }
    33. }
     
    tteneder, crekri, EirikWahl and 2 others like this.
  3. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    I just hit this same problem 2 minutes ago. It's baffling how in two years they haven't updated their version of nunit.

    Is there any plans to update in the future?

    Also, thanks @5argon for the custom work around! I also read your article on async await in unity as well and it's helped me a lot!
     
    EirikWahl, 5argon and matkoniecz like this.
  4. EirikWahl

    EirikWahl

    Joined:
    Apr 7, 2018
    Posts:
    21
    Thanks for the info, @5argon . I have created an alternative implementation, which has an API that mimics the NUnit-API:

    Code (CSharp):
    1. public static TActual AssertThrowsAsync<TActual>(AsyncTestDelegate code, string message = "", params object[] args) where TActual : Exception
    2. {
    3.     return Assert.Throws<TActual>(() =>
    4.     {
    5.         try
    6.         {
    7.             code.Invoke().Wait(); // Will wrap any exceptions in an AggregateException
    8.         }
    9.         catch (AggregateException e)
    10.         {
    11.             if (e.InnerException is null)
    12.             {
    13.                 throw;
    14.             }
    15.             throw e.InnerException; // Throw the unwrapped exception
    16.         }
    17.     }, message, args);
    18. }
    19.  
    20. public delegate Task AsyncTestDelegate();
     
    ForceMagic, tteneder and 5argon like this.
  5. ForceMagic

    ForceMagic

    Joined:
    Feb 27, 2015
    Posts:
    38
    @EirikWahl Awesome, just what I needed, face the same problems, UniTask would have been the other alternative, but I didn't want to add one more dependency to my in-house upm package. Thanks!
     
    EirikWahl likes this.