- Home /
How to refactor UnityTests - seperating methods with yield
Hello :)
I wonder how to seperate methods which return IEnumerator with yield. For example I have the following nearly identical tests (it doens't matter if the tests make sense):
[UnityTest]
public IEnumerator WaitFor1Second()
{
float startTime = Time.time;
yield return new WaitForSeconds(1f);
Assert.AreEqual(1f, Time.time - startTime, 0.1f);
}
[UnityTest]
public IEnumerator WaitFor2Seconds()
{
float startTime = Time.time;
yield return new WaitForSeconds(2f);
Assert.AreEqual(2f, Time.time - startTime, 0.1f);
}
How can I split those tests up? At the end it should look like this:
[UnityTest]
public IEnumerator WaitFor1Second()
{
yield return WaitSecondsTest(1f);
}
[UnityTest]
public IEnumerator WaitFor2Seconds()
{
yield return WaitSecondsTest(2f);
}
Thanks and have a great day!
Answer by HaraldNielsen · Apr 08, 2018 at 05:49 PM
Hi @Zwer99 Playmode UnityTest tests start a Coroutine, and Coroutines can handle nested IEnumerator's, so you can just yield IEnumerator from a method. So you should be able to:
IEnumerator WaitSecondsTest(float seconds)
{
float startTime = Time.time;
yield return new WaitForSeconds(1f);
Assert.AreEqual(1f, Time.time - startTime, 0.1f);
}
This is at least expected. If you see anything other than this its a bug
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Tests and code coverage analysis 0 Answers
Having trouble refactoring an IEnumerator method with multiple yields 1 Answer
Flip over an object (smooth transition) 3 Answers