Skip to content

Instantly share code, notes, and snippets.

@CodeSmile-0000011110110111
Created May 2, 2023 19:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeSmile-0000011110110111/09f1d508859136b9aa3a1023f5440664 to your computer and use it in GitHub Desktop.
Save CodeSmile-0000011110110111/09f1d508859136b9aa3a1023f5440664 to your computer and use it in GitHub Desktop.
Speed up Unity TestRunner running tests
// 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);
}
}
}
}
{
"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
}
@CodeSmile-0000011110110111
Copy link
Author

What this does:

  • Set "Interaction Mode" (Preferences => General) to "No Throttling" while tests are running.
  • Reset to user's settings after test run completes.

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment