Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using NSubstitute in custom package tests

Discussion in 'Package Manager' started by Kruemelchen, Sep 24, 2019.

  1. Kruemelchen

    Kruemelchen

    Joined:
    Aug 26, 2019
    Posts:
    27
    Hello guys,

    I want to substitute my classes with NSubstitute like it was described in this article:
    https://blogs.unity3d.com/2014/07/28/unit-testing-at-the-speed-of-light-with-unity-test-tools/
    but instead I now have headache :(
    NSubstitute seems neither getting shipped by Unity.TestRunner nor does the asset package for the Unity Test Tools longer exist. So I tried Nuget and using the dll. Using the official dll works as long as it's used in the main assembly. Otherwise in the packages I don't get it to run.
    Does someone have a possible setup for or suggestions this purpose? I'm nearly at the end of my knowledge and unity's fast changing API regarding custom packages and tests (and nearly any new feature) is not a great help at all (sorry :/)
    My custom packages are added from disk and marked as testables in the package manifest of the host project.
     
  2. cassandraL

    cassandraL

    Unity Technologies

    Joined:
    Dec 7, 2017
    Posts:
    111
    Hello @Kruemelchen,
    sorry to hear you have trouble testing your packages.

    You can indeed use NSubstitute by adding the dll into your package.
    You should then be able to access NSubstitute from your test assemblies.

    Are you trying to build one package or multiple ones?
     
  3. Kruemelchen

    Kruemelchen

    Joined:
    Aug 26, 2019
    Posts:
    27
    Hello @cassandraL,

    thanks for the quick response. I want to make multiple packages. Currently my setup is something like this:
    <Package Host>
    |── Packages
    ------ |── MyNamespace.MyTestTools (from disk)
    ------------└── Editor
    ------------------ |── NSubstitute.dll
    ------------------└── MyTestTools.Editor.asmdef
    ------ |── MyNamespace.PackageA (from disk)
    ------------ |── Runtime
    ------------------ |── <...>
    ------------------└── MyNamespace.PackageA.asmdef
    ------------└── Tests
    ------------------└── Runtime
    ------------------------ |── <...>
    ------------------------└── MyNamespace.PackageA.Tests.asmdef
    ------└── MyNamespace.PackageB (from disk)
    ------------ |── Runtime
    ------------------ |── <...>
    ------------------└── MyNamespace.PackageB.asmdef
    ------------└── Tests
    ------------------└── Runtime
    ------------------------ |── <...>
    ------------------------└── MyNamespace.PackageB.Tests.asmdef

    I decleared PackageA's and PackageB's package dependencies to require MyTestTools. NSubstitute.dll is only included in Editor, same go's for MyTestTools.Editor.asmdef
    MyNamespace.PackageA.Tests.asmdef and MyNamespace.PackageB.Tests.asmdef both references MyTestTools.Editor.asmdef

    Using NSubstitute.dll directly in both PackageA and PackageB results into a duplicate resource exception by unity.
    And when "Override References" is checked in PackageA and PackageB, I can also reference the dll correctly.

    But for some reasons I cannot use NSubstitute namespace in the runtime test scripts in Package A or B

    Edit: prettified the directory hierarchy
     
    Last edited: Sep 25, 2019
    brunogattai1996 likes this.
  4. cassandraL

    cassandraL

    Unity Technologies

    Joined:
    Dec 7, 2017
    Posts:
    111
    Hello,
    Is your MyTestTools.Editor.asmdef limited to the Editor platform?
    I am guessing that your Runtime assembly is not picking up the dll from an Editor-only assembly.

    What is the error message you get?
     
  5. Kruemelchen

    Kruemelchen

    Joined:
    Aug 26, 2019
    Posts:
    27
    Thank you @cassandraL,

    you were correct, MyTestTools.Editor.asmdef was limited to the Editor platform. After chancing it to be a runtime assembly it worked!
    So one more question to make sure I understood the concept:
    Is my new MyTestTools.asmdef only going to be built into the finished application when my Game Assembly explicitly reference MyTestTools.asmdef? I'm a little bit confused by the fact that it's now a runtime assembly. I really only want to use it in my tests.

    Thank you very much again for your help. For now and in advance! :)
     
  6. cassandraL

    cassandraL

    Unity Technologies

    Joined:
    Dec 7, 2017
    Posts:
    111
    Hello @Kruemelchen,
    Test assemblies are not included in the finished application.
    To mark an assembly as a test assembly,
    make sure the assembly's Auto Referenced is not checked and the Define Constraint include UNITY_INCLUDE_TESTS.

    upload_2019-9-26_10-37-48.png
     
  7. Kruemelchen

    Kruemelchen

    Joined:
    Aug 26, 2019
    Posts:
    27
    I found out which constellation works out:
    Package MyTestTools (containing NSubstitute.dll) HAS to be a runtime assembly. Otherwise namespace NSubstitute would not be recognized by referencing packages.
    But in this case NSubstitute WILL BE build into the finished application (I checked this). To prevent this I tried two things:
    * making MyTestTools a runtime test assembly -> doesn't work! Other packages can no longer reference the package
    * Only include MyTestTools.asmdef + NSubstitute in Editor -> works! And no more NSubstitute in build

    Obviously packages can only refer each others runtime assemblies. (maybe their editor assemblies too - didn't checked this)