Skip to content

feat: test utilities script including Playmode test helpers #484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Assets/BossRoom/Scripts/Editor/SceneBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class SceneBootstrapper
const string k_LoadBootstrapSceneOnPlay = "Boss Room/Load Bootstrap Scene On Play";
const string k_DoNotLoadBootstrapSceneOnPlay = "Boss Room/Don't Load Bootstrap Scene On Play";

const string k_TestRunnerSceneName = "InitTestScene";

static bool s_StoppingAndStarting;

static string BootstrapScene
Expand Down Expand Up @@ -99,6 +101,11 @@ static void DisableDoNotLoadBootstrapSceneOnPlay()

static void EditorApplicationOnplayModeStateChanged(PlayModeStateChange obj)
{
if (IsTestRunnerActive())
{
return;
}

if (!LoadBootstrapScene)
{
return;
Expand Down Expand Up @@ -157,5 +164,10 @@ static void EditorApplicationOnplayModeStateChanged(PlayModeStateChange obj)
}
}
}

static bool IsTestRunnerActive()
{
return EditorSceneManager.GetActiveScene().name.StartsWith(k_TestRunnerSceneName);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
using UnityEngine;
using UnityEditor;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;

class EditorExampleTest
namespace Unity.Multiplayer.Samples.Utilities.Tests.Editor
{

[Test]
public void EditorSampleTestSimplePasses()
class EditorExampleTest
{
// Use the Assert class to test conditions.
}
[Test]
public void EditorSampleTestSimplePasses()
{
// Use the Assert class to test conditions.
}

// A UnityTest behaves like a coroutine in PlayMode
// and allows you to yield null to skip a frame in EditMode
[UnityTest]
public IEnumerator EditorSampleTestWithEnumeratorPasses()
{
// Use the Assert class to test conditions.
// yield to skip a frame
yield return null;
// A UnityTest behaves like a coroutine in PlayMode
// and allows you to yield null to skip a frame in EditMode
[UnityTest]
public IEnumerator EditorSampleTestWithEnumeratorPasses()
{
// Use the Assert class to test conditions.
// yield to skip a frame
yield return null;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Unity.Multiplayer.Samples.Utilities.Tests.Editor",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using NUnit.Framework;
using System.Collections;

namespace Tests
namespace Unity.Multiplayer.Samples.Utilities.Tests.Runtime
{
public class RuntimeExampleTest
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "Unity.Multiplayer.Samples.Utilities.Tests.Runtime",
"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
}
18 changes: 0 additions & 18 deletions Packages/com.unity.multiplayer.samples.coop/Utilities/Test.cs

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NUnit.Framework;
using Unity.Netcode;
using UnityEngine.SceneManagement;

namespace Unity.Multiplayer.Samples.Utilities
{
public abstract class TestUtilities
{
const float k_MaxSceneLoadDuration = 10f;

/// <summary>
/// Helper wrapper method for asserting the completion of a network scene load to be used inside Playmode tests.
/// A scene is either loaded successfully, or the loading process has timed out and will throw an exception.
/// </summary>
/// <param name="sceneName"> Name of scene </param>
/// <param name="networkSceneManager"> NetworkSceneManager instance </param>
/// <returns> IEnumerator to track scene load process </returns>
public static IEnumerator AssertIsNetworkSceneLoaded(string sceneName, NetworkSceneManager networkSceneManager)
{
Assert.That(networkSceneManager != null, "NetworkSceneManager instance is null!");

yield return new WaitForNetworkSceneLoad(sceneName, networkSceneManager);
}

/// <summary>
/// Custom IEnumerator class to validate the loading of a Scene by name. If a scene load lasts longer than
/// k_MaxSceneLoadDuration it is considered a timeout.
/// </summary>
public class WaitForSceneLoad : CustomYieldInstruction
{
string m_SceneName;

float m_LoadSceneStart;

float m_MaxLoadDuration;

public override bool keepWaiting
{
get
{
var scene = SceneManager.GetSceneByName(m_SceneName);

var isSceneLoaded = scene.IsValid() && scene.isLoaded;

if (Time.time - m_LoadSceneStart >= m_MaxLoadDuration)
{
throw new Exception($"Timeout for scene load for scene name {m_SceneName}");
}

return !isSceneLoaded;
}
}

public WaitForSceneLoad(string sceneName, float maxLoadDuration = k_MaxSceneLoadDuration)
{
m_LoadSceneStart = Time.time;
m_SceneName = sceneName;
m_MaxLoadDuration = maxLoadDuration;
}
}

/// <summary>
/// Custom IEnumerator class to validate the loading of a Scene through Netcode for GameObjects by name.
/// If a scene load lasts longer than k_MaxSceneLoadDuration it is considered a timeout.
/// </summary>
class WaitForNetworkSceneLoad : CustomYieldInstruction
{
string m_SceneName;

float m_LoadSceneStart;

float m_MaxLoadDuration;

bool m_IsNetworkSceneLoaded;

NetworkSceneManager m_NetworkSceneManager;

public override bool keepWaiting
{
get
{
if (Time.time - m_LoadSceneStart >= m_MaxLoadDuration)
{
m_NetworkSceneManager.OnLoadEventCompleted -= ConfirmSceneLoad;

throw new Exception($"Timeout for network scene load for scene name {m_SceneName}");
}

return !m_IsNetworkSceneLoaded;
}
}

public WaitForNetworkSceneLoad(string sceneName, NetworkSceneManager networkSceneManager, float maxLoadDuration = k_MaxSceneLoadDuration)
{
m_LoadSceneStart = Time.time;
m_SceneName = sceneName;
m_MaxLoadDuration = maxLoadDuration;

m_NetworkSceneManager = networkSceneManager;

m_NetworkSceneManager.OnLoadEventCompleted += ConfirmSceneLoad;
}

void ConfirmSceneLoad(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut)
{
if (sceneName == m_SceneName)
{
m_IsNetworkSceneLoaded = true;

m_NetworkSceneManager.OnLoadEventCompleted -= ConfirmSceneLoad;
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "Unity.Multiplayer.Samples.Utilities.Testing",
"rootNamespace": "",
"references": [
"GUID:1491147abca9d7d4bb7105af628b223e"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.