与任何类型的开发一样,向包添加测试是一种好习惯。必须完成三个步骤来设置对包的测试:
可以将测试文件添加到包的 Tests 文件夹的 Editor 和 Runtime 子文件夹中。例如,一个包含测试的简单包可能如下所示:
MyPackage
├── package.json
├── Editor
│ ├── MyPackage.Editor.asmdef
│ └── EditorExample.cs
├── Runtime
│ ├── MyPackage.Runtime.asmdef
│ └── RuntimeExample.cs
└── Tests
├── Editor
│ ├── MyPackage.EditorTests.asmdef
│ └── EditorExampleTest.cs
└── Runtime
├── MyPackage.RuntimeTests.asmdef
└── RuntimeExampleTest.cs
每个子文件夹必须包含一个 .asmdef
文件,该文件提供对 Editor 和 Runtime 程序集的引用。程序集定义文件还提供对测试程序集文件的引用。有关更多信息,请参阅用于测试的程序集定义文件。
可以直接编辑程序集定义文件。需要确保添加以下引用:
属性 | 类型 | 描述 |
---|---|---|
name | String | 程序集的名称(不含文件扩展名)。 |
references | 字符串数组 | 对 Editor 和 Runtime 程序集的引用。程序集定义文件需要不同的引用,具体取决于它是用于 Editor 测试还是 Runtime 测试: - 对于 Editor 测试,请添加对包的 Editor 和 Runtime 程序集的引用。 - 对于 Runtime 测试,仅添加对包的 Runtime 程序集的引用。 |
optionalUnityReferences | 字符串数组 | 该 Unity 引用列表必须包含“TestAssemblies”才能将该程序集标记为测试程序集。这样会在程序集定义中添加对 unit.framework.dll 和 UnityEngine.TestRunner.dll 库的引用。 |
includePlatforms | 字符串数组 | 对于 Editor 测试,此平台列表必须包含“Editor”平台。 |
提示:还可以在 Inspector 中编辑程序集定义文件。请参阅程序集定义以了解更多信息。
Editor 测试 .asmdef
文件应如下所示:
{
"name": "MyPackage.Editor.Tests",
"references": [
"MyPackage.Editor",
"MyPackage"
],
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": []
}
运行时测试 .asmdef
文件应如下所示:
{
"name": "MyPackage.Tests",
"references": [
"MyPackage"
],
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [],
"excludePlatforms": []
}
对于嵌入式包,不需要显式启用测试,因为嵌入式包正在开发中。
但是,对于其他类型的依赖项,则需要向项目清单添加 testables 属性,并添加包含待运行测试的包名称。这包括项目的直接依赖项和间接依赖项。例如:
{
"dependencies": {
"com.unity.some-package": "1.0.0",
"com.unity.other-package": "2.0.0",
"com.unity.yet-another-package": "3.0.0",
},
"testables": ["com.unity.some-package", "com.unity.other-package"]
}
此示例为 Unity 的 Test Framework 包中的 com.unity.some-package 和 com.unity.other-package 包添加测试。
注意:可能需要重新导入包,因为 Test Framework 并不总是能立即获取对 testables 属性的更改。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.