Skip to content

Commit

Permalink
Add ScenesUsingInTest attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
nowsprinting committed Oct 16, 2023
1 parent d3e274e commit 9ca1b21
Show file tree
Hide file tree
Showing 12 changed files with 1,068 additions and 9 deletions.
1 change: 1 addition & 0 deletions Runtime/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("TestHelper.Editor")]
[assembly: InternalsVisibleTo("TestHelper.Tests")]
39 changes: 39 additions & 0 deletions Runtime/Attributes/ScenesUsingInTestAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2023 Koji Hasegawa.
// This software is released under the MIT License.

using System;
using NUnit.Framework;

namespace TestHelper.Attributes
{
/// <summary>
/// Temporarily build scene files not added to "Scenes in Build" when running a test.
/// It has the following effects:
/// - Can specify only scene name to `SceneManager.LoadScene()` method
/// - Can load in `SceneManager.LoadScene()` method when running a test on a standalone player.
///
/// This attribute is effective for the entire test run, not individual tests.
/// So, I recommend specifying it at the assembly level.
/// </summary>
/// <remarks>
/// The name is "Scene*s*" to like a "Scenes in Build".
/// The argument is a single string, but can attach multiple this attribute.
/// </remarks>
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class ScenesUsingInTestAttribute : NUnitAttribute
{
internal string ScenePath { get; private set; }

/// <summary>
/// Specify scene file path to temporarily build when running a test.
/// </summary>
/// <param name="scenePath">Scene path not in "Scenes in Build".
/// The scene file path starts with `Assets/` or `Packages/`, and ends with `.unity`.
/// Use `name` instead of `displayName` in package paths.
/// </param>
public ScenesUsingInTestAttribute(string scenePath)
{
ScenePath = scenePath;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Attributes/ScenesUsingInTestAttribute.cs.meta

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

6 changes: 0 additions & 6 deletions Tests/Editor/AssemblyInfo.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Tests/Editor/AssemblyInfo.cs.meta

This file was deleted.

40 changes: 40 additions & 0 deletions Tests/Runtime/Attributes/ScenesUsingInTestAttributeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2023 Koji Hasegawa.
// This software is released under the MIT License.

using System.Collections;
using System.Diagnostics.CodeAnalysis;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;

namespace TestHelper.Attributes
{
[TestFixture]
[SuppressMessage("ReSharper", "Unity.LoadSceneUnexistingScene")]
public class ScenesUsingInTestAttributeTest
{
[UnityTest]
[ScenesUsingInTest("Packages/com.nowsprinting.test-helper/Tests/Scenes/NotInScenesInBuild.unity")]
public IEnumerator Attach_CanLoadSceneNotIncludedBuild()
{
yield return SceneManager.LoadSceneAsync("NotInScenesInBuild");
var cube = GameObject.Find("CubeInNotInScenesInBuild");
Assert.That(cube, Is.Not.Null);
}

[UnityTest]
[ScenesUsingInTest("Packages/com.nowsprinting.test-helper/Tests/Scenes/NotInScenesInBuild.unity")]
[ScenesUsingInTest("Packages/com.nowsprinting.test-helper/Tests/Scenes/NotInScenesInBuild2.unity")]
public IEnumerator AttachMultiple_CanLoadScenesNotIncludedBuild()
{
yield return SceneManager.LoadSceneAsync("NotInScenesInBuild");
var cube = GameObject.Find("CubeInNotInScenesInBuild");
Assert.That(cube, Is.Not.Null);

yield return SceneManager.LoadSceneAsync("NotInScenesInBuild2");
var cube2 = GameObject.Find("CubeInNotInScenesInBuild2");
Assert.That(cube2, Is.Not.Null);
}
}
}

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

8 changes: 8 additions & 0 deletions Tests/Scenes.meta

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

Loading

0 comments on commit 9ca1b21

Please sign in to comment.