Created
May 2, 2023 19:13
-
-
Save CodeSmile-0000011110110111/09f1d508859136b9aa3a1023f5440664 to your computer and use it in GitHub Desktop.
Speed up Unity TestRunner running tests
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
// This work is Public Domain (CC0) - "No Rights Reserved" | |
// License: https://creativecommons.org/publicdomain/zero/1.0/ | |
using System.Reflection; | |
using UnityEditor; | |
using UnityEditor.TestTools.TestRunner.Api; | |
using UnityEngine; | |
namespace CodeSmile.Tests.Utilities | |
{ | |
[InitializeOnLoad] | |
public class FasterTests | |
{ | |
static FasterTests() => ScriptableObject.CreateInstance<TestRunnerApi>().RegisterCallbacks(new Callbacks()); | |
private class Callbacks : ICallbacks | |
{ | |
private const string ApplicationIdleTimeKey = "ApplicationIdleTime"; | |
private const string InteractionModeKey = "InteractionMode"; | |
private int m_UserApplicationIdleTime; | |
private int m_UserInteractionMode; | |
private static void UpdateInteractionModeSettings() | |
{ | |
const string UpdateInteractionModeMethodName = "UpdateInteractionModeSettings"; | |
var bindingFlags = BindingFlags.Static | BindingFlags.NonPublic; | |
var type = typeof(EditorApplication); | |
var method = type.GetMethod(UpdateInteractionModeMethodName, bindingFlags); | |
method.Invoke(null, null); | |
} | |
public void RunStarted(ITestAdaptor testsToRun) => SpeedUpTestRunner(); | |
public void RunFinished(ITestResultAdaptor result) => ResetInteractionMode(); | |
public void TestStarted(ITestAdaptor test) {} | |
public void TestFinished(ITestResultAdaptor result) {} | |
private void SpeedUpTestRunner() | |
{ | |
Debug.Log("Set Interaction Mode to 'No Throttling' during tests."); | |
SetInteractionModeToNoThrottling(); | |
UpdateInteractionModeSettings(); | |
} | |
private void ResetInteractionMode() | |
{ | |
Debug.Log("Reset Interaction Mode to user settings."); | |
SetInteractionModeToUserSetting(); | |
UpdateInteractionModeSettings(); | |
} | |
private void SetInteractionModeToNoThrottling() | |
{ | |
m_UserApplicationIdleTime = EditorPrefs.GetInt(ApplicationIdleTimeKey); | |
m_UserInteractionMode = EditorPrefs.GetInt(InteractionModeKey); | |
EditorPrefs.SetInt(ApplicationIdleTimeKey, 0); | |
EditorPrefs.SetInt(InteractionModeKey, 1); | |
} | |
private void SetInteractionModeToUserSetting() | |
{ | |
EditorPrefs.SetInt(ApplicationIdleTimeKey, m_UserApplicationIdleTime); | |
EditorPrefs.SetInt(InteractionModeKey, m_UserInteractionMode); | |
} | |
} | |
} | |
} |
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
{ | |
"name": "TestUtilities", | |
"rootNamespace": "", | |
"references": [ | |
"UnityEngine.TestRunner", | |
"UnityEditor.TestRunner" | |
], | |
"includePlatforms": [], | |
"excludePlatforms": [], | |
"allowUnsafeCode": false, | |
"overrideReferences": true, | |
"precompiledReferences": [ | |
"nunit.framework.dll" | |
], | |
"autoReferenced": false, | |
"defineConstraints": [ | |
"UNITY_INCLUDE_TESTS" | |
], | |
"versionDefines": [], | |
"noEngineReferences": false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What this does:
It has been proven to speed up TestRunner by 20-40% in my projects within the Unity Editor. It also speeds up running tests from within Rider IDE but at the time of this writing only when using Unity 2023.1 (beta) or higher.
Refer to this thread for more details:
https://forum.unity.com/threads/unity-test-runner-is-much-slower-if-unity-is-minimized-or-the-screen-is-locked.702209/