Skip to content

Commit fa76968

Browse files
committed
Fix examples about MaxTime, Repeat, Retry attributes with async test problem (UUM-25085)
1 parent 8794a5d commit fa76968

File tree

4 files changed

+44
-67
lines changed

4 files changed

+44
-67
lines changed

Assets/APIExamples/Tests/Runtime/NUnit/ConstraintExample.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void EqualConstraint_数値比較にWithin修飾子を使用_誤差を許
9595
{
9696
var actual = 42;
9797

98-
Assert.That(actual, Is.EqualTo(44.0f).Within(2)); // +/- 2まで許容
98+
Assert.That(actual, Is.EqualTo(44.0f).Within(2)); // +/- 2まで許容
9999
Assert.That(actual, Is.EqualTo(44.0f).Within(5).Percent); // +/- 5%まで許容
100100
// 失敗時メッセージ例:
101101
// Expected: 44.0f +/- 5 Percent
@@ -801,7 +801,7 @@ void GetNotThrow() { }
801801
// But was: <System.NullReferenceException: Object reference not set to an instance of an object.
802802
}
803803

804-
[Ignore("Throws制約をasyncメソッドに使用するとUnityエディターがフリーズ(Unity Test Framework v1.4.0時点)")]
804+
[Ignore("Throws制約をasyncメソッドに使用するとUnityエディターがフリーズ(Unity Test Framework v1.5.1時点)")]
805805
[Test]
806806
public async Task 非同期メソッドの例外捕捉を制約モデルで行なうことはできない_Unityエディターがフリーズ()
807807
{
@@ -817,7 +817,7 @@ async Task GetThrowWithMessageAsync()
817817
// See: https://unity3d.atlassian.net/servicedesk/customer/portal/2/IN-28107
818818
}
819819

820-
[Ignore("ThrowsAsyncをasyncメソッドに使用するとUnityエディターがフリーズ(Unity Test Framework v1.4.0時点)")]
820+
[Ignore("ThrowsAsyncをasyncメソッドに使用するとUnityエディターがフリーズ(Unity Test Framework v1.5.1時点)")]
821821
[Test]
822822
public async Task 非同期メソッドの例外捕捉をクラシックモデルで行なうことはできない_Unityエディターがフリーズ()
823823
{
Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,54 @@
1-
// Copyright (c) 2021-2023 Koji Hasegawa.
1+
// Copyright (c) 2021-2025 Koji Hasegawa.
22
// This software is released under the MIT License.
33

44
using System.Collections;
5-
using System.Diagnostics;
65
using System.Threading.Tasks;
76
using NUnit.Framework;
87
using UnityEngine;
98
using UnityEngine.TestTools;
10-
11-
#pragma warning disable CS0162
9+
using Debug = UnityEngine.Debug;
1210

1311
namespace APIExamples.NUnit
1412
{
1513
/// <summary>
1614
/// <see cref="MaxTimeAttribute"/>の使用例
1715
/// </summary>
16+
/// <remarks>
17+
/// <see cref="UnityTestAttribute"/> および非同期テストと使用できない問題は、Unity Test Framework v1.4.5で修正されました。
18+
/// <see href="https://issuetracker.unity3d.com/issues/timeout-attribute-is-not-working-when-used-with-the-async-test-in-the-test-runner"/>
19+
/// </remarks>
1820
[TestFixture]
21+
[Explicit("MaxTime属性によって失敗するテストの例")]
1922
public class MaxTimeAttributeExample
2023
{
21-
private readonly Stopwatch _stopwatch = new Stopwatch();
22-
private const bool Fail = false; // このフラグをtrueにするとこのクラスのテストはすべて失敗します
23-
24-
[SetUp]
25-
public void SetUp()
26-
{
27-
_stopwatch.Restart();
28-
}
29-
30-
[TearDown]
31-
public void TearDown()
32-
{
33-
UnityEngine.Debug.Log($"{_stopwatch.ElapsedMilliseconds} [ms]");
34-
Time.timeScale = 1f;
35-
}
36-
3724
[Test]
38-
[MaxTime(10)]
39-
public void MaxTimeを10ミリ秒に設定()
25+
[MaxTime(100)]
26+
public void MaxTime属性_指定ミリ秒よりも時間のかかるテストは実行後に失敗()
4027
{
41-
var loopCount = 1;
42-
if (Fail)
43-
{
44-
loopCount = 10000000;
45-
}
46-
28+
var loopCount = 50000000;
4729
var sum = 0f;
4830
for (var i = 0; i < loopCount; i++)
4931
{
5032
sum += Random.value;
5133
}
34+
35+
Debug.Log("テストは最後まで実行されます");
5236
}
5337

54-
[Explicit("MaxTime属性はUnityTest属性のテストに使用できない(Unity Test Framework v1.4.0時点)")]
5538
[UnityTest]
56-
[MaxTime(2000)]
57-
public IEnumerator MaxTime属性はUnityTest属性のテストに使用できない_実行時エラー()
39+
[MaxTime(100)]
40+
public IEnumerator MaxTime属性_指定ミリ秒よりも時間のかかるテストは実行後に失敗_UnityTest属性()
5841
{
59-
var waitSeconds = 1f;
60-
if (Fail)
61-
{
62-
waitSeconds = 10f;
63-
}
64-
65-
yield return new WaitForSeconds(waitSeconds);
42+
yield return new WaitForSeconds(0.5f);
43+
Debug.Log("テストは最後まで実行されます");
6644
}
6745

68-
[Ignore("MaxTime属性はasyncテストに使用できない(Unity Test Framework v1.4.0時点)")]
69-
// See: https://unity3d.atlassian.net/servicedesk/customer/portal/2/IN-28107
7046
[Test]
71-
[MaxTime(2000)]
72-
public async Task MaxTime属性は非同期テストに使用できない_テストが終了しない()
47+
[MaxTime(100)]
48+
public async Task MaxTime属性_指定ミリ秒よりも時間のかかるテストは実行後に失敗_非同期テスト()
7349
{
74-
await Task.Delay(3000);
50+
await Task.Delay(500);
51+
Debug.Log("テストは最後まで実行されます");
7552
}
7653
}
7754
}
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021-2023 Koji Hasegawa.
1+
// Copyright (c) 2021-2025 Koji Hasegawa.
22
// This software is released under the MIT License.
33

44
using System.Collections;
@@ -14,8 +14,11 @@ namespace APIExamples.NUnit
1414
/// </summary>
1515
/// <remarks>
1616
/// Unity Test Frameworkの<see href="https://docs.unity3d.com/Packages/[email protected]/manual/index.html#known-limitations">Known limitations</see>に
17-
/// The UnityTest attribute does not support the NUnit Repeat attribute.
17+
/// "The UnityTest attribute does not support the NUnit Repeat attribute."
1818
/// とありますが、v1.1.27で修正されました。
19+
/// <br/>
20+
/// <see cref="UnityTestAttribute"/> および非同期テストと使用できない問題は、Unity Test Framework v1.4.5で修正されました。
21+
/// <see href="https://issuetracker.unity3d.com/issues/timeout-attribute-is-not-working-when-used-with-the-async-test-in-the-test-runner"/>
1922
/// </remarks>
2023
public class RepeatAttributeExample
2124
{
@@ -27,29 +30,23 @@ public class RepeatAttributeExample
2730
[Repeat(2)]
2831
public void Repeat属性で繰り返し実行するテスト()
2932
{
30-
Debug.Log($"{nameof(Repeat属性で繰り返し実行するテスト)} {++_testCount}回目");
31-
Assert.That(true);
33+
Debug.Log($"{TestContext.CurrentContext.Test.Name} {++_testCount}回目");
3234
}
3335

3436
[UnityTest]
3537
[Repeat(2)]
3638
public IEnumerator Repeat属性で繰り返し実行するテスト_UnityTest属性()
3739
{
38-
Debug.Log($"{nameof(Repeat属性で繰り返し実行するテスト_UnityTest属性)} {++_unityTestCount}回目");
40+
Debug.Log($"{TestContext.CurrentContext.Test.Name} {++_unityTestCount}回目");
3941
yield return null;
40-
Assert.That(true);
4142
}
4243

43-
[Ignore("Repeat属性はasyncテストに使用できない(Unity Test Framework v1.4.0時点)")]
44-
// See: https://unity3d.atlassian.net/servicedesk/customer/portal/2/IN-28107
45-
// Note: Unity Test Framework v1.3.5 で追加された `-repeat` コマンドラインオプションは、asyncテストにも有効です
4644
[Test]
4745
[Repeat(2)]
48-
public async Task Repeat属性はasyncテストに使用できない_テストが終了しない()
46+
public async Task Repeat属性で繰り返し実行するテスト_非同期テスト()
4947
{
50-
Debug.Log($"{nameof(Repeat属性はasyncテストに使用できない_テストが終了しない)} {++_asyncTestCount}回目");
48+
Debug.Log($"{TestContext.CurrentContext.Test.Name} {++_asyncTestCount}回目");
5149
await Task.Yield();
52-
Assert.That(true);
5350
}
5451
}
5552
}

Assets/APIExamples/Tests/Runtime/NUnit/RetryAttributeExample.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021-2023 Koji Hasegawa.
1+
// Copyright (c) 2021-2025 Koji Hasegawa.
22
// This software is released under the MIT License.
33

44
using System.Collections;
@@ -16,8 +16,11 @@ namespace APIExamples.NUnit
1616
/// </summary>
1717
/// <remarks>
1818
/// Unity Test Frameworkの<see href="https://docs.unity3d.com/Packages/[email protected]/manual/index.html#known-limitations">Known limitations</see>に
19-
/// When using the NUnit Retry attribute in PlayMode tests, it throws InvalidCastException.
19+
/// "When using the NUnit Retry attribute in PlayMode tests, it throws InvalidCastException."
2020
/// とありますが、v1.1.27で修正されました。
21+
/// <br/>
22+
/// <see cref="UnityTestAttribute"/> および非同期テストと使用できない問題は、Unity Test Framework v1.4.5で修正されました。
23+
/// <see href="https://issuetracker.unity3d.com/issues/timeout-attribute-is-not-working-when-used-with-the-async-test-in-the-test-runner"/>
2124
/// </remarks>
2225
[TestFixture]
2326
public class RetryAttributeExample
@@ -30,28 +33,28 @@ public class RetryAttributeExample
3033
[Retry(2)] // リトライ回数ではなく総試行回数を指定
3134
public void Retry属性で最初は失敗するが2回目で成功するテスト()
3235
{
33-
Debug.Log($"{nameof(Retry属性で最初は失敗するが2回目で成功するテスト)} {++_testCount}回目");
36+
Debug.Log($"{TestContext.CurrentContext.Test.Name} {++_testCount}回目");
37+
3438
Assert.That(_testCount, Is.EqualTo(2));
3539
}
3640

3741
[UnityTest]
3842
[Retry(2)] // リトライ回数ではなく総試行回数を指定
3943
public IEnumerator Retry属性で最初は失敗するが2回目で成功するテスト_UnityTest属性()
4044
{
41-
Debug.Log($"{nameof(Retry属性で最初は失敗するが2回目で成功するテスト_UnityTest属性)} {++_unityTestCount}回目");
45+
Debug.Log($"{TestContext.CurrentContext.Test.Name} {++_unityTestCount}回目");
4246
yield return null;
47+
4348
Assert.That(_unityTestCount, Is.EqualTo(2));
4449
}
4550

46-
[Ignore("Retry属性はasyncテストに使用できない(Unity Test Framework v1.4.0時点)")]
47-
// See: https://unity3d.atlassian.net/servicedesk/customer/portal/2/IN-28107
48-
// Note: Unity Test Framework v1.3.5 で追加された `-retry` コマンドラインオプションは、asyncテストにも有効です
4951
[Test]
5052
[Retry(2)]
51-
public async Task Retry属性はasyncテストに使用できない_テストが終了しない()
53+
public async Task Retry属性で最初は失敗するが2回目で成功するテスト_非同期テスト()
5254
{
53-
Debug.Log($"{nameof(Retry属性はasyncテストに使用できない_テストが終了しない)} {++_asyncTestCount}回目");
55+
Debug.Log($"{TestContext.CurrentContext.Test.Name} {++_asyncTestCount}回目");
5456
await Task.Yield();
57+
5558
Assert.That(_asyncTestCount, Is.EqualTo(2));
5659
}
5760
}

0 commit comments

Comments
 (0)