Skip to content

Commit 7cc100b

Browse files
committed
Add UnityOneTimeSetUp and TearDown attribute examples (UTF 1.5+)
1 parent 0da6192 commit 7cc100b

8 files changed

+314
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) 2021-2025 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
#if ENABLE_UTF_1_5
5+
using System.Collections;
6+
using System.Diagnostics.CodeAnalysis;
7+
using System.Threading.Tasks;
8+
using APIExamples.NUnit;
9+
using NUnit.Framework;
10+
using UnityEngine.TestTools;
11+
using Is = APIExamples.NUnit.Is;
12+
13+
namespace APIExamples.Editor.UnityTestFramework
14+
{
15+
/// <summary>
16+
/// <see cref="UnityOneTimeSetupAttributeExample"/>の使用例
17+
/// </summary>
18+
/// <remarks>
19+
/// Required: Unity Test Framework v1.5 or later
20+
/// </remarks>
21+
/// <seealso cref="AsyncSetupAttributeExample"/>
22+
/// <seealso cref="UnitySetUpAttributeExample"/>
23+
[TestFixture]
24+
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType")]
25+
public class UnityOneTimeSetupAttributeExample
26+
{
27+
private int _oneTimeSetupCount;
28+
private int _setupCount;
29+
30+
/// <summary>
31+
/// テストクラス内の最初のテストの実行前に一度だけ実行されます
32+
/// </summary>
33+
[UnityOneTimeSetUp]
34+
public IEnumerator OneTimeSetUp()
35+
{
36+
yield return null;
37+
_oneTimeSetupCount++;
38+
}
39+
40+
/// <summary>
41+
/// 各テストメソッドの前に実行されます
42+
/// </summary>
43+
[SetUp]
44+
public void SetUp()
45+
{
46+
_setupCount++;
47+
}
48+
49+
[Test, Order(0)]
50+
public void TestMethod()
51+
{
52+
Assert.That(_oneTimeSetupCount, Is.EqualTo(1), "OneTimeSetUpはTestFixtureごとに一度だけ実行される");
53+
Assert.That(_setupCount, Is.EqualTo(1), "最初のテストなのでSetUpは1回実行されている");
54+
}
55+
56+
[Test, Order(1)]
57+
public async Task TestMethodAsync()
58+
{
59+
await Task.Yield();
60+
Assert.That(_oneTimeSetupCount, Is.EqualTo(1), "OneTimeSetUpはTestFixtureごとに一度だけ実行される");
61+
Assert.That(_setupCount, Is.EqualTo(2), "2番目のテストなのでSetUpは2回実行されている");
62+
}
63+
64+
[UnityTest, Order(2)]
65+
public IEnumerator UnityTestMethod()
66+
{
67+
yield return null;
68+
Assert.That(_oneTimeSetupCount, Is.EqualTo(1), "OneTimeSetUpはTestFixtureごとに一度だけ実行される");
69+
Assert.That(_setupCount, Is.EqualTo(3), "3番目のテストなのでSetUpは3回実行されている");
70+
}
71+
}
72+
}
73+
#endif

Assets/APIExamples/Tests/Editor/UnityTestFramework/UnityOneTimeSetupAttributeExample.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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2021-2025 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
#if ENABLE_UTF_1_5
5+
using System.Collections;
6+
using System.Diagnostics.CodeAnalysis;
7+
using System.Threading.Tasks;
8+
using APIExamples.NUnit;
9+
using NUnit.Framework;
10+
using UnityEngine;
11+
using UnityEngine.TestTools;
12+
using Is = APIExamples.NUnit.Is;
13+
14+
namespace APIExamples.Editor.UnityTestFramework
15+
{
16+
/// <summary>
17+
/// <see cref="UnityOneTimeTearDownAttributeExample"/>の使用例
18+
/// </summary>
19+
/// <remarks>
20+
/// Required: Unity Test Framework v1.5 or later
21+
/// </remarks>
22+
/// <seealso cref="AsyncTearDownAttributeExample"/>
23+
/// <seealso cref="UnityTearDownAttributeExample"/>
24+
[TestFixture]
25+
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType")]
26+
public class UnityOneTimeTearDownAttributeExample
27+
{
28+
private int _oneTimeTeardownCount;
29+
private int _teardownCount;
30+
31+
/// <summary>
32+
/// クラス内の最後のテストの実行後に一度だけ実行されます
33+
/// </summary>
34+
[UnityOneTimeTearDown]
35+
public IEnumerator OneTimeTearDown()
36+
{
37+
yield return null;
38+
Debug.Log($"UnityOneTimeTearDown");
39+
_oneTimeTeardownCount++;
40+
41+
Assert.That(_oneTimeTeardownCount, Is.EqualTo(1), "OneTimeTearDownはTestFixtureごとに一度だけ実行される");
42+
Assert.That(_teardownCount, Is.EqualTo(3), "3つのテストがあるのでTearDownは3回実行されている");
43+
}
44+
45+
/// <summary>
46+
/// 各テストメソッドの後に実行されます
47+
/// </summary>
48+
[TearDown]
49+
public void TearDown()
50+
{
51+
Debug.Log($"TearDown");
52+
_teardownCount++;
53+
}
54+
55+
[Test]
56+
public void TestMethod()
57+
{
58+
Debug.Log($"TestMethod");
59+
}
60+
61+
[Test]
62+
public async Task TestMethodAsync()
63+
{
64+
await Task.Yield();
65+
Debug.Log($"TestMethodAsync");
66+
}
67+
68+
[UnityTest]
69+
public IEnumerator UnityTestMethod()
70+
{
71+
yield return null;
72+
Debug.Log($"UnityTestMethod");
73+
}
74+
}
75+
}
76+
#endif

Assets/APIExamples/Tests/Editor/UnityTestFramework/UnityOneTimeTearDownAttributeExample.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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) 2021-2025 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
#if ENABLE_UTF_1_5
5+
using System.Collections;
6+
using System.Diagnostics.CodeAnalysis;
7+
using System.Threading.Tasks;
8+
using APIExamples.NUnit;
9+
using NUnit.Framework;
10+
using UnityEngine.TestTools;
11+
using Is = APIExamples.NUnit.Is;
12+
13+
namespace APIExamples.UnityTestFramework
14+
{
15+
/// <summary>
16+
/// <see cref="UnityOneTimeSetupAttributeExample"/>の使用例
17+
/// </summary>
18+
/// <remarks>
19+
/// Required: Unity Test Framework v1.5 or later
20+
/// </remarks>
21+
/// <seealso cref="SetupAttributeExample"/>
22+
/// <seealso cref="OneTimeSetupAttributeExample"/>
23+
/// <seealso cref="AsyncSetupAttributeExample"/>
24+
/// <seealso cref="UnitySetUpAttributeExample"/>
25+
[TestFixture]
26+
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType")]
27+
public class UnityOneTimeSetupAttributeExample
28+
{
29+
private int _oneTimeSetupCount;
30+
private int _setupCount;
31+
32+
/// <summary>
33+
/// テストクラス内の最初のテストの実行前に一度だけ実行されます
34+
/// </summary>
35+
[UnityOneTimeSetUp]
36+
public IEnumerator OneTimeSetUp()
37+
{
38+
yield return null;
39+
_oneTimeSetupCount++;
40+
}
41+
42+
/// <summary>
43+
/// 各テストメソッドの前に実行されます
44+
/// </summary>
45+
[SetUp]
46+
public void SetUp()
47+
{
48+
_setupCount++;
49+
}
50+
51+
[Test, Order(0)]
52+
public void TestMethod()
53+
{
54+
Assert.That(_oneTimeSetupCount, Is.EqualTo(1), "OneTimeSetUpはTestFixtureごとに一度だけ実行される");
55+
Assert.That(_setupCount, Is.EqualTo(1), "最初のテストなのでSetUpは1回実行されている");
56+
}
57+
58+
[Test, Order(1)]
59+
public async Task TestMethodAsync()
60+
{
61+
await Task.Yield();
62+
Assert.That(_oneTimeSetupCount, Is.EqualTo(1), "OneTimeSetUpはTestFixtureごとに一度だけ実行される");
63+
Assert.That(_setupCount, Is.EqualTo(2), "2番目のテストなのでSetUpは2回実行されている");
64+
}
65+
66+
[UnityTest, Order(2)]
67+
public IEnumerator UnityTestMethod()
68+
{
69+
yield return null;
70+
Assert.That(_oneTimeSetupCount, Is.EqualTo(1), "OneTimeSetUpはTestFixtureごとに一度だけ実行される");
71+
Assert.That(_setupCount, Is.EqualTo(3), "3番目のテストなのでSetUpは3回実行されている");
72+
}
73+
}
74+
}
75+
#endif

Assets/APIExamples/Tests/Runtime/UnityTestFramework/UnityOneTimeSetupAttributeExample.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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) 2021-2025 Koji Hasegawa.
2+
// This software is released under the MIT License.
3+
4+
#if ENABLE_UTF_1_5
5+
using System.Collections;
6+
using System.Diagnostics.CodeAnalysis;
7+
using System.Threading.Tasks;
8+
using APIExamples.NUnit;
9+
using NUnit.Framework;
10+
using UnityEngine;
11+
using UnityEngine.TestTools;
12+
using Is = APIExamples.NUnit.Is;
13+
14+
namespace APIExamples.UnityTestFramework
15+
{
16+
/// <summary>
17+
/// <see cref="UnityOneTimeTearDownAttributeExample"/>の使用例
18+
/// </summary>
19+
/// <remarks>
20+
/// Required: Unity Test Framework v1.5 or later
21+
/// </remarks>
22+
/// <seealso cref="TearDownAttributeExample"/>
23+
/// <seealso cref="OneTimeTearDownAttributeExample"/>
24+
/// <seealso cref="AsyncTearDownAttributeExample"/>
25+
/// <seealso cref="UnityTearDownAttributeExample"/>
26+
[TestFixture]
27+
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType")]
28+
public class UnityOneTimeTearDownAttributeExample
29+
{
30+
private int _oneTimeTeardownCount;
31+
private int _teardownCount;
32+
33+
/// <summary>
34+
/// クラス内の最後のテストの実行後に一度だけ実行されます
35+
/// </summary>
36+
[UnityOneTimeTearDown]
37+
public IEnumerator OneTimeTearDown()
38+
{
39+
yield return null;
40+
Debug.Log($"UnityOneTimeTearDown");
41+
_oneTimeTeardownCount++;
42+
43+
Assert.That(_oneTimeTeardownCount, Is.EqualTo(1), "OneTimeTearDownはTestFixtureごとに一度だけ実行される");
44+
Assert.That(_teardownCount, Is.EqualTo(3), "3つのテストがあるのでTearDownは3回実行されている");
45+
}
46+
47+
/// <summary>
48+
/// 各テストメソッドの後に実行されます
49+
/// </summary>
50+
[TearDown]
51+
public void TearDown()
52+
{
53+
Debug.Log($"TearDown");
54+
_teardownCount++;
55+
}
56+
57+
[Test]
58+
public void TestMethod()
59+
{
60+
Debug.Log($"TestMethod");
61+
}
62+
63+
[Test]
64+
public async Task TestMethodAsync()
65+
{
66+
await Task.Yield();
67+
Debug.Log($"TestMethodAsync");
68+
}
69+
70+
[UnityTest]
71+
public IEnumerator UnityTestMethod()
72+
{
73+
yield return null;
74+
Debug.Log($"UnityTestMethod");
75+
}
76+
}
77+
}
78+
#endif

Assets/APIExamples/Tests/Runtime/UnityTestFramework/UnityOneTimeTearDownAttributeExample.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.

0 commit comments

Comments
 (0)