やらなイカ?

たぶん、iOS/Androidアプリの開発・テスト関係。

Unity Automated QAのRecorded Testing機能 トラブルシュート

Unity Automated QA パッケージのRecorded Testing機能を試していて遭遇したトラブルと解消方法をまとめます。 バージョンは 0.2.0-preview.3 および 0.3.0-preview.8*1 で確認しています。

Recorded Testing機能とは、UnityエディタのPlay modeでuGUIの操作を記録したjsonファイルをPlay modeテストとして実行できる機能です。

Unity Automated QAパッケージ全体については先のエントリ参照。

www.nowsprinting.com

[12/26追記]v0.8.1対応の『Unity Automated QA攻略ガイド』をコミックマーケット99で頒布します。詳しくはこちらの記事を参照してください

www.nowsprinting.com

FileNotFoundException: Could not find config.json

テスト実行時、下記スタックトレースが出て失敗するケース。

FileNotFoundException: Could not find file "/Users/***/Library/Application Support/DefaultCompany/UnityTestFramework/config.json"
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <fb001e01371b4adca20013e0ac763896>:0)
(snip)
System.IO.File.ReadAllText (System.String path) (at <fb001e01371b4adca20013e0ac763896>:0)
Unity.RecordedTesting.Manager.GetDeviceFarmConfig (Unity.RecordedTesting.DeviceFarmOverrides dfOverrides) (at Library/PackageCache/com.unity.automated-testing@0.2.0-preview.3/RecordedTesting/Runtime/Manager.cs:31)
Unity.RecordedTesting.TestResults.RunStarted (NUnit.Framework.Interfaces.ITest testsToRun) (at Library/PackageCache/com.unity.automated-testing@0.2.0-preview.3/RecordedTesting/Runtime/TestTools/TestResults.cs:33)

これは、ローカル環境でテスト実行しようとしているのに、AWS Device Farmで実行するルートに入っているため発生しています。

Edit | Project Settings... を開き、Playerタブ、Other Settings下にあるScripting Define Symbolsの中に CLOUD_TEST_UTF シンボルが定義されていれば、それを削除すれば解消します。

Cloud Test Runnerウィンドウを開いて操作しようとした際にシンボルが設定されたままになっていたようです。

SetUp : System.Reflection.ReflectionTypeLoadException

テストのSetupですべてのアセンブリから型情報を取り出している処理があるのですが、型を含まないアセンブリでこの例外が出ています。

CanPlayToEnd (3.571s)
---
SetUp : System.Reflection.ReflectionTypeLoadException : Exception of type 'System.Reflection.ReflectionTypeLoadException' was thrown.
---
--SetUp
  at (wrapper managed-to-native) System.Reflection.Assembly.GetTypes(System.Reflection.Assembly,bool)
  at System.Reflection.Assembly.GetTypes () [0x00000] in <fb001e01371b4adca20013e0ac763896>:0
  at Unity.RecordedTesting.RecordedTesting.GetAllRecordedTests () [0x00023] in /Users/****/Documents/UnityTestWorkspace/Library/PackageCache/com.unity.automated-testing@0.2.0-preview.3/RecordedTesting/Runtime/RecordedTesting.cs:55
(snip)

遭遇したアセンブリMicrosoft.CodeAnalysis.Scripting で、これはImmediate Window (com.unity.immediate-window) パッケージの依存先になっています。 今回はImmediate WindowパッケージをPackage Managerから削除することで解消しましたが、Automated QA側で対応されるまでは他のアセンブリで発生する恐れもあります。

Scene couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded

Recorded Testingでは、再生しようとするjsonに記録されているSceneをロードしてから操作を開始します。 しかし対象のSceneがビルド設定のScenes in Buildに入っていない場合、この例外が発生します。

SetUp : System.NullReferenceException : Object reference not set to an instance of an object
SetUp : Unhandled log message: '[Error] Scene '***' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.
To add a scene to the build settings use the menu File->Build Settings...'. Use UnityEngine.TestTools.LogAssert.Expect

ビルドに含めるSceneであればScenes in Buildに含めればよいのですが、テスト専用やアセットバンドルに格納するものは別の手段でロードする必要があります。

Unityエディタでの実行であれば、SceneManager.LoadSceneAsyncでなくEditorSceneManager.LoadSceneAsyncInPlayModeを使うことで回避できます。

Standalone playerでの実行では、テスト実行前のビルドにフックするITestPlayerBuildModifierを実装して、ビルド対象Sceneを書き換えることで回避できます。

実装例はこちらを参考にしてください。 github.com

いずれの問題もForumで報告済みなので、そのうち対応されるはず。

参考

docs.unity3d.com

forum.unity.com

*1:本稿執筆時点でPackage Managerからはインポートできますが、マニュアルサイトには未反映