Game Automation with Unity Tutorial #2


Once you install Unity from the Unity Store, it will also install the following:-

  • Visual Studio Community Edition:-  Visual Studio is our recommended IDE to write code as it is much more advanced than MonoDevelop. It can also be downloaded from here if not installed along with Unity.
  • Visual Studio Tools for Unity – This is required for debugging. You can download it from here if not installed along with Unity.

Writing your First Automation test

The Unity Test Framework is derived from NUnit. So you can expect all NUnit features to be there in addition to some features that Unity brings along. A sample Test is illustrated here:-

ss Capture

Lets understand the above :-

[UnityTest]:- This is an attribute(read “annotation” if you are more familiar with Java) which tells the compiler that the code which follows is a Unity test case.

public IEnumerator AddSphereTest ():– IEnumerator is the return type for coroutine. A coroutine is a function that can suspend its execution until the given Yield instruction finishes. Every Unity Test should return a coroutine. For more info on coroutine, you can refer this link.

yield return StartCoroutine(AddSphere()):- This is a yield instruction which will start  another coroutine named AddSphere () and then wait for its completion.

GameObject go=GameObject.Find(“Sphere”):- The Find method of the GameObject class can be used to get a Game object from the unity scene. It can also be used to create or destroy objects.

Assert.IsNotNull(go , “Sphere is not found”):-  This is a simple NUnit Assertion. If there is any object with name “Sphere” in the unity scene, then it will pass otherwise it will fail.

GameObject Sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere):- This is  to add a primitive  sphere in the Unity scene.

Your Tests will be formed by a combination of yield instructions and assertions. For knowing more about them, you can refer the following links:-

Yield instructions in Unity

Assertions in NUnit

Happy Automating !

Nitin Mittal


1 reply »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s