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

How to handle unit tests in a package?

Discussion in 'Package Manager' started by benmultiplayerguys, Oct 18, 2018.

  1. benmultiplayerguys

    benmultiplayerguys

    Joined:
    Oct 3, 2018
    Posts:
    9
    I'm experimenting with moving some code out from my main program into a separate package, which seems to be working well. However, that code has some NUnit tests, and these either produce errors saying it can't import NUnit, or sometimes just gives errors after that point about Unexpected symbols inside the calls, and it's quite weird and inconsistent.

    I tried just setting up 2 asmdefs, one in the Editor subdirectory and one in the Runtime subdirectory, hoping that I could just set up the Editor assembly to access tests by having the Test Assemblies box checked, but my 'editor' assembly doesn't even show in the Visual Studio solution, never mind have tests showing in the Test Runner window.

    Is there an easy way to set up a package so that:
    • It has separate Editor and Runtime folders
    • The editor has access to NUnit?

    Currently (still) using 2018.2.2, but interested to know if anything has changed in this area since then.
     
    Kokowolo likes this.
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
  3. benmultiplayerguys

    benmultiplayerguys

    Joined:
    Oct 3, 2018
    Posts:
    9
    Yup, I have that setup. But it isn't reliable - it doesn't notice when I make changes in the Tests/Editor directory. The regular logic in Editor and Runtime is fine, but code in Tests/Editor and Tests/Runtime often just isn't found at all.

    If I delete and recreate the asmdef in the Tests/Editor, only then will it find and compile the code in there, but it still doesn't work:
    • If the assembly does not have Test Assemblies enabled, it shows up as a new assembly but says it can't compile the code, giving a syntax error. Also, it can't open the code by double-clicking the error in the Console (it just does nothing). I have to manually find it in Visual Studio.
    • If the assembly does have Test Assemblies enabled, the compile error is gone - but so is the assembly. In Visual Studio Solution Explorer I see MyPackage and MyPackage.Editor but MyPackage.Editor.Tests has disappeared entirely. Nothing is added to the Test Runner window.
    (I think it's not helped by the fact that sometimes the unit test discovery system just starts failing silently and won't discover any new tests until I restart Unity.)

    Here's the unit test I'm trying, which works perfectly in a regular Editor folder in my project but not at all from the Package/Tests/Editor folder.

    Code (CSharp):
    1.  
    2. using NUnit.Framework;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. namespace PackageEditorTest
    7. {
    8.    [TestFixture]
    9.    public static class UnitTestTest
    10.    {
    11.        [Test]
    12.        public static void WhoTestsTheTests()
    13.        {
    14.            Assert.AreEqual(1, 1);
    15.        }
    16.    }
    17. }
    18.  
     
  4. benmultiplayerguys

    benmultiplayerguys

    Joined:
    Oct 3, 2018
    Posts:
    9
    Actually, I just discovered that the Editors directory of my package doesn't work properly either.
    • if the assembly does NOT have 'Test Assemblies' flagged, my simple editor script in there works (e.g. I add a menu entry via [MenuItem] but building the project fails because "the type or namespace name `MenuItem' could not be found". I guess this is as expected.
    • if the assembly DOES have 'Test Assemblies' flagged, the project builds properly but the menu entry is gone and that assembly is missing from the solution.
    Seems like this is a more fundamental issue, bypassing the test stuff - assemblies flagged as TestAssemblies don't work in the Editor.
     
  5. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
  6. benmultiplayerguys

    benmultiplayerguys

    Joined:
    Oct 3, 2018
    Posts:
    9
    Thanks! The good news is that after fixing the assembly definitions, I can now build everything, and see it all in Visual Studio. It turns out that failing to clear all the platforms except Editor stops the tests in that assembly showing in the test runner, even when Test Assemblies is set, and that was the last piece of the puzzle.
     
    pedro_unity likes this.