Skip to content

Commit

Permalink
Merge pull request #15 from nowsprinting/chore/refactor_loadscene
Browse files Browse the repository at this point in the history
Refactor test using LoadScene attribute
  • Loading branch information
nowsprinting authored Nov 8, 2023
2 parents 4916c12 + feb2132 commit 96d50f1
Show file tree
Hide file tree
Showing 26 changed files with 99 additions and 237 deletions.
2 changes: 2 additions & 0 deletions Assembly-CSharp.csproj.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Cinternalsvisibletoexample_005Cscripts/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Cinternalsvisibletoexample_005Ctests/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Cloadassetexample_005Ctests/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Crandom_005Cscripts/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Crandom_005Ctests/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Csceneexample_005Cscripts/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Csceneexample_005Ctests/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Ctestdoubleexample_005Cscripts/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,34 @@

using System;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using InputExample.TestDoubles;
using NUnit.Framework;
using TestHelper.Attributes;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
using UnityEngine.TestTools.Utils;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEditor.SceneManagement;
#endif

// ReSharper disable Unity.InefficientPropertyAccess
// ReSharper disable AccessToStaticMemberViaDerivedType

namespace InputExample
{
[TestFixture]
[UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
public class FirstPersonControllerLegacyTest
{
private const string SandboxScenePath = "Assets/InputExample/Tests/Scenes/InputExampleSandbox.unity";

private FirstPersonControllerLegacy _controller;

[SetUp]
public async Task SetUp()
public void SetUp()
{
#if UNITY_EDITOR
await EditorSceneManager.LoadSceneAsyncInPlayMode(
SandboxScenePath,
new LoadSceneParameters(LoadSceneMode.Single));
// Note: Scenes in Buildに入れていないSceneなので、EditorSceneManagerでロード
#endif
_controller = Object.FindObjectOfType<FirstPersonControllerLegacy>();
// Note: LoadScene 属性は SetUp より先に処理されるため、ここで Scene はロード済み
_controller = GameObject.FindObjectOfType<FirstPersonControllerLegacy>();
}

[Test]
[LoadScene(SandboxScenePath)]
public async Task Wキーで前方に移動()
{
var beforePosition = _controller.transform.position;
Expand All @@ -59,6 +50,7 @@ public async Task Wキーで前方に移動()
}

[Test]
[LoadScene(SandboxScenePath)]
public async Task マウス右移動で右方向を向く()
{
var beforeRotation = _controller.transform.rotation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "InputSystemExample.Tests",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"InputSystemExample",
"Unity.InputSystem",
"Unity.InputSystem.TestFramework",
"UniTask",
"TestHelper"
"TestHelper",
"TestHelper.Random"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
6 changes: 3 additions & 3 deletions Assets/InputSystemExample/Tests/Runtime/MonkeyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void TearDown()
[Test]
public async Task MonkeyTesting()
{
var random = new RandomWrapper(); // 擬似乱数生成器
var random = new RandomImpl(); // 擬似乱数生成器
Debug.Log($"Random that monkey uses: {random}"); // シード値をログ出力(再現可能にするため)

await SceneManager.LoadSceneAsync("InputSystemExample");
Expand All @@ -57,8 +57,8 @@ public async Task MonkeyTesting()

using (var tokenSource = new CancellationTokenSource())
{
PressKeys(new RandomWrapper(random.Next()), tokenSource.Token).Forget();
MoveMouse(new RandomWrapper(random.Next()), tokenSource.Token).Forget();
PressKeys(new RandomImpl(random.Next()), tokenSource.Token).Forget();
MoveMouse(new RandomImpl(random.Next()), tokenSource.Token).Forget();

var expireTime = Time.time + 10.0f; // 10秒間動作させる(3分以上にする場合はTimeout属性でタイムアウト時間を延長)
while (Time.time < expireTime)
Expand Down
8 changes: 0 additions & 8 deletions Assets/SceneExample/Scripts.meta

This file was deleted.

8 changes: 0 additions & 8 deletions Assets/SceneExample/Scripts/Editor.meta

This file was deleted.

51 changes: 0 additions & 51 deletions Assets/SceneExample/Scripts/Editor/BuildAllScenesForTestPlayer.cs

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions Assets/SceneExample/Scripts/Editor/SceneExample.Editor.asmdef

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"name": "SceneExample.Editor.Tests",
"rootNamespace": "SceneExample.Editor",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"SceneExample.Editor"
"UnityEditor.TestRunner"
],
"includePlatforms": [
"Editor"
Expand Down
4 changes: 2 additions & 2 deletions Assets/SceneExample/Tests/Runtime/SceneExample.Tests.asmdef
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "SceneExample.Tests",
"rootNamespace": "SceneExample",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner"
"UnityEditor.TestRunner",
"TestHelper"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
53 changes: 44 additions & 9 deletions Assets/SceneExample/Tests/Runtime/SceneManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// This software is released under the MIT License.

using System.Collections;
using System.Linq;
using NUnit.Framework;
using SceneExample.Utils;
using TestHelper.Attributes;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
Expand All @@ -23,29 +24,63 @@ public IEnumerator CreateScene_クリーンなSceneを生成してテストを

var actual = SceneManager.GetActiveScene().name;
Assert.That(actual, Is.EqualTo("New Scene"));

yield return SceneManager.UnloadSceneAsync(scene);
}

[Test]
[CreateScene(camera: true, light: true)]
public void CreateSceneAttribute_クリーンなSceneをCreateSceneで生成する例()
{
var scene = SceneManager.GetActiveScene();
Assert.That(scene.name, Is.EqualTo(
"Scene of SceneExample.SceneManagerTest.CreateSceneAttribute_クリーンなSceneをCreateSceneで生成する例"));

var rootGameObjectNames = scene.GetRootGameObjects().Select(x => x.name).ToArray();
Assert.That(rootGameObjectNames, Is.EquivalentTo(new[] { "Main Camera", "Directional Light" }));
}

[UnityTest]
[Description("Load scene included in the \"Scenes in Build\" by LoadSceneAsync method")]
public IEnumerator LoadSceneAsync_ScenesInBuildに含まれるSceneをロードする例()
{
yield return SceneManager.LoadSceneAsync("HelloTesting");
// Scene名称指定でロードできる
// Can be load by Scene name.
// Scene名称指定でロードできます。エディター実行でもプレイヤー実行でも、同じコードで動作します。
// Can be load by Scene name. It works in both in Editor and on Player.

var cube = GameObject.Find("Cube");
Assert.That(cube, Is.Not.Null);
}

[UnityTest]
[Description("Load scene not included in the \"Scenes in Build\", Only running in Editor")]
[UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
public IEnumerator LoadSceneAsync_ScenesInBuildに含まれていないSceneをEditorSceneManagerを使ってロードする例()
{
// ReSharper disable once RedundantAssignment
AsyncOperation loadSceneAsync = null;
#if UNITY_EDITOR
loadSceneAsync = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(
"Assets/SceneExample/Scenes/NotContainScenesInBuild.unity",
new LoadSceneParameters(LoadSceneMode.Single));
#endif
yield return loadSceneAsync;

var sphere = GameObject.Find("Sphere");
Assert.That(sphere, Is.Not.Null);
}

[Test]
[LoadScene("Assets/SceneExample/Scenes/NotContainScenesInBuild.unity")]
[Description("Load scene not included in the \"Scenes in Build\"")]
public IEnumerator LoadSceneAsync_ScenesInBuildに含まれていないSceneをロードする例()
public void LoadSceneAttribute_ScenesInBuildに含まれていないSceneをLoadScene属性を使ってロードする例()
{
yield return TestSceneLoader.LoadSceneAsync("Assets/SceneExample/Scenes/NotContainScenesInBuild.unity");
// エディター実行・プレイヤー実行で扱いが異なる(`TestSceneLoader` 参照)
// いずれのケースでも、Assets/〜.unityまでのパスで指定が必要
// Different handling for editor and player execution. It is handled in the TestSceneLoader.
// In both cases, you need to specify the path to Assets/~.unity.
// Test Helper パッケージ(com.nowsprinting.test-helper)に含まれる LoadScene 属性は、テスト実行前に指定された Scene をロードします。
// "Scenes in Build" に含まれていない Scene であっても、エディター実行でもプレイヤー実行でも同じコードで動作します。
// 『Unity Test Framework完全攻略ガイド』10.2.2 および 10.3 で紹介している方法で、エディター実行・プレイヤー実行の差異を吸収しています。

// LoadScene attribute in Test Helper package (com.nowsprinting.test-helper) loads Scene before test execution.
// It works scene not included in "Scenes in Build". It works in both in Editor and on Player, same code.

var sphere = GameObject.Find("Sphere");
Assert.That(sphere, Is.Not.Null);
Expand Down
3 changes: 0 additions & 3 deletions Assets/SceneExample/Tests/Runtime/Utils.meta

This file was deleted.

44 changes: 0 additions & 44 deletions Assets/SceneExample/Tests/Runtime/Utils/TestSceneLoader.cs

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion Assets/UGUIExample/Tests/Runtime/MonkeyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Cysharp.Threading.Tasks;
using NUnit.Framework;
using TestHelper.Monkey;
using TestHelper.Monkey.Random;
using TestHelper.Random;
using UnityEngine;
using UnityEngine.SceneManagement;

Expand Down
4 changes: 2 additions & 2 deletions Assets/UGUIExample/Tests/Runtime/UGUIExample.Tests.asmdef
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "UGUIExample.Tests",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"UGUIExample",
"UniTask",
"TestHelper",
"TestHelper.Monkey"
"TestHelper.Monkey",
"TestHelper.Random"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
Loading

0 comments on commit 96d50f1

Please sign in to comment.