Skip to content
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

Refactor: judge the running test mode #18

Merged
merged 1 commit into from
Oct 22, 2023
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
28 changes: 19 additions & 9 deletions Runtime/Attributes/CreateSceneAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Camera>();
Expand Down
32 changes: 20 additions & 12 deletions Runtime/Attributes/LoadSceneAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace TestHelper.Attributes
///
/// Notes:
/// - Load scene run after <c>OneTimeSetUp</c> and before <c>SetUp</c>
/// - For the process of including a Scene not in "Scenes in Build" to a build for player, see: <see cref="TestHelper.Editor.TemporaryBuildScenesUsingInTest"/>
/// - For the process of including a Scene not in "Scenes in Build" to a build for player, see: <see cref="Editor.TemporaryBuildScenesUsingInTest"/>
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class LoadSceneAttribute : NUnitAttribute, IOuterUnityTestAction
Expand All @@ -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;
}

Expand Down