-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from nowsprinting/feature/createsceneattribute
Add CreateSceneAttribute
- Loading branch information
Showing
7 changed files
with
249 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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 | ||
|
||
namespace TestHelper.Attributes | ||
{ | ||
/// <summary> | ||
/// Create new scene before running test. | ||
/// | ||
/// Notes: | ||
/// - Create scene run after <c>OneTimeSetUp</c> and before <c>SetUp</c> | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method)] | ||
public class CreateSceneAttribute : NUnitAttribute, IOuterUnityTestAction | ||
{ | ||
private readonly bool _camera; | ||
private readonly bool _light; | ||
private string _newSceneName; | ||
|
||
/// <summary> | ||
/// Create new scene before running test. | ||
/// </summary> | ||
/// <param name="camera">true: create main camera object in new scene</param> | ||
/// <param name="light">true: create directional light object in new scene</param> | ||
public CreateSceneAttribute(bool camera = false, bool light = false) | ||
{ | ||
_camera = camera; | ||
_light = light; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IEnumerator BeforeTest(ITest test) | ||
{ | ||
_newSceneName = $"Scene of {TestContext.CurrentContext.Test.FullName}"; | ||
#if UNITY_EDITOR | ||
if (EditorApplication.isPlaying) | ||
{ | ||
var scene = SceneManager.CreateScene(_newSceneName); | ||
SceneManager.SetActiveScene(scene); | ||
} | ||
else | ||
{ | ||
var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene); | ||
scene.name = _newSceneName; | ||
} | ||
#else | ||
var scene = SceneManager.CreateScene(_newSceneName); | ||
SceneManager.SetActiveScene(scene); | ||
#endif | ||
if (_camera) | ||
{ | ||
var camera = new GameObject("Main Camera").AddComponent<Camera>(); | ||
camera.transform.position = new Vector3(0, 1, -10); | ||
camera.transform.LookAt(Vector3.zero); | ||
} | ||
|
||
if (_light) | ||
{ | ||
var light = new GameObject("Directional Light").AddComponent<Light>(); | ||
light.transform.position = new Vector3(0, 3, 0); | ||
light.transform.rotation = Quaternion.Euler(new Vector3(50, -30, 0)); | ||
light.type = LightType.Directional; | ||
light.color = new Color(0xff, 0xf4, 0xd6); | ||
} | ||
|
||
yield return null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IEnumerator AfterTest(ITest test) | ||
{ | ||
#if UNITY_EDITOR | ||
if (!EditorApplication.isPlaying) | ||
{ | ||
yield break; // Not for EditMode tests | ||
} | ||
#endif | ||
if (SceneManager.GetActiveScene().name == _newSceneName) | ||
{ | ||
yield return SceneManager.UnloadSceneAsync(_newSceneName); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,29 @@ | ||
// Copyright (c) 2023 Koji Hasegawa. | ||
// This software is released under the MIT License. | ||
|
||
using NUnit.Framework; | ||
using TestHelper.Attributes; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace TestHelper.Editor | ||
{ | ||
[TestFixture] | ||
public class CreateSceneAttributeTest | ||
{ | ||
[Test] | ||
[CreateScene(camera: true, light: true)] | ||
public void Attach_CreateNewSceneWithCameraAndLight() | ||
{ | ||
var scene = SceneManager.GetActiveScene(); | ||
Assert.That(scene.name, Is.EqualTo( | ||
"Scene of TestHelper.Editor.CreateSceneAttributeTest.Attach_CreateNewSceneWithCameraAndLight")); | ||
|
||
var camera = GameObject.Find("Main Camera"); | ||
Assert.That(camera, Is.Not.Null); | ||
|
||
var light = GameObject.Find("Directional Light"); | ||
Assert.That(light, Is.Not.Null); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,86 @@ | ||
// Copyright (c) 2023 Koji Hasegawa. | ||
// This software is released under the MIT License. | ||
|
||
using System.Collections; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
using UnityEngine.TestTools; | ||
|
||
namespace TestHelper.Attributes | ||
{ | ||
[TestFixture] | ||
public class CreateSceneAttributeTest | ||
{ | ||
[Test] | ||
[CreateScene] | ||
public void Attach_CreateNewSceneWithoutCameraAndLight() | ||
{ | ||
var scene = SceneManager.GetActiveScene(); | ||
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); | ||
} | ||
|
||
[Test] | ||
[CreateScene(camera: true)] | ||
public void Attach_WithCamera_CreateNewSceneWithCamera() | ||
{ | ||
var scene = SceneManager.GetActiveScene(); | ||
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); | ||
} | ||
|
||
[Test] | ||
[CreateScene(light: true)] | ||
public void Attach_WithLight_CreateNewSceneWithLight() | ||
{ | ||
var scene = SceneManager.GetActiveScene(); | ||
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); | ||
} | ||
|
||
[Test] | ||
[CreateScene] | ||
public void AttachToParameterizedTest_CreateNewScene([Values(0, 1)] int i) | ||
{ | ||
var scene = SceneManager.GetActiveScene(); | ||
Assert.That(scene.name, Is.EqualTo( | ||
$"Scene of TestHelper.Attributes.CreateSceneAttributeTest.AttachToParameterizedTest_CreateNewScene({i})")); | ||
} | ||
|
||
[Test] | ||
[CreateScene] | ||
public async Task AttachToAsyncTest_CreateNewScene() | ||
{ | ||
var scene = SceneManager.GetActiveScene(); | ||
Assert.That(scene.name, Is.EqualTo( | ||
"Scene of TestHelper.Attributes.CreateSceneAttributeTest.AttachToAsyncTest_CreateNewScene")); | ||
|
||
await Task.Yield(); | ||
} | ||
|
||
[UnityTest] | ||
[CreateScene] | ||
public IEnumerator AttachToUnityTest_CreateNewScene() | ||
{ | ||
var scene = SceneManager.GetActiveScene(); | ||
Assert.That(scene.name, Is.EqualTo( | ||
"Scene of TestHelper.Attributes.CreateSceneAttributeTest.AttachToUnityTest_CreateNewScene")); | ||
|
||
yield return null; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.