- Home /
Asynchronous unit testing
Unity recently released their suite of testing tools (http://blogs.unity3d.com/2013/12/18/unity-test-tools-released/). 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?
Answer by liortal · May 20, 2014 at 08:54 PM
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: http://wp.me/p4kypp-6s
Unity testing on a async method is not wrong and it's not an integration test. Unfortunately, Unity Test Tools doesn't seem to support async unity testing.
Fore example, Unity has a class called WWW for http requests. If you want to unity test a method which uses WWW class, you won't be able to do it with Unity test tools because WWW class is async.
Different people use different definitions. I consider an integration test as a test involving the interaction between 2 different classes (or systems). In this case, your own code working with the WWW class is considered (by me) to be an integration between these 2 components. What kind of test would you like to perform ? there's nothing preventing you from starting a coroutine just for testing and waiting for it to complete (or whatever you need to test).
If you implement your own async class and if you want to unit test it, you want be able to do so, not because it's dependent on another class, just because it's async. This is not about different definitions. Unity test tools doesn't support async unit testing unfortunately.
yes i have this problem when i tried to implement UNET tests.