This project is read-only.

Testing Coroutines


Here's a few simple steps to test a method that utilizes coroutines:
  1. When testing a coroutine, simply enumerate each result, one at a time. Do not call execute on your results. Remember, you are not testing the IResult implementation, but the method which uses it.
  2. Check to make sure that the correct IResult is yielded and check any properties it has which would indicate it's correct/incorrect configuration.
  3. Set any properties on the IResult which would have been set as a result of execution....but do not call execute.
  4. Go to beginning.

By following these steps you will be able to "ignore" the async part of the coroutine (if it has one) by exerting direct control over the enumeration. You can even choose to stop enumerating at any point once you have confirmed that your test passes/fails. You will probably want to use several test methods to test a single coroutine implementation in order to keep individual tests simple and small.

Here's a sample class you might use to help in enumerating the results in tests:

public class CoroutineEnumerator
{
    private readonly IEnumerator<IResult> enumerator;

    public CoroutineEnumerator(IEnumerable<IResult> enumerable)
    {
        this.enumerator = enumerable.GetEnumerator();
    }

    public CoroutineEnumerator(IEnumerator<IResult> enumerator)
    {
        this.enumerator = enumerator;
    }

    public TCoroutine Next<TCoroutine>()
    {
        while(enumerator.MoveNext())
        {
            if(enumerator.Current is TCoroutine)
                return (TCoroutine)enumerator.Current;
        }

        throw new InvalidOperationException("List of coroutines does not include " + typeof(TCoroutine).Name);
    }

    public void Finish()
    {
        while(enumerator.MoveNext()) {}
    }
}

Last edited Dec 8, 2013 at 12:55 PM by tibel, version 6