Add tests to a package
Verify behavior with tests during development.
Read time 3 minutesLast updated 14 days ago
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:
- Create the C# test files and put them under the Tests folder.
- Create asmdef files for your tests.
- Enable tests for your package.
Location of test files
You can add your test files to the Tests folder of your package in the and subfolders. For example, a simple package with tests might look something like this:
EditorRuntime<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 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.
.asmdefAssembly definition files for tests
Use the Test Framework package to create or edit your assembly definition files. For more information, refer to Creating test assemblies.
Although you can choose to edit assembly definition files directly, you need to make sure to add the following references:
Attribute | Type | Description |
|---|---|---|
| name | String | The name of the assembly without the file extension. |
| references | Array of Strings | 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 | Array of Strings | This list of Unity references must include |
| includePlatforms | Array of Strings | For the Editor test, this list of platforms must include the |
Editor file example
The editor test file looks like this:
.asmdef{ "name": "MyCompany.MyPackage.Editor.Tests", "references": [ "MyPackage.Editor", "MyPackage" ], "optionalUnityReferences": [ "TestAssemblies" ], "includePlatforms": [ "Editor" ], "excludePlatforms": []}
Runtime file example
The runtime test file looks like this:
.asmdef{ "name": "MyCompany.MyPackage.Tests", "references": [ "MyPackage" ], "optionalUnityReferences": [ "TestAssemblies" ], "includePlatforms": [], "excludePlatforms": []}
Enabling tests for a package
For embedded packages, you don't need to explicitly enable tests because embedded packages are in development.
However, for other types of dependencies, you need to add the testables attribute to the Project manifest and add the names of packages that have tests you want to run. That includes direct and indirect dependencies of the Project. 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.