What is this used for? https://docs.unity3d.com/ScriptReference/TestTools.MonoBehaviourTest_1.html I cannot find any documentation on it at all.
It's for unit testing MonoBehaviours. It appears to be new (this is the first I'm hearing of it). But it's api appears to be you start you unit test as a coroutine, and you use this as a yield instruction to run a test on said MonoBehaviour (passed in as the generic type T0).
@techmage Snippet is from: https://docs.google.com/document/d/13zXykcNGMKl7gL0zbh3RSx4lT11qBnOV0JrKAKVxhJk/edit# Code (CSharp): [UnityTest] public IEnumerator MonoBehaviourTest_Works() { yield return new MonoBehaviourTest<MyMonoBehaviourTest>(); } public class MyMonoBehaviourTest : MonoBehaviour, IMonoBehaviourTest { private int frameCount; public bool IsTestFinished { get { return frameCount > 10; } } void Update() { frameCount++; } } This will create a new instance of the monobehaviour and wait for IsTestFinished to return true. @Tomek-Paszek I'm looking into testing a reference to a monobehaviour instead of creating a new instance. Does anyone know how this can be done? Thanks
Is there any another examples? Is it ok to use [UnityTest] inside Monobehaviour. i see that those methods marked by this attribute appeared in test runner, but I don't understand how it work. e.g. Is Monobehaviour instantiated everytime for each test method. and so on.
2.5 years later: Still zero documentation on this apart from 3 incoherent sentences that are meaningless in English. The official Unity Test Framework has even less (the entry for it is blank here: https://docs.unity3d.com/Packages/com.unity.test-framework@1.1/api/UnityEngine.TestTools.html) Anyone figured out WTF this is actually meant to do, and how it works, and what the rules are? Any info AT ALL about the specification and purpose? The name suggests its for mocking MB's. The example in the docs is absurd and would never exist in a real project (I'm not sure it's even compilable! Given that tests are required to live in a separate assembly, which means: not in the same classfile?)
I know this is a bit old but i managed to get this to work with the following test. Code (CSharp): public class ExperienceBarControllerTest { [UnityTest] public IEnumerator TestExperiencBarFillsCorrectly() { yield return new MonoBehaviourTest<ExperienceBarTest>(); } public class ExperienceBarTest : MonoBehaviour, IMonoBehaviourTest { // Need to set testFinished to true after assertions. private bool testFinished = false; private void Awake() { // Instantiate monobehaviour under test with addComponent var controller = gameObject.AddComponent<ExperienceBarController>(); // Add any serializable field references to prevent null pointer exceoptions. controller.experienceRenderer = new GameObject(); controller.experienceRenderer.AddComponent<ExperienceItemRendererController>(); controller.container = new GameObject(); // Method under test. controller.setExperience(222); // Assertions that the monobehaviour updated correctly. var itemRenderers = controller.container.GetComponentsInChildren<ExperienceItemRendererController>(); Assert.AreEqual(10, itemRenderers.Length); Assert.AreEqual(1, itemRenderers[0].getExperience()); Assert.AreEqual(1, itemRenderers[1].getExperience()); Assert.AreEqual(0.22f, itemRenderers[2].getExperience()); Assert.AreEqual(0, itemRenderers[3].getExperience()); Assert.AreEqual(0, itemRenderers[4].getExperience()); Assert.AreEqual(0, itemRenderers[5].getExperience()); Assert.AreEqual(0, itemRenderers[6].getExperience()); Assert.AreEqual(0, itemRenderers[7].getExperience()); Assert.AreEqual(0, itemRenderers[8].getExperience()); Assert.AreEqual(0, itemRenderers[9].getExperience()); // Tests finished testFinished = true; } // Required to tell test suite we are finished. public bool IsTestFinished { get { return testFinished; } } } }
This reference code might help: https://github.com/Unity-Technologi.../PlaymodeTests/BallControlMonoBehaviorTest.cs