Skip to content

Commit

Permalink
Merge pull request #15 from nowsprinting/feature/createsceneattribute
Browse files Browse the repository at this point in the history
Add CreateSceneAttribute
  • Loading branch information
nowsprinting committed Oct 22, 2023
2 parents d82dd8e + 4bc9775 commit 19f6103
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 1 deletion.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Required Unity 2019 LTS or later.

## Features


### Attributes

#### FocusGameView
Expand Down Expand Up @@ -131,6 +130,36 @@ public class MyTestClass
}
```

#### CreateScene

`CreateSceneAttribute` is an NUnit test attribute class to create new scene before running test.

This attribute can attached to test method only.

Usage:

```csharp
using System;
using NUnit.Framework;
using TestHelper.Attributes;

[TestFixture]
public class MyTestClass
{
[Test]
[CreateScene]
public void MyTestMethod(camera: true, light: true)
{
var camera = GameObject.Find("Main Camera");
Assert.That(camera, Is.Not.Null);
}
}
```

> **Note**
> - Create scene run after <c>OneTimeSetUp</c> and before <c>SetUp</c>
> - Create or not `Main Camera` and `Directional Light` can be specified with parameters (default is not create)
#### LoadScene

`LoadSceneAttribute` is an NUnit test attribute class to load scene before running test.
Expand Down
95 changes: 95 additions & 0 deletions Runtime/Attributes/CreateSceneAttribute.cs
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);
}
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Attributes/CreateSceneAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Tests/Editor/CreateSceneAttributeTest.cs
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);
}
}
}
3 changes: 3 additions & 0 deletions Tests/Editor/CreateSceneAttributeTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions Tests/Runtime/Attributes/CreateSceneAttributeTest.cs
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;
}
}
}
3 changes: 3 additions & 0 deletions Tests/Runtime/Attributes/CreateSceneAttributeTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 19f6103

Please sign in to comment.