问题描述
我创建了以下结构:
├── Assets
├── Scenes
├── Scripts
│ └── MyExample.cs
├── Tests
│ ├── MyExampleTest.cs
│ └── Tests.asmdef
现在,当我单击全部运行"时,在Unity的Test Runner窗口中,出现以下错误:
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来设置您的Test Assembly文件夹和第一个Test脚本.
Try using the built-in Test Runner UI to set-up your Test Assembly Folder and first Test script.
使用Window -> Test Runner -> EditMode -> "Create Test Assembly Folder"
,然后导航到新的测试部件文件夹",然后使用Create Test Script in current folder
按钮.
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.
请注意,当我的Assembly File设置为"Any Platform"时,如下所示(如您的问题所示):
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 Editor检查器窗口中进行设置.选择.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中设置单元测试并修复缺少的程序集引用错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!