From 472135c910ee59c998b0132fd44ac25bdc6af677 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Sun, 22 Oct 2023 22:44:39 +0900 Subject: [PATCH 1/2] Refactor: create scene --- Runtime/Attributes/CreateSceneAttribute.cs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/Runtime/Attributes/CreateSceneAttribute.cs b/Runtime/Attributes/CreateSceneAttribute.cs index f547101..6492b03 100644 --- a/Runtime/Attributes/CreateSceneAttribute.cs +++ b/Runtime/Attributes/CreateSceneAttribute.cs @@ -44,26 +44,17 @@ public IEnumerator BeforeTest(ITest test) { _newSceneName = $"Scene of {TestContext.CurrentContext.Test.FullName}"; - if (Application.isEditor) + if (Application.isEditor && !Application.isPlaying) { #if UNITY_EDITOR - if (Application.isPlaying) - { - // Play Mode tests running in Editor - var scene = SceneManager.CreateScene(_newSceneName); - SceneManager.SetActiveScene(scene); - } - else - { - // Edit Mode tests - var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene); - scene.name = _newSceneName; - } + // Edit Mode tests + var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene); + scene.name = _newSceneName; #endif } else { - // Play Mode tests running on Player + // Play Mode tests var scene = SceneManager.CreateScene(_newSceneName); SceneManager.SetActiveScene(scene); } From 4cbd683bb6bd6673068f766b910f765224ac3b01 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Sun, 22 Oct 2023 22:59:14 +0900 Subject: [PATCH 2/2] Fix tests GameObject.Find finds objects in inactive scenes. --- .../Attributes/CreateSceneAttributeTest.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Tests/Runtime/Attributes/CreateSceneAttributeTest.cs b/Tests/Runtime/Attributes/CreateSceneAttributeTest.cs index df97cf4..87fbdb1 100644 --- a/Tests/Runtime/Attributes/CreateSceneAttributeTest.cs +++ b/Tests/Runtime/Attributes/CreateSceneAttributeTest.cs @@ -4,7 +4,6 @@ using System.Collections; using System.Threading.Tasks; using NUnit.Framework; -using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.TestTools; @@ -21,11 +20,8 @@ public void Attach_CreateNewSceneWithoutCameraAndLight() Assert.That(scene.name, Is.EqualTo( "Scene of TestHelper.Attributes.CreateSceneAttributeTest.Attach_CreateNewSceneWithoutCameraAndLight")); - var camera = GameObject.Find("Main Camera"); - Assert.That(camera, Is.Null); - - var light = GameObject.Find("Directional Light"); - Assert.That(light, Is.Null); + var rootGameObjects = scene.GetRootGameObjects(); // Note: GameObject.Find finds objects in inactive scenes + Assert.That(rootGameObjects, Is.Empty); } [Test] @@ -36,8 +32,9 @@ public void Attach_WithCamera_CreateNewSceneWithCamera() Assert.That(scene.name, Is.EqualTo( "Scene of TestHelper.Attributes.CreateSceneAttributeTest.Attach_WithCamera_CreateNewSceneWithCamera")); - var camera = GameObject.Find("Main Camera"); - Assert.That(camera, Is.Not.Null); + var rootGameObjects = scene.GetRootGameObjects(); // Note: GameObject.Find finds objects in inactive scenes + Assert.That(rootGameObjects, Has.Length.EqualTo(1)); + Assert.That(rootGameObjects[0].name, Is.EqualTo("Main Camera")); } [Test] @@ -48,8 +45,9 @@ public void Attach_WithLight_CreateNewSceneWithLight() Assert.That(scene.name, Is.EqualTo( "Scene of TestHelper.Attributes.CreateSceneAttributeTest.Attach_WithLight_CreateNewSceneWithLight")); - var light = GameObject.Find("Directional Light"); - Assert.That(light, Is.Not.Null); + var rootGameObjects = scene.GetRootGameObjects(); // Note: GameObject.Find finds objects in inactive scenes + Assert.That(rootGameObjects, Has.Length.EqualTo(1)); + Assert.That(rootGameObjects[0].name, Is.EqualTo("Directional Light")); } [Test]