Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

c# - How to set up unit tests in Unity and fix missing assembly reference error?

I created the following structure:

├── Assets
├── Scenes
├── Scripts
│   └── MyExample.cs
├── Tests
│   ├── MyExampleTest.cs
│   └── Tests.asmdef

Now, when I click on Run All, in the Test Runner window, in Unity, I have the following error:

The type or namespace name `MyExample' could not be found. Are you missing an assembly reference?

In Visual Studio I have two projects:

  • Assembly-CSharp (containing src)

  • Tests (containing Tests)

I added Assembly-CSharp as a reference in the second project. Visual Studio is able to build the solution with no errors.

Does anyone know how to properly setup a UnitTest regression for a Unity project?

This is Tests.asmdef

{
    "name": "Tests",
    "optionalUnityReferences": [
        "TestAssemblies"
    ]
}

MyExampleTest.cs

using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
using abc;

public class MyExampleTest{

    [Test]
    public void NewTestScriptSimplePasses() {
        // Use the Assert class to test conditions.
    }

    [UnityTest]
    public IEnumerator NewTestScriptWithEnumeratorPasses() {
        abc.Example m;
        Assert.That(false);
        yield return null;
    }
}

MyExample.cs

namespace abc
{
    public class Example
    {


    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try using the built-in Test Runner UI to set-up your Test Assembly Folder and first Test script.

Use Window -> Test Runner -> EditMode -> "Create Test Assembly Folder", and once you navigate to the new Test Assembly Folder, use the Create Test Script in current folder button.

In particular, your Tests.asmdef is missing an "Editor" include compared to the default setup (in Unity 2018.1).

{
    "name": "Tests",
    "optionalUnityReferences": [
        "TestAssemblies"
    ],
    "includePlatforms": [
        "Editor"
    ]
}

You should not have to do anything manually in Visual Studio project for the purpose of setting up your tests.

Note that when my Assembly File is set to "Any Platform" as follows (like in your question):

{
    "name": "Tests",
    "optionalUnityReferences": [
        "TestAssemblies"
    ]
}

My tests do not show up in the Test Runner window.

When my Assembly File is explicitly set to include "Editor" platform only (as per my previous example), my tests show up correctly in the Test Runner window.

(This behaviour seems a bit counterintuitive to me.)


You also need to set up an Assembly Definition for your scripts. Under your Scripts, folder, create an assembly definition file MyScriptAssembly.asmdef (using the Unity menu Assets -> Create -> Assembly Definition or manually):

{
    "name": "MyScriptAssembly"
}

Then, make sure your Tests.asmdef reference your script Assembly:

{
    "name": "Tests",
    "references": [
        "MyScriptAssembly"
    ],
    "optionalUnityReferences": [
        "TestAssemblies"
    ],
    "includePlatforms": [
        "Editor"
    ],
    "excludePlatforms": [],
    "allowUnsafeCode": false
}

You can also set this up in the Unity Editor inspector window. See 'References' in the Inspector when selecting a .asmdef file:

assembly definition inspector window

(For more details, see Unity's documentation on assembly definition files)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...