More Unity Shortcut Keys
The Unity Test Tools don't have shortcut keys to run your test suites. All the interesting methods are private. The following code adds shortcuts for running both unit and integration tests.
NOTE: This is a followup article to article Unity Shortcuts via Reflection
using UnityEditor;
using UnityEngine;
using UnityTest;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace SDD.Editor {
/// <summary>
/// Salty Dog Digital Unity IDE shortcuts
/// </summary>
public class Shortcuts : MonoBehaviour {
/// <summary>
/// Auto running unit tests after each recompile getting you down? Add a
/// shortcut.
/// </summary>
[MenuItem("Unity Test Tools/Run Unit Tests %u")]
static void RunUnitTests() {
EditorApplication.ExecuteMenuItem("Unity Test Tools/Unit Test Runner");
System.Type type = typeof(UnitTestView);
FieldInfo info = type.GetField("s_Instance", BindingFlags.NonPublic | BindingFlags.Static);
UnitTestView unitTestView = (UnitTestView) info.GetValue(null);
// find overloaded method "RunTests()"
MethodInfo methodInfo = unitTestView.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(method => method.Name == "RunTests" && method.GetParameters().Count() == 0);
methodInfo.Invoke(unitTestView, null);
}
[MenuItem("Unity Test Tools/Run Unit Tests (Selected) %#u")]
static void RunUnitTestsSelected() {
EditorApplication.ExecuteMenuItem("Unity Test Tools/Unit Test Runner");
System.Type type = typeof(UnitTestView);
FieldInfo info = type.GetField("s_Instance", BindingFlags.NonPublic | BindingFlags.Static);
UnitTestView unitTestView = (UnitTestView) info.GetValue(null);
info = type.GetField("m_TestLines", BindingFlags.NonPublic | BindingFlags.Instance);
UnitTestRendererLine m_TestLines = (UnitTestRendererLine) info.GetValue(unitTestView);
m_TestLines.RunSelectedTests();
}
[MenuItem("Unity Test Tools/Run Unit Tests (Failed) %&u")]
static void RunUnitTestsFailed() {
EditorApplication.ExecuteMenuItem("Unity Test Tools/Unit Test Runner");
System.Type type = typeof(UnitTestView);
FieldInfo info = type.GetField("s_Instance", BindingFlags.NonPublic | BindingFlags.Static);
UnitTestView unitTestView = (UnitTestView) info.GetValue(null);
info = type.GetField("m_TestLines", BindingFlags.NonPublic | BindingFlags.Instance);
UnitTestRendererLine m_TestLines = (UnitTestRendererLine) info.GetValue(unitTestView);
info = type.GetField("m_ResultList", BindingFlags.NonPublic | BindingFlags.Instance);
List<UnitTestResult> m_ResultList = (List<UnitTestResult>) info.GetValue(unitTestView);
Debug.Log(m_ResultList);
m_TestLines.RunTests(m_ResultList.Where(result => result.IsFailure || result.IsError).Select(l => l.FullName).ToArray());
}
[MenuItem("Unity Test Tools/Run Integration Tests %t")]
static void RunIntegrationTests() {
EditorApplication.ExecuteMenuItem("Unity Test Tools/Integration Test Runner");
System.Type type = typeof(IntegrationTestsRunnerWindow);
FieldInfo info = type.GetField("s_Instance", BindingFlags.NonPublic | BindingFlags.Static);
IntegrationTestsRunnerWindow testRunner = (IntegrationTestsRunnerWindow) info.GetValue(null);
MethodInfo methodInfo = testRunner.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(method => method.Name == "RunTests");
object[] objArray = new object[1];
objArray[0] = TestComponent.FindAllTestsOnScene().Cast<ITestComponent>().ToList();
methodInfo.Invoke(testRunner, objArray);
}
[MenuItem("Unity Test Tools/Run Integration Tests (Selected) %#t")]
static void RunIntegrationTestsSelected() {
EditorApplication.ExecuteMenuItem("Unity Test Tools/Integration Test Runner");
System.Type type = typeof(IntegrationTestsRunnerWindow);
FieldInfo info = type.GetField("s_Instance", BindingFlags.NonPublic | BindingFlags.Static);
IntegrationTestsRunnerWindow testRunner = (IntegrationTestsRunnerWindow) info.GetValue(null);
MethodInfo methodInfo = testRunner.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(method => method.Name == "RunTests");
object[] objArray = new object[1];
objArray[0] = Selection.gameObjects.Select(t => t.GetComponent(typeof(TestComponent))).Cast<ITestComponent>().ToList();
methodInfo.Invoke(testRunner, objArray);
}
}
}
Dynamically Ignoring Unity Unit Tests the Fast Way
Unity Shortcuts via Reflection