How to refactor UnityTests - seperating methods with yield

Hello :slight_smile:

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!

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