How to use NUnit's Timeout attribute to stop coroutine

I wrote a simple test case using Unity Test Tools, and I’d like to use NUnit’s ‘Timeout’ attribute to handle timeout declaratively.

I tried the following code, but it seems that even though timeout itself works correctly (meaning failing the test), it doesn’t stop the coroutine which causes the Unity TestRunner to hang indefinitely:

        [UnityTest, Timeout(3), Description("It should move towards the target destination.")]
        public IEnumerator ShouldMoveTowardsTargetDestination(
            [TestRange(0, 360, 45)] float heading,
            [Values(1, 10)] float distance)
        {
            Debug.LogFormat("Using data: heading={0}deg., distance={1}m.", heading, distance);

            var transform = Navigator.Transform;
            var destination = Quaternion.AngleAxis(heading, transform.up) * transform.forward * distance;

            Navigator.Destination = destination;

            while (transform.position != destination)
            {
                Navigator.LateTick();

                yield return null;
            }

            Expect(
                transform.position,
                Is.EqualTo(destination),
                "Navigator has failed to reach the destination.");
        }

Is there any way to use *Timeout* attribute, rather than programatically checking elapsed time? Or is it possible to exit the while loop once the surrounding test fails?

Thanks!

@mysticfall Did you found a solution/workaround for that ?

Sorry, I haven’t yet. Maybe we should try it again with the latest version and report an issue if it’s still broken.

Nunit supports its own timeout attribute. However I don’t think your syntax is quite right. Try something like this:

[UnityTest]
[Timeout(int.MaxValue)]
public IEnumerator someName()
{
    yield return null;
}

Of note is that for some reason there is a default 30 second timeout which if hit will cause the test to pass…So I tend to set it to max and handle any timeouts within the test itself, but your milage may vary. Hope this helps.

Other random thought…why not just assert your timeout?

Assert.That(actual, Is.EqualTo(expected).After(5000, 50));