Search Unity

Integrating XUnit with Unity, resolving - assembly:<unknown assembly>

Discussion in 'Scripting' started by iliaJelly, Jan 2, 2021.

  1. iliaJelly

    iliaJelly

    Joined:
    Jan 15, 2017
    Posts:
    10
    Hi all,

    In my enterprise it would be much beneficial if I could integrate Unity with XUnit. I have opened an XUnit .net 4.7.2 project and added to it as a reference the Unity assembled project from the Assembly Definition that I wish to run tests on.

    Everything works good until the test encounters some Unity specific funtionality like the UnityWebRequest and the Application.internetReachability functionalities.

    I get these exceptions
    assembly:<unknown assembly> type:<unknown type> member:(null)

    No stack trace is given with these exceptions (its weird)

    I am using Rider IDE test runner.

    I dont know how to solve this problem, what references should I add?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I would assume UnityEngine ? Good luck with this. I've never seen anyone stick with unit testing under Unity long enough just because there's so many gotchas to get it all set up properly.
     
    iliaJelly likes this.
  3. iliaJelly

    iliaJelly

    Joined:
    Jan 15, 2017
    Posts:
    10
    I have tried adding UnityEngine dll and all the UnityWebModules. However core Unity Functionalities seem to be missing. What I have decided to do now is to Inject .Net functionalities into my Unity Code so that No Actual Unity Engine functionalities will be used in Testing (Logging, Networking, Time.currentFrame, etc).

    I would still really like to know how to invoke UnityEngine functionalities outside a Unity Project. It would be a much more valid test if the Testing would actually perform the real thing.
     
  4. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    150
    As far as I know it's impossible.
    Unity specific functionality depend on Unity runtime. it use a lot of internal calls(don't be confused with P/Invoke) into unity engine.

    so probably only available option to perform real things in tests it's run tests under Unity Project.
     
    Last edited: Jan 3, 2021
    Kurt-Dekker likes this.
  5. iliaJelly

    iliaJelly

    Joined:
    Jan 15, 2017
    Posts:
    10
    @Unity devs

    If its impossible then so be it, we will just have to write a MockUnityEngine

    Currently doing a simple test

    Code (CSharp):
    1. namespace Jellybone.ContractTests
    2. {
    3.     public class UnitTest1
    4.     {
    5.         [Fact]
    6.         public void Test1()
    7.         {
    8.             UnityWebRequest rr = new UnityWebRequest("www.google.com");
    9.  
    10.             Assert.True(rr != null);
    11.         }
    12.     }
    13. }

    in XUnit produces the following Error

    System.MissingMethodException
    assembly:<unknown assembly> type:<unknown type> member:(null)
    at (wrapper managed-to-native) UnityEngine.Networking.UnityWebRequest.Create()
    at UnityEngine.Networking.UnityWebRequest..ctor (System.String url) [0x00008] in /Users/builduser/buildslave/unity/build/Modules/UnityWebRequest/Public/UnityWebRequest.bindings.cs:160
    at Jellybone.ContractTests.UnitTest1.Test1 () [0x00007] in /Users/iliag/jellyclienttools/Tests/Jellybone.ContractTests/Jellybone.ContractTests/UnitTest1.cs:14
    at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
    at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in /Users/builder/jenkins/workspace/build-package-osx-mono/2020-02/external/bockbuild/builds/mono-x64/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395

    I have added as a reference the UnitEngine dll and WebRequestModule. And the XUnit solution is building.

    So is it possible or not?