diff --git a/unity-project-with-correct-tests/Assets/Scripts/SampleComponent.cs b/unity-project-with-correct-tests/Assets/Scripts/SampleComponent.cs new file mode 100644 index 0000000..e9bd241 --- /dev/null +++ b/unity-project-with-correct-tests/Assets/Scripts/SampleComponent.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +public class SampleComponent : MonoBehaviour +{ + public BasicCounter Counter; + + void Start() + { + Counter = new BasicCounter(5); + } + + // Update is called once per frame + void Update() + { + Counter.Increment(); + } +} diff --git a/unity-project-with-correct-tests/Assets/Scripts/SampleComponent.cs.meta b/unity-project-with-correct-tests/Assets/Scripts/SampleComponent.cs.meta new file mode 100644 index 0000000..db855db --- /dev/null +++ b/unity-project-with-correct-tests/Assets/Scripts/SampleComponent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7a5f63b9ea4b465194653c4d681faf42 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity-project-with-correct-tests/Assets/Scripts/TimerComponent.cs b/unity-project-with-correct-tests/Assets/Scripts/TimerComponent.cs new file mode 100644 index 0000000..2d65e1a --- /dev/null +++ b/unity-project-with-correct-tests/Assets/Scripts/TimerComponent.cs @@ -0,0 +1,18 @@ +using UnityEngine; + +public class TimerComponent : MonoBehaviour +{ + public BasicCounter Counter = new BasicCounter(); + public float Timer = 1f; + + void Update() + { + Timer -= Time.deltaTime; + + if (Timer > 0) + return; + + Counter.Increment(); + Timer = 1f; + } +} diff --git a/unity-project-with-correct-tests/Assets/Scripts/TimerComponent.cs.meta b/unity-project-with-correct-tests/Assets/Scripts/TimerComponent.cs.meta new file mode 100644 index 0000000..453e96d --- /dev/null +++ b/unity-project-with-correct-tests/Assets/Scripts/TimerComponent.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b668d68a45bb48108ccda73269e3da7b +timeCreated: 1610056748 \ No newline at end of file diff --git a/unity-project-with-correct-tests/Assets/Tests/PlayMode/PlayModeTests.asmdef b/unity-project-with-correct-tests/Assets/Tests/PlayMode/PlayModeTests.asmdef index e15904d..13dfb6b 100644 --- a/unity-project-with-correct-tests/Assets/Tests/PlayMode/PlayModeTests.asmdef +++ b/unity-project-with-correct-tests/Assets/Tests/PlayMode/PlayModeTests.asmdef @@ -2,7 +2,8 @@ "name": "PlayModeTests", "references": [ "UnityEngine.TestRunner", - "UnityEditor.TestRunner" + "UnityEditor.TestRunner", + "MyScripts" ], "includePlatforms": [], "excludePlatforms": [], @@ -16,4 +17,4 @@ "UNITY_INCLUDE_TESTS" ], "versionDefines": [] -} \ No newline at end of file +} diff --git a/unity-project-with-correct-tests/Assets/Tests/PlayMode/SampleComponentTest.cs b/unity-project-with-correct-tests/Assets/Tests/PlayMode/SampleComponentTest.cs new file mode 100644 index 0000000..87dac53 --- /dev/null +++ b/unity-project-with-correct-tests/Assets/Tests/PlayMode/SampleComponentTest.cs @@ -0,0 +1,31 @@ +using System.Collections; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; + +namespace Tests +{ + public class SampleComponentTest + { + private GameObject target; + private SampleComponent component; + + [SetUp] + public void Setup() + { + target = GameObject.Instantiate(new GameObject()); + component = target.AddComponent(); + } + + [UnityTest] + public IEnumerator TestIncrementOnUpdateAfterNextFrame() + { + // Save the current value, since it was updated after component Start() method called + var count = component.Counter.Count; + + // Skip frame and assert the new value + yield return null; + Assert.AreEqual(count + 1, component.Counter.Count); + } + } +} diff --git a/unity-project-with-correct-tests/Assets/Tests/PlayMode/SampleComponentTest.cs.meta b/unity-project-with-correct-tests/Assets/Tests/PlayMode/SampleComponentTest.cs.meta new file mode 100644 index 0000000..cbdfcf5 --- /dev/null +++ b/unity-project-with-correct-tests/Assets/Tests/PlayMode/SampleComponentTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c688799d17b14d35ad515bff9de8d12c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity-project-with-correct-tests/Assets/Tests/PlayMode/SamplePlayModeTest.cs b/unity-project-with-correct-tests/Assets/Tests/PlayMode/SamplePlayModeTest.cs index 41cab67..27542b6 100644 --- a/unity-project-with-correct-tests/Assets/Tests/PlayMode/SamplePlayModeTest.cs +++ b/unity-project-with-correct-tests/Assets/Tests/PlayMode/SamplePlayModeTest.cs @@ -1,7 +1,5 @@ using System.Collections; -using System.Collections.Generic; using NUnit.Framework; -using UnityEngine; using UnityEngine.TestTools; namespace Tests @@ -12,8 +10,14 @@ namespace Tests [Test] public void NewTestScriptSimplePasses() { - // Use the Assert class to test conditions - Assert.True(true); + // Given + var counter = new BasicCounter(0); + + // When + counter.Increment(); + + // Then + Assert.AreEqual(1, counter.Count); } // A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use @@ -21,9 +25,18 @@ namespace Tests [UnityTest] public IEnumerator NewTestScriptWithEnumeratorPasses() { + // Given + var counter = new BasicCounter(3); + // Use the Assert class to test conditions. // Use yield to skip a frame. yield return null; + + // When + counter.Increment(); + + // Then + Assert.AreEqual(4, counter.Count); } } } diff --git a/unity-project-with-correct-tests/Assets/Tests/PlayMode/TimerComponentTest.cs b/unity-project-with-correct-tests/Assets/Tests/PlayMode/TimerComponentTest.cs new file mode 100644 index 0000000..34dd94b --- /dev/null +++ b/unity-project-with-correct-tests/Assets/Tests/PlayMode/TimerComponentTest.cs @@ -0,0 +1,66 @@ +using System.Collections; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; + +namespace Tests +{ + public class TimerComponentTest + { + private GameObject target; + private TimerComponent component; + + [SetUp] + public void Setup() + { + target = GameObject.Instantiate(new GameObject()); + component = target.AddComponent(); + } + + [UnityTest] + public IEnumerator TestIncrementAfterSomeTime() + { + // Save the current value, since it was updated after component Start() method called + var count = component.Counter.Count; + + // Skip frame and assert the new value + yield return null; + Assert.AreEqual(count, component.Counter.Count); + + yield return new WaitForSeconds(1.1f); + Assert.AreEqual(count + 1, component.Counter.Count); + + yield return new WaitForSeconds(1.1f); + Assert.AreEqual(count + 2, component.Counter.Count); + } + + [UnityTest] + public IEnumerator TestTimeScaleIsAffectingIncrement() + { + // Save the current value, since it was updated after component Start() method called + var count = component.Counter.Count; + Time.timeScale = .5f; + + // Skip frame and assert the new value + yield return null; + Assert.AreEqual(count, component.Counter.Count); + + yield return WaitForRealSeconds(1.1f); + Assert.AreEqual(count, component.Counter.Count); + + yield return WaitForRealSeconds(1.1f); + Assert.AreEqual(count + 1, component.Counter.Count); + } + + // Skipping time ignoring Time.scale + // https://answers.unity.com/questions/301868/yield-waitforseconds-outside-of-timescale.html + public static IEnumerator WaitForRealSeconds(float time) + { + float start = Time.realtimeSinceStartup; + while (Time.realtimeSinceStartup < start + time) + { + yield return null; + } + } + } +} diff --git a/unity-project-with-correct-tests/Assets/Tests/PlayMode/TimerComponentTest.cs.meta b/unity-project-with-correct-tests/Assets/Tests/PlayMode/TimerComponentTest.cs.meta new file mode 100644 index 0000000..8676afb --- /dev/null +++ b/unity-project-with-correct-tests/Assets/Tests/PlayMode/TimerComponentTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 818b22370d404398b87b47a922a435c9 +timeCreated: 1610056889 \ No newline at end of file