Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,6 @@ resharper_switch_statement_handles_some_known_enum_values_with_default_highlight
# BannedApiAnalyzers
dotnet_diagnostic.RS0030.severity = error

# Suppress C#8.0+ syntax sugar (for under Unity 2020.2 compatibility)
# See: https://www.jetbrains.com/help/resharper/Reference__Code_Inspections_CSHARP.html
resharper_convert_to_using_declaration_highlighting = none
resharper_convert_to_null_coalescing_compound_assignment_highlighting = none
resharper_merge_into_logical_pattern_highlighting = none
resharper_use_negated_pattern_in_is_expression_highlighting = none

# Test codes for Unity Test Framework
[**/Tests/**/*.cs]

Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/test-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ jobs:
fail-fast: false
matrix:
unityVersion: # Available versions see: https://game.ci/docs/docker/versions
- 2019.4.40f1
- 2020.3.49f1
- 2021.3.45f1
- 2022.3.62f1
- 6000.0.43f1
- 6000.0.44f1 # pin test-framework v1.5.1
- 6000.0.59f2 # pin test-framework v1.6.0
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ jobs:
fail-fast: false
matrix:
unityVersion: # Available versions see: https://game.ci/docs/docker/versions
- 2019.4.40f1
- 2020.3.49f1
- 2021.3.45f1
- 2022.3.62f1
- 6000.0.43f1
- 6000.0.44f1 # pin test-framework v1.5.1
- 6000.0.59f2 # pin test-framework v1.6.0
Expand Down

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

21 changes: 16 additions & 5 deletions Assets/APIExamples/Tests/Editor/APIExamples.Editor.Tests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"NUnit.Analyzers_Unity",
"APIExamples.Tests",
"UniTask",
"TestHelper"
"APIExamples.Tests",
"UniTask",
"TestHelper",
"NUnit.Analyzers_Unity"
],
"includePlatforms": [
"Editor"
Expand All @@ -22,6 +22,17 @@
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"versionDefines": [
{
"name": "com.unity.test-framework",
"expression": "1.5",
"define": "ENABLE_UTF_1_5"
},
{
"name": "com.unity.test-framework",
"expression": "1.6",
"define": "ENABLE_UTF_1_6"
}
],
"noEngineReferences": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2021-2025 Koji Hasegawa.
// This software is released under the MIT License.

#if ENABLE_UTF_1_5
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using APIExamples.NUnit;
using NUnit.Framework;
using UnityEngine.TestTools;
using Is = APIExamples.NUnit.Is;

namespace APIExamples.Editor.UnityTestFramework
{
/// <summary>
/// <see cref="UnityOneTimeSetupAttributeExample"/>の使用例
/// </summary>
/// <remarks>
/// Required: Unity Test Framework v1.5 or later
/// </remarks>
/// <seealso cref="AsyncSetupAttributeExample"/>
/// <seealso cref="UnitySetUpAttributeExample"/>
[TestFixture]
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType")]
public class UnityOneTimeSetupAttributeExample
{
private int _oneTimeSetupCount;
private int _setupCount;

/// <summary>
/// テストクラス内の最初のテストの実行前に一度だけ実行されます
/// </summary>
[UnityOneTimeSetUp]
public IEnumerator OneTimeSetUp()
{
yield return null;
_oneTimeSetupCount++;
}

/// <summary>
/// 各テストメソッドの前に実行されます
/// </summary>
[SetUp]
public void SetUp()
{
_setupCount++;
}

[Test, Order(0)]
public void TestMethod()
{
Assert.That(_oneTimeSetupCount, Is.EqualTo(1), "OneTimeSetUpはTestFixtureごとに一度だけ実行される");
Assert.That(_setupCount, Is.EqualTo(1), "最初のテストなのでSetUpは1回実行されている");
}

[Test, Order(1)]
public async Task TestMethodAsync()
{
await Task.Yield();
Assert.That(_oneTimeSetupCount, Is.EqualTo(1), "OneTimeSetUpはTestFixtureごとに一度だけ実行される");
Assert.That(_setupCount, Is.EqualTo(2), "2番目のテストなのでSetUpは2回実行されている");
}

[UnityTest, Order(2)]
public IEnumerator UnityTestMethod()
{
yield return null;
Assert.That(_oneTimeSetupCount, Is.EqualTo(1), "OneTimeSetUpはTestFixtureごとに一度だけ実行される");
Assert.That(_setupCount, Is.EqualTo(3), "3番目のテストなのでSetUpは3回実行されている");
}
}
}
#endif

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2021-2025 Koji Hasegawa.
// This software is released under the MIT License.

#if ENABLE_UTF_1_5
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using APIExamples.NUnit;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using Is = APIExamples.NUnit.Is;

namespace APIExamples.Editor.UnityTestFramework
{
/// <summary>
/// <see cref="UnityOneTimeTearDownAttributeExample"/>の使用例
/// </summary>
/// <remarks>
/// Required: Unity Test Framework v1.5 or later
/// </remarks>
/// <seealso cref="AsyncTearDownAttributeExample"/>
/// <seealso cref="UnityTearDownAttributeExample"/>
[TestFixture]
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType")]
public class UnityOneTimeTearDownAttributeExample
{
private int _oneTimeTeardownCount;
private int _teardownCount;

/// <summary>
/// クラス内の最後のテストの実行後に一度だけ実行されます
/// </summary>
[UnityOneTimeTearDown]
public IEnumerator OneTimeTearDown()
{
yield return null;
Debug.Log($"UnityOneTimeTearDown");
_oneTimeTeardownCount++;

Assert.That(_oneTimeTeardownCount, Is.EqualTo(1), "OneTimeTearDownはTestFixtureごとに一度だけ実行される");
Assert.That(_teardownCount, Is.EqualTo(3), "3つのテストがあるのでTearDownは3回実行されている");
}

/// <summary>
/// 各テストメソッドの後に実行されます
/// </summary>
[TearDown]
public void TearDown()
{
Debug.Log($"TearDown");
_teardownCount++;
}

[Test]
public void TestMethod()
{
Debug.Log($"TestMethod");
}

[Test]
public async Task TestMethodAsync()
{
await Task.Yield();
Debug.Log($"TestMethodAsync");
}

[UnityTest]
public IEnumerator UnityTestMethod()
{
yield return null;
Debug.Log($"UnityTestMethod");
}
}
}
#endif

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

18 changes: 15 additions & 3 deletions Assets/APIExamples/Tests/Runtime/APIExamples.Tests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"APIExamples",
"UniTask",
"TestHelper"
"UniTask",
"TestHelper",
"NUnit.Analyzers_Unity"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand All @@ -19,6 +20,17 @@
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"versionDefines": [
{
"name": "com.unity.test-framework",
"expression": "1.5",
"define": "ENABLE_UTF_1_5"
},
{
"name": "com.unity.test-framework",
"expression": "1.6",
"define": "ENABLE_UTF_1_6"
}
],
"noEngineReferences": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
namespace APIExamples.UnityTestFramework
{
/// <summary>
/// <see cref="UnityEngine.TestTools.PrebuildSetupAttribute"/>で指定した<see cref="UnityEngine.TestTools.IPrebuildSetup"/>実装クラスの`Setup()`メソッドが使用されます
/// <see cref="UnityEngine.TestTools.PrebuildSetupAttribute"/> で指定した <see cref="UnityEngine.TestTools.IPrebuildSetup"/> 実装クラスの <c>Setup</c> メソッドが使用されます
/// <list type="bullet">
/// <item>複数のテストに <c>PrebuildSetup</c> 属性が配置されていても、<c>Setup</c> の実行は1回だけです</item>
/// <item><see cref="UnityEngine.TestTools.IPrebuildSetup"/> を実装していないクラスを渡した場合、何も起きません(エラーにもなりません)</item>
/// <item><c>PrebuildSetup</c> 属性は、クラスにもメソッドにも配置できます</item>
/// </list>
/// </summary>
/// <remarks>
/// - 複数のテストに属性が付与されていた場合、`Setup()`の実行は1回だけです
/// - <see cref="UnityEngine.TestTools.IPrebuildSetup"/>を実装していないクラスを渡した場合、何も起きません(エラーにもなりません)
/// - 属性は、クラスにもメソッドにも付与できます
/// </remarks>
[PrebuildSetup(typeof(PreBuildSetupExample))]
[PostBuildCleanup(typeof(PreBuildSetupExample))]
public class PreBuildSetupAttributeExample
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,34 @@
// This software is released under the MIT License.

using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

namespace APIExamples.UnityTestFramework
{
/// <summary>
/// ビルド前後に処理を挿むための <see cref="UnityEngine.TestTools.IPrebuildSetup"/> および <see cref="UnityEngine.TestTools.IPostBuildCleanup"/> の実装例
/// </summary>
public class PreBuildSetupExample : IPrebuildSetup, IPostBuildCleanup
{
///<inheritdoc/>
/// <remarks>
/// Unityエディター実行(Edit ModeテストおよびPlay Modeテスト)では、すべてのテストの実行に先立って実行されます。
/// プレイヤー実行(Play Modeテスト)では、ビルド前に実行されます。
/// </remarks>
public void Setup()
{
// Edit ModeテストおよびPlay Modeテスト(Unityエディター実行)では、すべてのテストの実行に先立って実行される
// Play Modeテスト(プレイヤー実行)では、ビルド前に実行される
// テスト用のビルドにのみResourcesに含めたいアセットをコピーする使用例は `LoadAssetExample` を参照してください
Debug.Log("PreBuildSetupExample.Setup");
}

///<inheritdoc/>
/// <remarks>
/// Unityエディター実行(Edit ModeテストおよびPlay Modeテスト)では、すべてのテストの実行終了後に実行されます。
/// プレイヤー実行(Play Modeテスト)では、ビルド後に実行されます。
/// </remarks>
public void Cleanup()
{
// Edit ModeテストおよびPlay Modeテスト(Unityエディター実行)では、すべてのテストの実行終了後に実行される
// Play Modeテスト(プレイヤー実行)では、ビルド後に実行される
Debug.Log("PreBuildSetupExample.Cleanup");
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2021-2025 Koji Hasegawa.
// This software is released under the MIT License.

#if ENABLE_UTF_1_6
using NUnit.Framework;
using UnityEngine.TestTools;

namespace APIExamples.UnityTestFramework
{
/// <summary>
/// <see cref="UnityEngine.TestTools.PrebuildSetupWithTestDataAttribute"/> で指定した <see cref="UnityEngine.TestTools.IPrebuildSetupWithTestData"/> 実装クラスの <c>Setup</c> メソッドが使用されます
/// </summary>
/// <remarks>
/// Required: Unity Test Framework v1.6 or later
/// </remarks>
/// <seealso cref="PreBuildSetupAttributeExample"/>
[PrebuildSetupWithTestData(typeof(PreBuildSetupWithTestDataExample))]
[PostBuildCleanupWithTestData(typeof(PreBuildSetupWithTestDataExample))]
public class PreBuildSetupWithTestDataAttributeExample
{
[Test]
public void PrebuildSetupWithTestDataAttributeを付与したテストの例()
{
Assert.That(true, Is.True);
}
}
}
#endif

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

Loading