Add tests to your Unity Package Manager (UPM) package to verify its behavior and identify issues during development.
Adding tests is relevant if you’re creating a package that contains scripts or logic you want to validate as your package evolves. Including tests helps you catch bugs, improve reliability, and support safe reuse of the package across projects. Unity’s test framework lets you organize and run these tests from within the Unity Editor or as part of your continuous integration (CI) pipeline.
When you create your package with the Package Manager window, Package Manager creates Tests/Editor and Tests/Runtime folders with C# and assembly definition (.asmdef) files in each, to help you get started.
To complete the test files for your package:
Tests/Editor or Tests/Runtime folder for the tests you want to complete.Tests folder, you can optionally create additional asmdef files for the code you want to put in separate assemblies. If you choose to create additional assembly definitions, refer to Assembly definition files for tests.
Use the Test FrameworkThe Test Framework package (formerly called the Test Runner) is a Unity tool that tests your code in both Edit mode and Play mode, and also on target platforms such as Standalone, Android, or iOS. More info
See in Glossary to create or edit your assembly definition files. For more information, refer to Create a test assembly.
When you create your package with the Package Manager window, Package Manager creates assembly definition files (.asmdef) in Tests/Editor and Tests/Runtime to accompany the test scripts it created in those folders.
The sample assembly definition files have the required properties and values. If you want to add optional properties and values, use the Inspector window to edit the assembly definitions. For more information, refer to Assembly Definition properties reference.
You can also edit .asmdef files directly, but this method is more error prone. For more information, refer to Assembly Definition File Format reference.
The .asmdef file in the Tests/Editor folder looks like this:
{
"name": "MyCompany.MyPackage.Editor.Tests",
"references": [
"MyPackage.Editor",
"MyPackage"
],
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": []
}
The .asmdef file in the Tests/Runtime folder looks like this:
{
"name": "MyCompany.MyPackage.Tests",
"references": [
"MyPackage"
],
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [],
"excludePlatforms": []
}
You might need to enable tests depending on the folder where you’re developing your package.
If you’re developing your package in the Packages folder of your project, you don’t need to explicitly enable tests.
However, if you’re developing a package outside the project’s Packages folder, you need to manually enable its tests. To do this, add the testables property to your project manifest and list the packages whose tests you want to run. If you also want to run tests from packages they depend on, include those indirect dependencies as well. For example:
{
"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.