Write your first test#
On this page you write a test that opens a scene in your app and interacts with its objects. Choose your language in any of the tabs below: the others switch with it.
Before you start#
AltTester® Desktop is running. It starts AltTester® Server, which your app and your tests connect to. See Start the AltTester® Server.
Your instrumented app is running and connected. The green AltTester® pop-up in the app first shows
Waiting to connect to AltTester® Server on IP:port with app name: 'your app name', thenConnected to AltTester® Server…. If the app runs on another device, enter the IP of the machine where AltTester® Desktop runs in the pop-up and press Restart. See Connecting to the server.The tools for your language: .NET for C#, Java, Python, or Robot Framework. C# tests can also run inside the Unity Editor, with nothing else to install.
1. Set up a test project#
Create an NUnit project in an empty folder, and add the AltTester® driver to it:
dotnet new nunit
dotnet add package AltTester-Driver --version x.y.z
x.y.z is the driver version; see Compatibility for the versions that work
with your AltTester® Unity SDK.
The C# driver comes with the AltTester® package, so you can write the tests in your Unity project:
Create a folder named
Editorin your Unity project.Right-click the
Editorfolder and choose Create > AltTest. This creates a test file from a template.Name the file
MyFirstTest.
Create a Maven project,
for example in IntelliJ IDEA, and add the AltTester® driver and JUnit to its pom.xml.
Use the latest driver version:
<dependency>
<groupId>com.alttester</groupId>
<artifactId>alttester</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.4</version>
</dependency>
Install pytest and the AltTester® driver:
pip install pytest AltTester-Driver
Install the AltTester® library for Robot Framework:
pip install alttester-robotframework-library
2. Write the test#
The test connects to AltTester® Server when it starts, and stops the driver when it ends. In between it loads a scene and works with its objects: it finds them, taps or drags them, and checks the result.
using NUnit.Framework;
using AltTester.AltTesterSDK.Driver;
public class MyFirstTest
{
private AltDriver altDriver;
[OneTimeSetUp]
public void SetUp()
{
altDriver = new AltDriver();
}
[OneTimeTearDown]
public void TearDown()
{
altDriver.Stop();
}
[Test]
public void TestStartGame()
{
altDriver.LoadScene("Scene 2 Draggable Panel");
altDriver.FindObject(By.NAME, "Close Button").Tap();
altDriver.FindObject(By.NAME, "Button").Tap();
var panelElement = altDriver.WaitForObject(By.NAME, "Panel");
Assert.IsTrue(panelElement.enabled);
}
}
Replace the content of MyFirstTest:
using NUnit.Framework;
using AltTester.AltTesterSDK.Driver;
public class MyFirstTest
{
private AltDriver altDriver;
[OneTimeSetUp]
public void SetUp()
{
altDriver = new AltDriver();
}
[OneTimeTearDown]
public void TearDown()
{
altDriver.Stop();
}
[Test]
public void TestStartGame()
{
altDriver.LoadScene("Scene 2 Draggable Panel");
altDriver.FindObject(By.NAME, "Close Button").Tap();
altDriver.FindObject(By.NAME, "Button").Tap();
var panelElement = altDriver.WaitForObject(By.NAME, "Panel");
Assert.IsTrue(panelElement.enabled);
}
}
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import com.alttester.AltDriver;
import com.alttester.AltObject;
import com.alttester.Commands.FindObject.AltFindObjectsParams;
import com.alttester.Commands.FindObject.AltWaitForObjectsParams;
import com.alttester.Commands.UnityCommand.AltLoadSceneParams;
import java.io.IOException;
public class MyFirstTest {
private static AltDriver altDriver;
@BeforeClass
public static void setUp() throws IOException {
altDriver = new AltDriver();
}
@AfterClass
public static void tearDown() throws Exception {
altDriver.stop();
}
@Test
public void openClosePanelTest() {
altDriver.loadScene(new AltLoadSceneParams.Builder("Scene 2 Draggable Panel").build());
AltFindObjectsParams altFindObjectsParametersCamera = new AltFindObjectsParams.Builder(
AltDriver.By.PATH, "//Main Camera")
.build();
AltObject camera = altDriver.findObject(altFindObjectsParametersCamera);
AltFindObjectsParams closeButtonObjectsParameters = new AltFindObjectsParams.Builder(
AltDriver.By.NAME, "Close Button")
.withCamera(AltDriver.By.ID, String.valueOf(camera.id))
.build();
altDriver.findObject(closeButtonObjectsParameters).tap();
AltFindObjectsParams buttonObjectsParameters = new AltFindObjectsParams.Builder(
AltDriver.By.NAME, "Button")
.withCamera(AltDriver.By.ID, String.valueOf(camera.id))
.build();
altDriver.findObject(buttonObjectsParameters).tap();
AltFindObjectsParams panelObjectsParameters = new AltFindObjectsParams.Builder(
AltDriver.By.NAME, "Panel")
.withCamera(AltDriver.By.ID, String.valueOf(camera.id))
.build();
AltWaitForObjectsParams panelWaitForObjectsParameters = new AltWaitForObjectsParams.Builder(
panelObjectsParameters).build();
AltObject panelElement = altDriver.waitForObject(panelWaitForObjectsParameters);
Assert.assertTrue(panelElement.enabled);
}
}
Save it as tests/test_my_first_test.py:
import unittest
from alttester import *
class MyFirstTest(unittest.TestCase):
alt_driver = None
@classmethod
def setUpClass(cls):
cls.alt_driver = AltDriver()
@classmethod
def tearDownClass(cls):
cls.alt_driver.stop()
def test_open_close_panel(self):
self.alt_driver.load_scene('Scene 2 Draggable Panel')
self.alt_driver.find_object(By.NAME, "Close Button").tap()
self.alt_driver.find_object(By.NAME, "Button").tap()
panel_element = self.alt_driver.wait_for_object(By.NAME, "Panel")
self.assertTrue(panel_element.enabled)
Save it as my_first_test.robot. Initialize Altdriver connects with the default host,
port and app name; pass them as arguments if your app uses others.
*** Settings***
Library AltTesterLibrary
Suite Setup Initialize Altdriver
Suite Teardown Stop Altdriver
*** Test Cases ***
Test Resize Panel
Load Scene Scene 2 Draggable Panel
${alt_object}= Find Object NAME Resize Zone
${alt_object_x}= Get Object X ${alt_object}
${alt_object_y}= Get Object Y ${alt_object}
${position_init}= Create List ${alt_object_x} ${alt_object_y}
${screen_position}= Get Screen Position ${alt_object}
${new_x}= Evaluate ${alt_object_x}-200
${new_y}= Evaluate ${alt_object_y}-200
${new_screen_position}= Create List ${new_x} ${new_y}
Swipe ${screen_position} ${new_screen_position} duration=2
${alt_object}= Find Object NAME Resize Zone
${alt_object_x}= Get Object X ${alt_object}
${alt_object_y}= Get Object Y ${alt_object}
${position_final}= Create List ${alt_object_x} ${alt_object_y}
Should Not Be Equal ${position_init} ${position_final}
On Android#
An app on an Android device connected by USB reaches AltTester® Server through reverse port forwarding. The test sets it up before it connects and removes it at the end. See Reverse port forwarding for the same test with those lines added, in every language.
On iOS#
The test doesn’t change. Enter the IP of the machine where AltTester® Server runs in the app’s pop-up, as described in Connecting to the server.
3. Run the test#
In the project folder, run:
dotnet test
Open the AltTester® Editor and, in the Run Tests section, press Run All Tests. The results appear in the Unity Editor console.
To run the test from the command line instead:
<UnityPath>/Unity -projectPath $PROJECT_DIR -executeMethod AltTestRunner.RunTestFromCommandLine -tests MyFirstTest.TestStartGame -logFile logFile.log -batchmode -quit
Run the test with your IDE’s test runner, for example IntelliJ IDEA’s.
In the folder that contains tests, run:
pytest tests/
To run one file, or one test in it:
pytest tests/test_my_first_test.py
pytest tests/test_my_first_test.py -k test_open_close_panel
Run the file, or a folder of test files:
robot my_first_test.robot
robot path/to/my_tests/
Robot Framework writes report.html, log.html and output.xml. Open log.html
in a browser to see each test’s status, its keywords and any error.
Next steps#
AltDriver and the rest of the Reference list every command you can use.
Choosing a selector and Writing reliable tests help tests keep working as the app changes. As a suite grows, a pattern such as Page Object Model keeps it organized.
Running tests in CI runs the suite on a build server.