Get Started
To run the first test for your Unity game you need to:
Note
If you don’t have access to source code of the game you need to ask a person with access to give you an instrumented version of the game.
Import AltUnity Tester package in Unity Editor
To instrument your Unity application with AltUnity Tester you first need to import the AltUnity Tester package into Unity.
Download AltUnity Tester Alpha.
Import it by drag and drop inside your Unity project.
Important
To make sure the import was correct, check if you can open AltUnity Tester Editor Window from Unity Editor -> AltUnity Tools -> AltUnityTester.
Instrument your game with AltUnity Tester Alpha
Steps:
Open AltUnity Tester Editor window from Unity Editor -> AltUnity Tools -> AltUnityTester
In the Build Settings section set the Proxy host to the IP/hostname of the device where the Proxy is running. Set the Proxy port to the port configured in the Proxy.
In the Scene Manager section select the scenes you want to include in your build
In the Platform section select desired platform and set the path to where you want to save the build
Press “Build Only” to instrument the game or “Build & Run” to start your instrumented game after the build succeeded
Check the console to see if the build was successful.
Note
To be able to run your instrumented game in the background, go to File -> Build Settings -> Player Settings -> Project Settings -> Player -> Resolution and presentation and check the box next to Run in background*.
Note
When running the WebGL build of your game in browser, even with the Run in background* setting enabled, you still might experience slow performance if the tab with your content is not on focus. Make sure that the tab with your app is visible, otherwise your content will only update once per second in most browsers.
Start the Proxy Module
The Proxy Module is incorporated in AltUnity Pro Alpha. In order to start it, all you have to do is to start AltUnity Pro Alpha.
Run your game in Unity or on desired platform
Before running your tests you need to start the instrumented Unity application. Upon startup, your instrumented Unity app should display a popup with the message: “Connecting to AltUnity Proxy on {ProxyHost}:{ProxyPort}”. The popup disappears when your app has successfully connected to the proxy.
Open AltUnity Tester Window
In platform section select Editor
Click Play in Editor
Open AltUnity Tester Window
In platform section select Standalone
Choose your build target
Click Build & Run
Important
Make sure to set the “Api Compatibility Level” to “.NET 4.x” in Unity when building using the Standalone option.
This setting can be found under Edit menu -> Project Settings -> Player -> Other Settings -> Configuration.
Prerequisites:
Use the Unity Hub to install Android Build Support and the required dependencies: Android SDK & NDK tools, and OpenJDK
Steps:
Open AltUnity Tester Window
In platform section select Android
Click Build & Run
Prerequisites:
Have IProxy installed:
brew install libimobiledevice
Steps:
Open AltUnity Tester Window
In platform section select iOS
Click Build & Run
Note
Check the following link to see how to build and run your game for iOS (.ipa file) – link.
Prerequisites:
Use the Unity Hub to install WebGL Build Support
Steps:
Open AltUnity Tester Window
In platform section select WebGL
Click Build & Run
Write and execute first test for your game
To write tests with AltUnity Tester you need to import the AltUnity Driver in your tests project.
AltUnity Tester package contains AltUnityDriver class used to connect to the instrumented game. In the setup method create an instance of the driver and in the tear-down method invoke the stop method of the driver. With the instance of the driver you can query the Unity objects and interact with the game.
AltUnity C# Driver is already included in AltUnity Tester package. If you are writing tests in C# then you can create your tests directly from Unity.
Create a folder named Editor in your Unity Project.
Right-click on Editor folder and select Create -> AltUnityTest. This will create a template file in which you could start to write your test.
Name the file MyFirstTest.
Open AltUnity Tester Window.
In the Run Tests section press “Run All Tests” button. You should see the output of the tests in Unity Editor Console
Example test file:
using NUnit.Framework;
using Altom.AltUnityDriver;
public class MyFirstTest
{
private AltUnityDriver altUnityDriver;
[OneTimeSetUp]
public void SetUp()
{
altUnityDriver = new AltUnityDriver();
}
[OneTimeTearDown]
public void TearDown()
{
altUnityDriver.Stop();
}
[Test]
public void TestStartGame()
{
altUnityDriver.LoadScene("Scene 2 Draggable Panel");
altUnityDriver.FindObject(By.NAME, "Close Button").Tap();
altUnityDriver.FindObject(By.NAME, "Button").Tap();
var panelElement = altUnityDriver.WaitForObject(By.NAME, "Panel");
Assert.IsTrue(panelElement.enabled);
}
}
Run your test file from the command line by using the following command:
<UnityPath>/Unity -projectPath $PROJECT_DIR -executeMethod AltUnityTestRunner.RunTestFromCommandLine -tests MyFirstTest.TestStartGame -logFile logFile.log -batchmode -quit
AltUnityDriver is available also as a nuget package. You can use the nuget package to write your tests in a separate tests project, independent of the Unity application.
Create a new test project
dotnet new nunit
Install AltUnityDriver nuget package
dotnet add package AltUnityDriver --version 1.7.0-alpha
Example test file:
using NUnit.Framework;
using Altom.AltUnityDriver;
public class MyFirstTest
{
private AltUnityDriver altUnityDriver;
[OneTimeSetUp]
public void SetUp()
{
altUnityDriver = new AltUnityDriver();
}
[OneTimeTearDown]
public void TearDown()
{
altUnityDriver.Stop();
}
[Test]
public void TestStartGame()
{
altUnityDriver.LoadScene("Scene 2 Draggable Panel");
altUnityDriver.FindObject(By.NAME, "Close Button").Tap();
altUnityDriver.FindObject(By.NAME, "Button").Tap();
var panelElement = altUnityDriver.WaitForObject(By.NAME, "Panel");
Assert.IsTrue(panelElement.enabled);
}
}
Run your tests
dotnet test
AltUnity Java Driver is available as a maven package or as a standalone jar. Use one of the following methods to import the driver in your tests project.
Method 1: Add AltUnity Java Driver as a dependency in your pom.xml file:
<dependency> <groupId>com.altom</groupId> <artifactId>altunitytester-java-client</artifactId> <version>1.7.0-alpha</version> </dependency>Method 2: Use the .jar file from GIT (without building it from source)
Download AltUnity Java Driver.
Install the .jar file:
mvn install:install-file -Dfile=./target/altunitytester-java-client-jar-with-dependencies.jar -DgroupId=ro.altom -DartifactId=altunitytester -Dversion=1.7.0-alpha -Dpackaging=jar``
Example test file:
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import ro.altom.altunitytester.AltUnityDriver;
import ro.altom.altunitytester.AltUnityObject;
import ro.altom.altunitytester.Commands.FindObject.AltFindObjectsParameters;
import ro.altom.altunitytester.Commands.FindObject.AltWaitForObjectsParameters;
import java.io.IOException;
public class myFirstTest {
private static AltUnityDriver altUnityDriver;
@BeforeClass
public static void setUp() throws IOException {
altUnityDriver = new AltUnityDriver();
}
@AfterClass
public static void tearDown() throws Exception {
altUnityDriver.stop();
}
@Test
public void openClosePanelTest() {
altUnityDriver.loadScene("Scene 2 Draggable Panel");
AltFindObjectsParameters altFindObjectsParametersCamera = new AltFindObjectsParameters
.Builder(AltUnityDriver.By.PATH, "//Main Camera")
.build();
AltUnityObject camera = altUnityDriver.findObject(altFindObjectsParametersCamera);
AltFindObjectsParameters closeButtonObjectsParameters = new AltFindObjectsParameters
.Builder(AltUnityDriver.By.NAME, "Close Button")
.withCamera(AltUnityDriver.By.ID, String.valueOf(camera.id))
.build();
altUnityDriver.findObject(closeButtonObjectsParameters).tap();
AltFindObjectsParameters buttonObjectsParameters = new AltFindObjectsParameters
.Builder(AltUnityDriver.By.NAME, "Button")
.withCamera(AltUnityDriver.By.ID, String.valueOf(camera.id))
.build();
altUnityDriver.findObject(buttonObjectsParameters).tap();
AltFindObjectsParameters panelObjectsParameters = new AltFindObjectsParameters
.Builder(AltUnityDriver.By.NAME, "Panel")
.withCamera(AltUnityDriver.By.ID, String.valueOf(camera.id))
.build();
AltWaitForObjectsParameters panelWaitForObjectsParameters = new AltWaitForObjectsParameters
.Builder(panelObjectsParameters).build();
AltUnityObject panelElement = altUnityDriver.waitForObject(panelWaitForObjectsParameters);
Assert.assertTrue(panelElement.isEnabled());
}
}
Run your tests by using the following command (in the test project folder):
mvn test
There are two methods of installing the AltUnity Python Driver pip package:
Method 1: Installing using Pip:
pip install --pre altunityrunner
Method 2: Install from the source code in the repository:
cd <project-dir>/Bindings~/python python setup.py install
Example test file:
# -*- coding: UTF-8
import os
import unittest
import sys
import json
import time
from altunityrunner import *
class MyFirstTest(unittest.TestCase):
altUnityDriver = None
@classmethod
def setUpClass(cls):
cls.altUnityDriver = AltUnityDriver()
@classmethod
def tearDownClass(cls):
cls.altUnityDriver.stop()
def test_open_close_panel(self):
self.altUnityDriver.load_scene('Scene 2 Draggable Panel')
self.altUnityDriver.find_object(By.NAME, "Close Button").tap()
self.altUnityDriver.find_object(By.NAME, "Button").tap()
panelElement = self.altUnityDriver.wait_for_object(By.NAME, "Panel")
self.assertTrue(panelElement.enabled)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(MyFirstTest)
result = unittest.TextTestRunner(verbosity=2).run(suite)
sys.exit(not result.wasSuccessful())
Run your test file by using the following command:
python <nameOfYourTestFile.py>
Now your project can use all the AltUnity Driver Commands.
Note
Before running your tests, start the Proxy and the Instrumented Unity app.