Skip to content

Commit c2f91bb

Browse files
committed
Fix setup and teardown examples
1 parent 120a860 commit c2f91bb

19 files changed

+461
-133
lines changed
Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,57 @@
11
// Copyright (c) 2021-2025 Koji Hasegawa.
22
// This software is released under the MIT License.
33

4+
using System.Collections;
45
using System.Threading.Tasks;
56
using NUnit.Framework;
6-
using UnityEngine;
7+
using UnityEngine.TestTools;
78

89
namespace APIExamples.Editor.NUnit
910
{
1011
/// <summary>
11-
/// 非同期の<see cref="SetUpAttribute"/>の例(Edit Modeテスト)
12-
/// <see cref="OneTimeSetUpAttribute"/>はasyncサポートされていない(UTF v1.3時点)
12+
/// 非同期の<see cref="SetUpAttribute"/>の使用例(Edit Modeテスト)
13+
/// <see cref="OneTimeSetUpAttribute"/>はasyncサポートされていない(UTF v1.6.0時点)
14+
/// <p/>
1315
/// Async SetUp attribute example (in Edit Mode tests)
14-
/// Async OneTimeSetUp attribute is not yet supported in UTF v1.3
16+
/// Async OneTimeSetUp attribute is not yet supported in UTF v1.6.0
1517
/// </summary>
1618
/// <remarks>
1719
/// Required: Unity Test Framework v1.3 or later
1820
/// </remarks>
21+
/// <seealso cref="APIExamples.Editor.UnityTestFramework.UnitySetUpAttributeExample"/>
1922
[TestFixture]
2023
public class AsyncSetupAttributeExample
2124
{
25+
private int _setupCount;
26+
2227
/// <summary>
2328
/// 各テストメソッドの前に実行されます
2429
/// </summary>
2530
[SetUp]
26-
public async Task SetUp()
31+
public async Task SetUpAsync()
2732
{
2833
await Task.Yield();
29-
Debug.Log($"SetUp, {Time.time}");
34+
_setupCount++;
3035
}
3136

32-
[Test]
37+
[Test, Order(0)]
3338
public void TestMethod()
3439
{
35-
Debug.Log($"TestMethod, {Time.time}");
40+
Assert.That(_setupCount, Is.EqualTo(1), "最初のテストなのでSetUpは1回実行されている");
41+
}
42+
43+
[Test, Order(1)]
44+
public async Task TestMethodAsync()
45+
{
46+
await Task.Yield();
47+
Assert.That(_setupCount, Is.EqualTo(2), "2番目のテストなのでSetUpは2回実行されている");
3648
}
3749

38-
[Test]
39-
public void TestMethod2()
50+
[UnityTest, Order(2)]
51+
public IEnumerator UnityTestMethod()
4052
{
41-
Debug.Log($"TestMethod2, {Time.time}");
53+
yield return null;
54+
Assert.That(_setupCount, Is.EqualTo(3), "3番目のテストなのでSetUpは3回実行されている");
4255
}
4356
}
4457
}
Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,56 @@
11
// Copyright (c) 2021-2025 Koji Hasegawa.
22
// This software is released under the MIT License.
33

4+
using System.Collections;
45
using System.Threading.Tasks;
56
using NUnit.Framework;
67
using UnityEngine;
8+
using UnityEngine.TestTools;
79

810
namespace APIExamples.Editor.NUnit
911
{
1012
/// <summary>
11-
/// 非同期の<see cref="TearDownAttribute"/>の例(Edit Modeテスト)
12-
/// <see cref="OneTimeTearDownAttribute"/>はasyncサポートされていない(UTF v1.3時点)
13+
/// 非同期の<see cref="TearDownAttribute"/>の使用例(Edit Modeテスト)
14+
/// <see cref="OneTimeTearDownAttribute"/>はasyncサポートされていない(UTF v1.6.0時点)
15+
/// <p/>
1316
/// Async TearDown attribute example (in Edit Mode tests)
14-
/// Async OneTimeTearDown attribute is not yet supported in UTF v1.3
17+
/// Async OneTimeTearDown attribute is not yet supported in UTF v1.6.0
1518
/// </summary>
1619
/// <remarks>
1720
/// Required: Unity Test Framework v1.3 or later
1821
/// </remarks>
22+
/// <seealso cref="APIExamples.Editor.UnityTestFramework.UnityTearDownAttributeExample"/>
1923
[TestFixture]
2024
public class AsyncTearDownAttributeExample
2125
{
2226
/// <summary>
2327
/// 各テストメソッドの後に実行されます
2428
/// </summary>
2529
[TearDown]
26-
public async Task TearDown()
30+
public async Task TearDownAsync()
2731
{
2832
await Task.Yield();
29-
Debug.Log($"TearDown, {Time.time}");
33+
Debug.Log($"TearDownAsync");
3034
}
3135

3236
[Test]
3337
public void TestMethod()
3438
{
35-
Debug.Log($"TestMethod, {Time.time}");
39+
Debug.Log($"TestMethod");
3640
}
3741

3842
[Test]
39-
public void TestMethod2()
43+
public async Task TestMethodAsync()
4044
{
41-
Debug.Log($"TestMethod2, {Time.time}");
45+
await Task.Yield();
46+
Debug.Log($"TestMethodAsync");
47+
}
48+
49+
[UnityTest]
50+
public IEnumerator UnityTestMethod()
51+
{
52+
yield return null;
53+
Debug.Log($"UnityTestMethod");
4254
}
4355
}
4456
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2021-2025 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
using System.Collections;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
using UnityEngine.TestTools;
8+
9+
namespace APIExamples.Editor.UnityTestFramework
10+
{
11+
/// <summary>
12+
/// <see cref="UnitySetUpAttribute"/>の使用例
13+
/// </summary>
14+
/// <seealso cref="APIExamples.Editor.NUnit.AsyncSetupAttributeExample"/>
15+
[TestFixture]
16+
public class UnitySetUpAttributeExample
17+
{
18+
private int _setupCount;
19+
20+
/// <summary>
21+
/// SetUpをコルーチン書式で記述できます
22+
/// <see cref="UnityEngine.TestTools.UnityTestAttribute"/>専用ではなく、通常のTest向けのSetUpとしても使用できます
23+
/// </summary>
24+
[UnitySetUp]
25+
public IEnumerator SetUpCoroutine()
26+
{
27+
yield return null;
28+
_setupCount++;
29+
}
30+
31+
[Test, Order(0)]
32+
public void TestMethod()
33+
{
34+
Assert.That(_setupCount, Is.EqualTo(1), "最初のテストなのでSetUpは1回実行されている");
35+
}
36+
37+
[Test, Order(1)]
38+
public async Task TestMethodAsync()
39+
{
40+
await Task.Yield();
41+
Assert.That(_setupCount, Is.EqualTo(2), "2番目のテストなのでSetUpは2回実行されている");
42+
}
43+
44+
[UnityTest, Order(2)]
45+
public IEnumerator UnityTestMethod()
46+
{
47+
yield return null;
48+
Assert.That(_setupCount, Is.EqualTo(3), "3番目のテストなのでSetUpは3回実行されている");
49+
}
50+
}
51+
}

Assets/APIExamples/Tests/Editor/UnityTestFramework/UnitySetUpAttributeExample.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2021-2025 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
using System.Collections;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
using UnityEngine;
8+
using UnityEngine.TestTools;
9+
10+
namespace APIExamples.Editor.UnityTestFramework
11+
{
12+
/// <summary>
13+
/// <see cref="UnityTearDownAttribute"/>の使用例
14+
/// </summary>
15+
/// <seealso cref="APIExamples.Editor.NUnit.AsyncTearDownAttributeExample"/>
16+
[TestFixture]
17+
public class UnityTearDownAttributeExample
18+
{
19+
/// <summary>
20+
/// TearDownをコルーチン書式で記述できます
21+
/// <see cref="UnityEngine.TestTools.UnityTestAttribute"/>専用ではなく、通常のTest向けのTearDownとしても使用できます
22+
/// </summary>
23+
[UnityTearDown]
24+
public IEnumerator TearDownCoroutine()
25+
{
26+
yield return null;
27+
Debug.Log($"TearDown");
28+
}
29+
30+
[Test]
31+
public void TestMethod()
32+
{
33+
Debug.Log($"TestMethod");
34+
}
35+
36+
[Test]
37+
public async Task TestMethodAsync()
38+
{
39+
await Task.Yield();
40+
Debug.Log($"TestMethodAsync");
41+
}
42+
43+
[UnityTest]
44+
public IEnumerator UnityTestMethod()
45+
{
46+
yield return null;
47+
Debug.Log($"UnityTestMethod");
48+
}
49+
}
50+
}

Assets/APIExamples/Tests/Editor/UnityTestFramework/UnityTearDownAttributeExample.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,61 @@
11
// Copyright (c) 2021-2025 Koji Hasegawa.
22
// This software is released under the MIT License.
33

4+
using System.Collections;
5+
using System.Diagnostics.CodeAnalysis;
46
using System.Threading.Tasks;
57
using NUnit.Framework;
6-
using UnityEngine;
8+
using UnityEngine.TestTools;
79

810
namespace APIExamples.NUnit
911
{
1012
/// <summary>
11-
/// 非同期の<see cref="SetUpAttribute"/>の例
12-
/// <see cref="OneTimeSetUpAttribute"/>はasyncサポートされていない(UTF v1.3時点)
13+
/// 非同期の<see cref="SetUpAttribute"/>の使用例
14+
/// <see cref="OneTimeSetUpAttribute"/>はasyncサポートされていない(UTF v1.6.0時点)
15+
/// <p/>
1316
/// Async SetUp attribute example
14-
/// Async OneTimeSetUp attribute is not yet supported in UTF v1.3
17+
/// Async OneTimeSetUp attribute is not yet supported in UTF v1.6.0
1518
/// </summary>
1619
/// <remarks>
1720
/// Required: Unity Test Framework v1.3 or later
1821
/// </remarks>
22+
/// <seealso cref="SetupAttributeExample"/>
23+
/// <seealso cref="OneTimeSetupAttributeExample"/>
24+
/// <seealso cref="APIExamples.UnityTestFramework.UnitySetUpAttributeExample"/>
1925
[TestFixture]
26+
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType")]
2027
public class AsyncSetupAttributeExample
2128
{
29+
private int _setupCount;
30+
2231
/// <summary>
2332
/// 各テストメソッドの前に実行されます
2433
/// </summary>
2534
[SetUp]
26-
public async Task SetUp()
35+
public async Task SetUpAsync()
2736
{
2837
await Task.Yield();
29-
Debug.Log($"SetUp, {Time.time}");
38+
_setupCount++;
3039
}
3140

32-
[Test]
41+
[Test, Order(0)]
3342
public void TestMethod()
3443
{
35-
Debug.Log($"TestMethod, {Time.time}");
44+
Assert.That(_setupCount, Is.EqualTo(1), "最初のテストなのでSetUpは1回実行されている");
45+
}
46+
47+
[Test, Order(1)]
48+
public async Task TestMethodAsync()
49+
{
50+
await Task.Yield();
51+
Assert.That(_setupCount, Is.EqualTo(2), "2番目のテストなのでSetUpは2回実行されている");
3652
}
3753

38-
[Test]
39-
public void TestMethod2()
54+
[UnityTest, Order(2)]
55+
public IEnumerator UnityTestMethod()
4056
{
41-
Debug.Log($"TestMethod2, {Time.time}");
57+
yield return null;
58+
Assert.That(_setupCount, Is.EqualTo(3), "3番目のテストなのでSetUpは3回実行されている");
4259
}
4360
}
4461
}
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,58 @@
11
// Copyright (c) 2021-2025 Koji Hasegawa.
22
// This software is released under the MIT License.
33

4+
using System.Collections;
45
using System.Threading.Tasks;
56
using NUnit.Framework;
67
using UnityEngine;
8+
using UnityEngine.TestTools;
79

810
namespace APIExamples.NUnit
911
{
1012
/// <summary>
11-
/// 非同期の<see cref="TearDownAttribute"/>の例
12-
/// <see cref="OneTimeTearDownAttribute"/>はasyncサポートされていない(UTF v1.3時点)
13+
/// 非同期の<see cref="TearDownAttribute"/>の使用例
14+
/// <see cref="OneTimeTearDownAttribute"/>はasyncサポートされていない(UTF v1.6.0時点)
15+
/// <p/>
1316
/// Async TearDown attribute example
14-
/// Async OneTimeTearDown attribute is not yet supported in UTF v1.3
17+
/// Async OneTimeTearDown attribute is not yet supported in UTF v1.6.0
1518
/// </summary>
1619
/// <remarks>
1720
/// Required: Unity Test Framework v1.3 or later
1821
/// </remarks>
22+
/// <seealso cref="TearDownAttributeExample"/>
23+
/// <seealso cref="OneTimeTearDownAttributeExample"/>
24+
/// <seealso cref="APIExamples.UnityTestFramework.UnityTearDownAttributeExample"/>
1925
[TestFixture]
2026
public class AsyncTearDownAttributeExample
2127
{
2228
/// <summary>
2329
/// 各テストメソッドの後に実行されます
2430
/// </summary>
2531
[TearDown]
26-
public async Task TearDown()
32+
public async Task TearDownAsync()
2733
{
2834
await Task.Yield();
29-
Debug.Log($"TearDown, {Time.time}");
35+
Debug.Log($"TearDownAsync");
3036
}
3137

3238
[Test]
3339
public void TestMethod()
3440
{
35-
Debug.Log($"TestMethod, {Time.time}");
41+
Debug.Log($"TestMethod");
3642
}
3743

3844
[Test]
39-
public void TestMethod2()
45+
public async Task TestMethodAsync()
4046
{
41-
Debug.Log($"TestMethod2, {Time.time}");
47+
await Task.Yield();
48+
Debug.Log($"TestMethodAsync");
49+
}
50+
51+
[UnityTest]
52+
public IEnumerator UnityTestMethod()
53+
{
54+
yield return null;
55+
Debug.Log($"UnityTestMethod");
4256
}
4357
}
4458
}

0 commit comments

Comments
 (0)