0

I try to make a simple nunit test within Unity, but get missing reference error when i try to reference a MyScript

Project setup:

├── Assets
├── Scenes
├── Scripts
│   └── MyScript.cs
│   └── NewAssembly.asmdef -> see below "what i tried"
├── Tests
│   ├── MyScriptTest.cs
│   └── Tests.asmdef

Tests.asdmdef (created by Unity)

{
    "name": "Tests",
    "rootNamespace": "",
    "references": [
        "UnityEngine.TestRunner",
        "UnityEditor.TestRunner"
    ],
    "includePlatforms": [
        "Editor"
    ],
    "excludePlatforms": [],
    "allowUnsafeCode": false,
    "overrideReferences": true,
    "precompiledReferences": [
        "nunit.framework.dll"
    ],
    "autoReferenced": false,
    "defineConstraints": [
        "UNITY_INCLUDE_TESTS"
    ],
    "versionDefines": [],
    "noEngineReferences": false
}

MyScriptTest.cs:

using NUnit.Framework;
public class MyScriptTest
{
    [Test]
    public void SomeTest()
    {
        var obj = new  MyScript(); // Error CS0246  The type or namespace name 'MyScript' could not be found (are you missing a using directive or an assembly reference?)
    }
}

MyScript.cs

using UnityEngine;
using TMPro; // see below "What i tried"
public class MyScript : MonoBehaviour {}

Unity: 2030.3.37f1
Visual Studio: 2019 (Version 16.11.20)

What i tried
I tried to solve the problem with how-to-set-up-unit-tests-in-unity-and-fix-missing-assembly-reference-error by creating a Assemply Definition in Scripts/. This solves the error in MyScriptTest.cs, but then i got tons of other errors in my (previously working) scripts like Assets\Scripts\MyScript.cs(2,7): error CS0246: The type or namespace name 'TMPro' could not be found (are you missing a using directive or an assembly reference?)

Any help is welcome! I can't believe this is so complicated....

Update:

Above was just a small example of the problem. I have around 46 issues like this in my project (many of the link to external libraries or own classes which i can't find the suitable reference by its name), so having an assembly reference for every single using in my project just to do unit test work is not acceptable. Is this really the only way to do unit test work in Unity?

tturbo
  • 680
  • 5
  • 16

1 Answers1

1

in the NewAssembly - which the MyScript belongs to - reference the Unity.TextMeshPro assembly (and any others that are required) as dependency (= "references")

enter image description here

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • While this solves one particular problem, I have 46 (!) others like this in my project. I can't believe I have to manually wire together every assembly, including every reference to my own classes, just to enable unit testing? Is that really the only way? If so, I'll have to decide not to do unit testing (or build my own "poor mans" unit testing framework) – tturbo Jan 16 '23 at 08:32
  • well, yes, just solve them one by one ... I don't believe you have 46 assemblies ;) Most of them will probably share the same assembly. But yes this is pretty normal - some IDEs might even do it for you (e.g. Rider does) ... do it once now => then it is over ;) – derHugo Jan 16 '23 at 08:48
  • True some are the same, but "do it one" isn't true as the project is growing and more and more assembly will be come together. I just unreliable that this can't be done automatically.. again, without unit test the project works normal, so technically there is no problem in my opinion. – tturbo Jan 16 '23 at 09:02
  • up to you of course ^^ but I would rather go that tiny extra mile ;) – derHugo Jan 16 '23 at 09:03
  • Also, which assembly reference to i have to add for my own classes which are in the global namespace? Or libraries which don't appear in the list "Assembly Definitions Reference"? (your supposed way is what i tried before seeking help on stackoverflow, but i wasn't able do find many of my references) – tturbo Jan 16 '23 at 09:03
  • BTW to you know a way to do it automatically like Rider does in Visual Studio (2019)? – tturbo Jan 16 '23 at 09:07
  • The supposed way would be that your stuff is in a dedicated assembly usually .. even splitting it out into `Assets/MyStuff/Runtime` and `Assets/MyStuff/Editor` .. and then accordingly `Assets/MyStuff/Tests` and `Assets/MyStuff/EditorTests` ... in your question you seemed to at least do this partially in `NewAssembly` – derHugo Jan 16 '23 at 09:08