Last active
August 10, 2018 23:06
-
-
Save robertwahler/ea0de59a5b525ffaa87c to your computer and use it in GitHub Desktop.
Unity Shortcuts via Reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment