Version: 2022.3
言語: 日本語
パッケージレイアウト
パッケージのサンプルの作成

パッケージにテストを追加する

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:

  1. C# テストファイルを作成し、Tests フォルダーに置きます
  2. テスト用の asmdef ファイルを作成します
  3. パッケージのテストを有効にします

テストファイルの場所

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 文字列 ファイル拡張子を除いたアセンブリの名前。
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.

パッケージレイアウト
パッケージのサンプルの作成