In this article you’ll discover how to write and run automated tests for your WebGL game using AltUnity Tools in order to make the process easier.
When it comes to testing a WebGL build of your game there really aren’t many solutions on the market at the moment. There’s the glcheck framework which is WebGL focused, but it can help you more, if you’re looking for a solution for writing and running unit tests for your game.
If you’re resuming testing the game in Unity Editor or only do manual testing, you might spend a lot of time doing the same tests over and over again or neglect some browser performance issues. What if you can actually do test automation for your game in the browser? This is where AltUnity’s latest solution comes in handy.
AltUnity Tools are designed to make the testing of Unity games easier. So far our tools were compatible with PC, Android and iOS games, but now we also support WebGL.
These tools consist of AltUnity Tester and AltUnity Pro Alpha.
- AltUnity Tester is an asset which allows you to identify and interact with Unity objects and run end-to-end automated tests on different devices.
- AltUnity Pro Alpha is a desktop application which helps you to visualize the game object hierarchy and get components, properties, methods and fields easily without access to the source code.
To help you get started with test automation on the WebGL platform, we’ve created an example C# project. The game we chose to use in our example is “TANKS!”, a tutorial project released by Unity.
You can import it from the Unity Asset Store or you can clone our example project from gitlab. Once you have the game on your machine, open it in Unity Editor and make sure it works. To do so, open the “Complete-Game” scene from Assets, click on the “Play in Editor” button and interact with both of the tanks to see if they work properly.
Instrument your Unity game with AltUnity Tester
In order to run automated tests using AltUnity Pro Alpha, you first have to instrument your game with the AltUnity Tester asset. This will enable a connection with your game, to get information about the Unity objects.
Download the Unity Package from this article and drag it in the project’s Assets in Unity Editor. If the import was successful, you’ll see AltUnity Tools in the Unity Editor toolbar. Click on AltUnity Tools in order to open the AltUnity Tester Editor. More information about the editor can be found here.
AltUnity Tester Unity Package also includes the AltUnity Driver along with its bindings in C#, Python and Java, meaning that you can also write your tests in Python or Java, if C# is not your preferred language.
To instrument your game with AltUnity Tester, you first have to open the AltUnity Tester Editor window mentioned in the previous section and follow the steps below to build your game:
- In the Build Settings section set the Proxy host to the IP/hostname of the device where the AltUnity Pro desktop app (including the Proxy) is running .Set the Proxy port to another available port if 13000 is in use on your machine. You can also leave the default values for the host or the port. The Proxy module is explained in a more detailed manner in the next section.
- In the Scene Manager section select the scenes you want to include in your build.
- In the Platform section select the WebGL 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.
For better performance on the WebGL platform, you have to do some additional settings:
- Open the WebGL player settings and go to the Resolution and Presentation section. Check the Run In Background Setting. This setting allows you to run the game in background, which is necessary when using AltUnity Pro Alpha.
- Enable the exceptions in the Publishing Settings area.
Run your Unity game on WebGL
In order to run the tests, you need to have the WebGL build opened. You can either build and run your own game, or you can use the existing WebGL build from the example project.
If you choose the “Build & Run” option, mentioned in the previous section, then you can skip this step as Unity itself will run as a small web server to host your game.
Otherwise, if you choose the “Build” option you will need a web server to host your game. You can run a game locally by following the next steps:
- Make sure you have node.js installed.
- Type in the command line the following command ‘npm install -g http-server’.
- Go to the build folder location and in the command line write ‘http-server‘.
- From the console, take the provided address to open the game build in browser.
After you follow all the steps provided above, you should have the WebGL game instrumented with AltUnity Tester opened in the browser. The green pop-up tells us the current version of AltUnity Tester along with the Proxy Host and Port. It also shows us that the game is not connected yet to the Proxy Module but it’s waiting for the Proxy to be available.
Connect your game to AltUnity Pro Alpha
After creating a test build instrumented with AltUnity Tester, you have to get the AltUnity Pro Alpha desktop application from here. This application contains a Proxy module that allows the game and the tests to communicate with each other via an IP address or hostname. This connection is necessary because it acts as a websocket server that facilitates the communication between the tests and the instrumented Unity app. Your tests won’t be able to run until this connection is enabled.
In order to start the Proxy Module, you have to open the AltUnity Pro Alpha application and start the connection to your game build. To do so, you have to type in the Proxy Port that you set in the second section of this article.
Write and Run automated tests
Now that you have the game instrumented we can move forward and write some AltUnity tests. You can either follow our example or you can be creative and write your own automated tests.
Take a look in the documentation and explore the AltUnity Tester API. Write simple tests to get a better understanding of the framework. Try finding objects or moving the tanks. Check objects’ properties or call some methods.
If you’re not familiar with the source code of the game you are about to test, inspecting the elements in AltUnity Pro Alpha is exactly what you need. You might find it easier to see the exact fields or properties of an object in the Element Information Panel than in the Inspector window in Unity Editor. You can also copy the path of an object to use it in finding the object in the scene in a test.
For example, you can write a simple test with the help of AltUnity Pro Alpha checking the health of the tank after it takes some damage.
Notice that the Tank Object has a component named TankHealth. Click on it and go through the Methods, Fields and Properties and see if you can use any information from there.
Now we know that we have a method called TakeDamage which accepts a parameter – the amount of damage a tank receives. We also can see that the TankHealth class has a field called CurrentHealth which is initialized with 100 at start and modifies throughout the gameplay depending on the parameters of the TakeDamage method.
If we were to put all this information into a test it would look something like this:
Once you have written some tests in your test file, you’ll see them in the expanded Tests List in the AltUnity Tester Editor window. Select some of them and click on “Run Selected Tests”. Wait for them to run and check their status.
If you’re looking for a solution other than C# to write your tests, AltUnity Tester also supports other languages such as Python and Java. More information on how to run the tests in different languages can be found here.
Let us know if you tried our example and whether you found it helpful. If you have any questions or feedback, please leave us a comment below.
Join us on Discord, if you want to discuss more about test automation related topics with other game developers and game testers or if you just got stuck with the setup.