-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace ScenesUsingInTestAttribute to LoadSceneAttribute
- Loading branch information
1 parent
521c930
commit ed34b9c
Showing
24 changed files
with
193 additions
and
3,170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) 2023 Koji Hasegawa. | ||
// This software is released under the MIT License. | ||
|
||
using System; | ||
using System.Collections; | ||
using NUnit.Framework; | ||
using NUnit.Framework.Interfaces; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
using UnityEngine.TestTools; | ||
#if UNITY_EDITOR | ||
using UnityEditor; | ||
using UnityEditor.SceneManagement; | ||
#endif | ||
|
||
// ReSharper disable InvalidXmlDocComment | ||
|
||
namespace TestHelper.Attributes | ||
{ | ||
/// <summary> | ||
/// Load scene before running test. | ||
/// | ||
/// It has the following benefits: | ||
/// - Can be used when running play mode tests in-editor and on-player | ||
/// - Can be specified scenes that are not in "Scenes in Build" | ||
/// | ||
/// 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"/> | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method)] | ||
public class LoadSceneAttribute : NUnitAttribute, IOuterUnityTestAction | ||
{ | ||
internal string ScenePath { get; private set; } | ||
|
||
/// <summary> | ||
/// Load scene before running test. | ||
/// </summary> | ||
/// <param name="path">Scene file path. | ||
/// The path starts with `Assets/` or `Packages/`. | ||
/// And package name using `name` instead of `displayName`, when scenes in the package. | ||
/// (e.g., `Packages/com.nowsprinting.test-helper/Tests/Scenes/Scene.unity`) | ||
/// </param> | ||
public LoadSceneAttribute(string path) | ||
{ | ||
ScenePath = path; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IEnumerator BeforeTest(ITest test) | ||
{ | ||
AsyncOperation loadSceneAsync = null; | ||
#if UNITY_EDITOR | ||
if (EditorApplication.isPlaying) | ||
{ | ||
// Use EditorSceneManager at run on Unity-editor | ||
loadSceneAsync = EditorSceneManager.LoadSceneAsyncInPlayMode( | ||
ScenePath, | ||
new LoadSceneParameters(LoadSceneMode.Single)); | ||
} | ||
else | ||
{ | ||
EditorSceneManager.OpenScene(ScenePath); | ||
} | ||
#else | ||
// Use ITestPlayerBuildModifier to change the "Scenes in Build" list before run on player | ||
loadSceneAsync = SceneManager.LoadSceneAsync(ScenePath); | ||
#endif | ||
yield return loadSceneAsync; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IEnumerator AfterTest(ITest test) | ||
{ | ||
yield return null; | ||
} | ||
} | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2023 Koji Hasegawa. | ||
// This software is released under the MIT License. | ||
|
||
using NUnit.Framework; | ||
using TestHelper.Attributes; | ||
using UnityEngine; | ||
|
||
namespace TestHelper.Editor | ||
{ | ||
[TestFixture] | ||
public class LoadSceneAttributeTest | ||
{ | ||
private const string TestScene = "Packages/com.nowsprinting.test-helper/Tests/Scenes/NotInScenesInBuild.unity"; | ||
private const string ObjectName = "CubeInNotInScenesInBuild"; | ||
|
||
[Test] | ||
[LoadScene(TestScene)] | ||
public void Attach_AlreadyLoadedSceneNotInBuild() | ||
{ | ||
var cube = GameObject.Find(ObjectName); | ||
Assert.That(cube, Is.Not.Null); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.