As with any kind of development, it’s good practice to add tests to your package. There are three things you must do to set up tests on your package:
You can add your test files to the Tests folder of your package in the Editor
and Runtime
subfolders. For example, a simple package with tests might look something like this:
<package-root>
├── package.json
├── Editor
│ ├── <company-name>.<package-name>.Editor.asmdef
│ └── EditorExample.cs
├── Runtime
│ ├── <company-name>.<package-name>.asmdef
│ └── RuntimeExample.cs
└── Tests
├── Editor
│ ├── <company-name>.<package-name>.Editor.Tests.asmdef
│ └── EditorExampleTest.cs
└── Runtime
├── <company-name>.<package-name>.Tests.asmdef
└── RuntimeExampleTest.cs
Each of those subfolders must contain an .asmdef
file, which provides references to the Editor and Runtime assemblies. The assembly definition files also provide a reference to the test assembly files. For more information, refer to Assembly definition files for tests.
可以直接编辑程序集定义文件。需要确保添加以下引用:
属性 | 类型 | 描述 |
---|---|---|
name | String | 程序集的名称(不含文件扩展名)。 |
references | 字符串数组 | References to the Editor and Runtime assemblies. Assembly definition files require different references, depending on the test type: - For Editor tests, add a reference to the package’s Editor and Runtime assemblies. - For Runtime tests, add a reference to the package’s Runtime assembly only. |
optionalUnityReferences | 字符串数组 | This list of Unity references must include "TestAssemblies" to mark the assembly as a test assembly. This adds references to the nunit.framework.dll and UnityEngine.TestRunner.dll libraries to the Assembly Definition. |
includePlatforms | 字符串数组 | For the Editor test, this list of platforms must include the "Editor" platform. |
Tip: You can also edit the assembly definition files in the Inspector. Refer to Assembly Definitions for more information.
The editor test .asmdef
file looks like this:
{
"name": "MyCompany.MyPackage.Editor.Tests",
"references": [
"MyPackage.Editor",
"MyPackage"
],
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": []
}
The runtime test .asmdef
file looks like this:
{
"name": "MyCompany.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"]
}
This example adds tests for the com.unity.some-package and com.unity.other-package packages in Unity’s Test Framework package.
Note: You might need to import the package again, because the test framework doesn’t always immediately pick up changes to the testables
attribute.
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.