Testing Unity-built projects targeting the Android platform is a straightforward process, and can easily be added to the build-verification pipeline with a few simple steps. 


As a starter, we recommend reading Unity’s documentation for working with Android here.


This article assumes you have already installed the GameDriver agent into your Unity project, and have at least a basic test running against the Unity editor.


Unity Project Settings

Make sure your project is targeting iOS under the Unity editor > File > Build Settings, and that your project supports the following under Player Settings > Other Settings:


The Company Name, Product Name, and Version should all be filled as these will be used to populate additional fields for the player build.


Next, verify the package name and version appear as expected and that the Minimum and Target API levels are in line with the supported devices and APIs required for your project. Again, refer to the Unity documentation for more details on the API Compatibility Level required by your project.


Note: The GameDriver agent requires "ARM64" Target Architecture to run.


Installing the app on your device or emulator


Once you build your project, you will need to install the resulting .apk file on your device or emulator before you can run your test. You can then connect to the device via Unity, Android Studio, or the command-line. At a high level, the process is as follows:

  1. Build your APK via Unity
  2. Connect to the device and install the APK.
    • To do this, you will use the Android Debug Bridge (ADB) that ships with the Android Studio. For a physical device, you first need to put the device into Developer mode and enable debugging over USB or Wifi. For detailed steps, please refer to the ADB documentation.
    • You can also connect to the device/emulator automatically during the build process, by selecting it in the Unity Build Settings dialog, as shown below.
  3. Before running your test you will need to run the following command to forward GameDriver API traffic to the connected device/emulator, replacing "19734" with the port you are using in the GameDriver agent configuration in Unity. Be sure to add android_sdk/platform-toolsto your PATH environment variable first.
    • adb forward tcp:19734 tcp:19734
  4. Run your tests!
  5. Once your test has been completed, you will need to run the following command to remove the TCP port forwarding via ADB in order to continue testing via the editor.
    • adb forward --remove tcp:19734


Setting your tests to automatically connect to a device

The sample test includes an example [OneTimeSetup] for connecting to the IDE with autoplay, launching an executable, or connecting to an already running game.
//These parameters can be used to override settings used to test when running from the NUnit command line
public string testMode = TestContext.Parameters.Get("Mode", "IDE");
public string pathToExe = TestContext.Parameters.Get("pathToExe", null); // replace null with the path to your executable as needed

ApiClient api;

[OneTimeSetUp]
public void Connect()
{
      try
      {
      // First we need to create an instance of the ApiClient
      api = new ApiClient();

      // If an executable path was supplied, we will launch the standalone game
      if (pathToExe != null)
      {
            ApiClient.Launch(pathToExe);
            api.Connect("localhost", 19734, false, 30);
      }

      // If no executable path was given, we will attempt to connect to the Unity editor and initiate Play mode
      else if (testMode == "IDE")
      {
             api.Connect("localhost", 19734, true, 30);
      }
      // Otherwise, attempt to connect to an already playing game
      else api.Connect("localhost", 19734, false, 30);

      }
      catch (Exception e)
      {
              Console.WriteLine(e.ToString());
      }
      // Enable input hooking
      api.EnableHooks(HookingObject.ALL);

      //Start the Game - in this example we're waiting for an object called "StartButton" to become active, then clicking it.
      api.WaitForObject("//*[@name='StartButton']");
      api.ClickObject(MouseButtons.LEFT, "//*[@name='StartButton']", 30);
      api.Wait(3000);
      }


This doesn't perform the ADB forward for you, but could be modified to do so using step 13 in the Appium instructions (minus Appium):
string arguments = "forward tcp:19734 tcp:19734";
var process = new Process();
var startInfo = new ProcessStartInfo

{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Minimized,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
FileName = "adb",
Arguments = arguments
};

process.StartInfo = startInfo;

Then you would want to add something to the [OneTimeTeardown] to back this out with the command adb forward --remove tcp:19734 using the same approach as above.

string arguments = "forward --remove tcp:19734";
var process = new Process();
var startInfo = new ProcessStartInfo

{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Minimized,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
FileName = "adb",
Arguments = arguments
};

process.StartInfo = startInfo;