-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadSceneAttribute.cs
90 lines (82 loc) · 2.95 KB
/
LoadSceneAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// 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 TestHelper.Utils;
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="Editor.TemporaryBuildScenesUsingInTest"/>
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class LoadSceneAttribute : NUnitAttribute, IOuterUnityTestAction
{
internal string ScenePath { get; private set; }
/// <summary>
/// Load scene before running test.
/// Can be specified path by glob pattern. However, there are restrictions, top level and scene name cannot be omitted.
/// </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>
/// <seealso href="https://en.wikipedia.org/wiki/Glob_(programming)"/>
public LoadSceneAttribute(string path)
{
ScenePath = path;
}
/// <inheritdoc />
public IEnumerator BeforeTest(ITest test)
{
var existScenePath = ScenePathFinder.GetExistScenePath(ScenePath);
AsyncOperation loadSceneAsync = null;
if (Application.isEditor)
{
#if UNITY_EDITOR
if (Application.isPlaying)
{
// Play Mode tests running in Editor
loadSceneAsync = EditorSceneManager.LoadSceneAsyncInPlayMode(
existScenePath,
new LoadSceneParameters(LoadSceneMode.Single));
}
else
{
// Edit Mode tests
EditorSceneManager.OpenScene(existScenePath);
}
#endif
}
else
{
// Play Mode tests running on Player
loadSceneAsync = SceneManager.LoadSceneAsync(existScenePath);
}
yield return loadSceneAsync;
}
/// <inheritdoc />
public IEnumerator AfterTest(ITest test)
{
yield return null;
}
}
}