はなちるのマイノート

Unityをメインとした技術ブログ。自分らしくまったりやっていきたいと思いますー!

【Unity】UTFで「UNITY_INCLUDE_TESTS」があると通常のビルドにはテストコードが含まれないようになっているらしい

はじめに

Unity Test Framework(UTF)PlayModeを利用していた際、実機ビルドしたときにコードが含まれちゃうんじゃないかと不安になってしまいました。
docs.unity3d.com

対応プラットフォーム

結果的に含まないっぽいですが、一応実験をしてみました。

対処法

UNITY_INCLUDE_TESTS

こちらを対象asmdefDefine Constraintsに含めてあげればOKです。

Define ConstraintsにUNITY_INCLUDE_TESTSを加える


公式フォーラムにて以下のように言及されています。

Hey. The purpose of UNITY_INCLUDE_TESTS is to signal whether an assembly contains tests. The constraint is added to the asmdef for the test assembly automatically when it is created. It is used by the build system to ensure that your tests does not get included in the player build, when building normally.

// DeepL翻訳
UNITY_INCLUDE_TESTSの目的は、アセンブリにテストが含まれているかどうかを通知することです。この制約は、テストアセンブリが作成されたときに、自動的にテストアセンブリのasmdefに追加されます。この制約は、ビルドシステムによって使用され、通常のビルドでは、テストがプレーヤのビルドに含まれないことを保証します。

Info on UNITY_INCLUDE_TESTS define - Unity Forum

実験

疑り深いので、実際に調べてみます。

またUNITY_INCLUDE_TESTSにテストコード以外があったらどうなるのかも面白そうなので調べてみました。

Assets/Scripts/PlayerModeTests
|-- PlayModeNormalCode.cs
|-- PlayModeTest.cs
|-- PlayModeTests.asmdef

PlayModeNormalCode.csMonoBehaviorを継承したもの、PlayModeTestはテスト用スクリプトになります。

PlayModeTests

StandaloneOSX向けにBuild SettingsよりBuildを実行しました(Mono)

UNITY_INCLUDE_TESTSあり

出力された.appの中身を見てみても、アセンブリは含まれていないようでした。

中身

テストコードだけ取り除かれるとかではなく、アセンブリ自体含まれないようですね。

UNITY_INCLUDE_TESTSなし

そもそもビルドできないっぽいです。

ArgumentException: The Assembly UnityEngine.TestRunner is referenced by PlayModeTests ('Library/PlayerScriptAssemblies/PlayModeTests.dll'). But the dll is not allowed to be included or could not be found.
UnityEditor.AssemblyHelper.AddReferencedAssembliesRecurse (System.String assemblyPath, System.Collections.Generic.List`1[T] alreadyFoundAssemblies, System.String allAssemblyPaths, System.String foldersToSearch, System.Collections.Generic.Dictionary`2[TKey,TValue] cache, UnityEditor.BuildTarget target) (at /Users/bokken/buildslave/unity/build/Editor/Mono/AssemblyHelper.cs:160)
UnityEditor.AssemblyHelper.FindAssembliesReferencedBy (System.String paths, System.String foldersToSearch, UnityEditor.BuildTarget target) (at /Users/bokken/buildslave/unity/build/Editor/Mono/AssemblyHelper.cs:201)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&) (at /Users/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:189)

エラーが出てくる