コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Unity Test Runner をスクリプトから実行する方法

概要

using UnityEditor;
using UnityEditor.TestTools.TestRunner.Api;
using UnityEngine;

internal static class Example
{
    [MenuItem( "Tools/Run Play Mode" )]
    private static void RunPlayMode()
    {
        RunTest( TestMode.PlayMode );
    }
    
    [MenuItem( "Tools/Run Edit Mode" )]
    private static void RunEditMode()
    {
        RunTest( TestMode.EditMode );
    }

    private static void RunTest( TestMode testMode )
    {
        var testRunnerApi = ScriptableObject.CreateInstance<TestRunnerApi>();

        // Filter 構造体でテスト対象の絞り込みができる
        var filter = new Filter
        {
            testMode       = testMode,
            testNames      = null,
            groupNames     = null,
            categoryNames  = null,
            assemblyNames  = null,
            targetPlatform = null,
        };

        // Filter 構造体は引数に複数渡すことができる
        testRunnerApi.Execute( new ExecutionSettings( filter ) );
        //testRunnerApi.Execute( new ExecutionSettings( filter, filter ) );
    }
}

参考サイト様