Asynchronous unit testing

Unity recently released their suite of testing tools (Unity Test Tools Released | Unity Blog). These tools include NUnit for unit testing, which I want to make use of.

The component being tested performs various asynchronous tasks. These tasks use the Unity scheduler (coroutines, yield, invoke etc.) and signal completion via callbacks. To verify that this works correctly, I need the unit test to wait for the task to complete before asserting.

However, because these tasks use the Unity scheduler, I cannot simply block in the unit test until the tasks complete, since that blocks the main thread and thus the scheduler, and so the task will never complete. But if I return from the unit test, to free up the main thread, the test will complete before the callback has arrived and any assertions are made.

How can I run asynchronous processes that require the Unity main thread to not be blocked in a unit test with NUnit/Unity’s test tools?

Sounds like what you’re doing is not a unit test at all. It has too many external dependencies on the Unity engine.

A unit test runs in isolation and shouldn’t rely on anything else, making it deterministic.

See this tutorial for basic setup for integration tests using Unity Test Tools: Introduction to Unity Test ToolsRandom Bits