From dd810501f7d7ed6caff11a41ad15b18de9025f39 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Sun, 22 Oct 2023 21:39:41 +0900 Subject: [PATCH] Refactor: judge the running test mode --- Runtime/Attributes/CreateSceneAttribute.cs | 28 +++++++++++++------ Runtime/Attributes/LoadSceneAttribute.cs | 32 ++++++++++++++-------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/Runtime/Attributes/CreateSceneAttribute.cs b/Runtime/Attributes/CreateSceneAttribute.cs index abefff9..f547101 100644 --- a/Runtime/Attributes/CreateSceneAttribute.cs +++ b/Runtime/Attributes/CreateSceneAttribute.cs @@ -43,21 +43,31 @@ public CreateSceneAttribute(bool camera = false, bool light = false) public IEnumerator BeforeTest(ITest test) { _newSceneName = $"Scene of {TestContext.CurrentContext.Test.FullName}"; -#if UNITY_EDITOR - if (EditorApplication.isPlaying) + + if (Application.isEditor) { - var scene = SceneManager.CreateScene(_newSceneName); - SceneManager.SetActiveScene(scene); +#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; + } +#endif } else { - var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene); - scene.name = _newSceneName; - } -#else + // Play Mode tests running on Player var scene = SceneManager.CreateScene(_newSceneName); SceneManager.SetActiveScene(scene); -#endif + } + if (_camera) { var camera = new GameObject("Main Camera").AddComponent(); diff --git a/Runtime/Attributes/LoadSceneAttribute.cs b/Runtime/Attributes/LoadSceneAttribute.cs index 1b072b5..733ae30 100644 --- a/Runtime/Attributes/LoadSceneAttribute.cs +++ b/Runtime/Attributes/LoadSceneAttribute.cs @@ -26,7 +26,7 @@ namespace TestHelper.Attributes /// /// Notes: /// - Load scene run after OneTimeSetUp and before SetUp - /// - For the process of including a Scene not in "Scenes in Build" to a build for player, see: + /// - For the process of including a Scene not in "Scenes in Build" to a build for player, see: /// [AttributeUsage(AttributeTargets.Method)] public class LoadSceneAttribute : NUnitAttribute, IOuterUnityTestAction @@ -50,22 +50,30 @@ public LoadSceneAttribute(string path) public IEnumerator BeforeTest(ITest test) { AsyncOperation loadSceneAsync = null; -#if UNITY_EDITOR - if (EditorApplication.isPlaying) + + if (Application.isEditor) { - // Use EditorSceneManager at run on Unity-editor - loadSceneAsync = EditorSceneManager.LoadSceneAsyncInPlayMode( - ScenePath, - new LoadSceneParameters(LoadSceneMode.Single)); +#if UNITY_EDITOR + if (Application.isPlaying) + { + // Play Mode tests running in Editor + loadSceneAsync = EditorSceneManager.LoadSceneAsyncInPlayMode( + ScenePath, + new LoadSceneParameters(LoadSceneMode.Single)); + } + else + { + // Edit Mode tests + EditorSceneManager.OpenScene(ScenePath); + } +#endif } else { - EditorSceneManager.OpenScene(ScenePath); + // Play Mode tests running on Player + loadSceneAsync = SceneManager.LoadSceneAsync(ScenePath); } -#else - // Use ITestPlayerBuildModifier to change the "Scenes in Build" list before run on player - loadSceneAsync = SceneManager.LoadSceneAsync(ScenePath); -#endif + yield return loadSceneAsync; }