如何在 Unity 中设置单元测试并修复缺少的程序集引用错误? [英] How to set up unit tests in Unity and fix missing assembly reference error?

查看:26
本文介绍了如何在 Unity 中设置单元测试并修复缺少的程序集引用错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下结构:

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

现在,当我在 Unity 的 Test Runner 窗口中单击 Run All 时,出现以下错误:

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?

在 Visual Studio 中,我有两个项目:

In Visual Studio I have two projects:

  • Assembly-CSharp(包含 src)

  • Assembly-CSharp (containing src)

测试(包含测试)

我在第二个项目中添加了 Assembly-CSharp 作为参考.Visual Studio 能够构建没有错误的解决方案.

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

有谁知道如何为 Unity 项目正确设置 UnitTest 回归?

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

这是Tests.asmdef

This is Tests.asmdef

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

MyExampleTest.cs

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

MyExample.cs

namespace abc
{
    public class Example
    {


    }
}

推荐答案

尝试使用内置的 Test Runner UI 来设置您的测试程序集文件夹和第一个测试脚本.

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

使用 Window ->测试运行器 ->编辑模式 ->创建测试程序集文件夹",导航到新的测试程序集文件夹后,使用在当前文件夹中创建测试脚本按钮.

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.

特别是,与默认设置(在 Unity 2018.1 中)相比,您的 Tests.asmdef 缺少编辑器"包含.

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"
    ]
}

您不必为了设置测试而在 Visual Studio 项目中手动执行任何操作.

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.)

您还需要为脚本设置程序集定义.在您的 Scripts 文件夹下,创建一个程序集定义文件 MyScriptAssembly.asmdef(使用 Unity 菜单 Assets -> Create -> Assembly Definition或手动):

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"
}

然后,确保您的 Tests.asmdef 引用您的脚本程序集:

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

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

您也可以在 Unity 编辑器检查器窗口中进行设置.选择 .asmdef 文件时,请参阅检查器中的参考":

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

(有关详细信息,请参阅 Unity 的程序集定义文件文档)

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

这篇关于如何在 Unity 中设置单元测试并修复缺少的程序集引用错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆