Unity Shortcuts via Reflection
Auto running unit tests after each recompile getting you down? Take control, reflect a bit, and add a shortcut.
Auto running unit tests is a great idea...as long as it happens in a separate process or in the cloud. The Unity unit test runner is a private mouse only affair and it will take focus until all the tests are complete. The following editor code will add a menu item and hotkey to load and run unit tests manually.
NOTE: There is a followup to this article with additional shortcut keys More Unity Shortcut Keys
using UnityEditor;
using UnityEngine;
using UnityTest;
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 to the private method "RunTests".
/// </summary>
[MenuItem("Unity Test Tools/Run Unit Tests %u")]
static void RunUnitTests() {
// make sure the runner is open
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);
}
}
}
This is where someone tells me there already is a unit test hotkey.
More Unity Shortcut Keys
National Dog Day - 2015