はなちるのマイノート

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

【Unity】コマンドラインからUnity Test Frameworkのテストを実行する

はじめに

今回はコマンドラインからUnity Test Frameworkを実行する方法について紹介したいと思います。

やり方

まずはMacEditorModeのテストをなるべくシンプルに実行してみたいと思います。UnityのバージョンがUnity2023.2.0b17なので以下のように記述していますが、適宜文字列を変えてください。あとプロジェクトパスも適宜変更してください。

$ /Applications/Unity/Hub/Editor/2023.2.0b17/Unity.app/Contents/MacOS/Unity -runTests -batchmode -projectPath /Users/user/Sample2023.2.0b17  

無事に実行が終わると、対象プロジェクトのルートにTestResults-638397495464741480.xmlのようなファイルが生成されているはずです。冒頭の箇所を見ると2個のテストがpassedになっていることが確認できます。

<?xml version="1.0" encoding="utf-8"?>
<test-run id="2" testcasecount="2" result="Passed" total="2" passed="2" failed="0" inconclusive="0" skipped="0" asserts="0" engine-version="3.5.0.0" clr-version="4.0.30319.42000" start-time="2024-01-01 14:45:46Z" end-time="2024-01-01 14:45:46Z" duration="0.0170823">
  <test-suite type="TestSuite" id="1000" name="Sample2023.2.0b17" fullname="Sample2023.2.0b17" runstate="Runnable" testcasecount="2" result="Passed" start-time="2024-01-01 14:45:46Z" end-time="2024-01-01 14:45:46Z" duration="0.017082" total="2" passed="2" failed="0" inconclusive="0" skipped="0" asserts="0">
    <properties>
      <property name="platform" value="EditMode" />
    </properties>
    <test-suite type="Assembly" id="1004" name="Tests.dll" fullname="/Users/user/Sample2023.2.0b17/Library/ScriptAssemblies/Tests.dll" runstate="Runnable" testcasecount="2" result="Passed" start-time="2024-01-01 14:45:46Z" end-time="2024-01-01 14:45:46Z" duration="0.013907" total="2" passed="2" failed="0" inconclusive="0" skipped="0" asserts="0">
      <properties>
        <property name="_PID" value="27043" />
        <property name="_APPDOMAIN" value="Unity Child Domain" />
        <property name="platform" value="EditMode" />
      </properties>
      <test-suite type="TestFixture" id="1001" name="Sample" fullname="Sample" classname="Sample" runstate="Runnable" testcasecount="2" result="Passed" start-time="2024-01-01 14:45:46Z" end-time="2024-01-01 14:45:46Z" duration="0.010798" total="2" passed="2" failed="0" inconclusive="0" skipped="0" asserts="0">
        <properties>
          <property name="platform" value="EditMode" />
        </properties>
        <test-case id="1002" name="SampleSimplePasses" fullname="Sample.SampleSimplePasses" methodname="SampleSimplePasses" classname="Sample" runstate="Runnable" seed="493399175" result="Passed" start-time="2024-01-01 14:45:46Z" end-time="2024-01-01 14:45:46Z" duration="0.004329" asserts="0">
          <properties>
            <property name="platform" value="EditMode" />
            <property name="retryIteration" value="0" />
            <property name="repeatIteration" value="0" />
          </properties>
          <reason>
            <message><![CDATA[]]></message>
          </reason>
        </test-case>
        <test-case id="1003" name="SampleSimplePasses2" fullname="Sample.SampleSimplePasses2" methodname="SampleSimplePasses2" classname="Sample" runstate="Runnable" seed="2037186997" result="Passed" start-time="2024-01-01 14:45:46Z" end-time="2024-01-01 14:45:46Z" duration="0.000090" asserts="0">
          <properties>
            <property name="platform" value="EditMode" />
            <property name="retryIteration" value="0" />
            <property name="repeatIteration" value="0" />
          </properties>
          <reason>
            <message><![CDATA[]]></message>
          </reason>
          <output><![CDATA[Saving results to: /Users/user/Sample2023.2.0b17/TestResults-638397495464741480.xml
]]></output>
        </test-case>
      </test-suite>
    </test-suite>
  </test-suite>
</test-run>


よく使いそうなコマンドライン引数を紹介します。

-testResults

結果を記述したファイルを保存するパス。デフォルトはプロジェクトのルートフォルダに保存されます。

$ /Applications/Unity/Hub/Editor/2023.2.0b17/Unity.app/Contents/MacOS/Unity -runTests -batchmode -projectPath /Users/user/Sample2023.2.0b17 -testResults /Users/user/Desktop/results.xml

-testPlatform

テストを実行するプラットフォーム。引数には以下のどれかを指定します。

  • EditMode
  • PlayMode
  • BuildTarget列挙型の中の文字列一つ

BuildTargetの一覧(Unity2023.2の場合)

  • StandaloneOSX
  • StandaloneWindows
  • iOS
  • Android
  • StandaloneWindow64
  • WebGL
  • WSAPlayer
  • Standalone64
  • PS4
  • XboxOne
  • tvOS
  • Switch
  • LinuxHeadlessSimulation
  • PS5
  • Bratwurst
$ /Applications/Unity/Hub/Editor/2023.2.0b17/Unity.app/Contents/MacOS/Unity -runTests -batchmode -projectPath /Users/user/Sample2023.2.0b17 -testPlatform EditMode 

また明示的に指定しない場合は、Edit Modeで実行されます。

さいごに

もっと詳しく知りたい方は公式ドキュメントを参照してみてください。
docs.unity3d.com