Using the Command Line Toolset to Run Unity Tests

产品设计 2016-06-09

In the game development cycle, Quality Assurance and testing can be tedious and time consuming. Because of this, these tasks are all too often delayed for small studios. Things like
critical bug fixes or new features typically take a higher priority and time is not consistently spent on automated testing solutions.

That said, every so often a bug gets leaked into a release or a mysterious compile error crops up in the main code repository, making the need for testing clear again. To mitigate these risks, teams will typically perform unit testing with tools like nUnit or Google Test. However, additional steps are required to execute Unity specific code and tests through an automated process.

Having a programmatic way of integrating testing tools with Unity can benefit teams greatly by offloading repetitive tasks like build releases or running unit tests on an automated system. Luckily Unity has an often overlooked feature: its command line toolset. This is available on all supported Unity editor platforms, including the Unity Linux beta. By using the command line toolset, teams can automate events like builds and unit testing.

For our team at KinematicSoup, we chose to use Unity’s command line as it provided us with the flexibility we needed. In our case, when we learned that Unity for Linux supported the command line toolset, we wanted to be able to migrate our deployment and build systems to Linux. To do this, we needed the Windows deployment and asset bundle builder tools we built for our current system to work on Linux in the future. The command line toolset in Unity allowed us to do this, and here’s how:

Using Command Line methods

The basic format of invoking Unity command line methods is to call the Unity editor application followed by the command line options: “/Unity.exe ”. In order to do this you will need to know the paths of the installation locations for Unity. The default Unity install locations for Windows, Mac and Linux are as follows:

  • Windows: C:Program FilesUnityEditorUnity.exe
  • OSX: /Applications/Unity/
  • Linux: /opt/Unity/Editor/Unity

Before we discuss how to run custom classes and methods through the command line, let’s briefly review the built in Unity command line options. The built in functions allow you to do things like build an asset package or build a targeted release. The following list is an excerpt from the full documentation

  • -batchmode -
    this should always be present when calling Unity from the command line, it lets Unity know not to open pop ups.

  • -nographics -
    tells Unity to not load any GUI or windows. Unfortunately Unity will still load the Direct X frameworks which will throw errors on systems without graphics capabilities. For example, a headless Linux build machine. We will discuss a work around for this later in the blog.

  • -projectPath -
    opens the Unity project at the specified path. This will likely also be used every time.

  • -logFile -
    Unity will redirect the log output to this file. This is really useful for builds and debugging.

  • -buildWindows64Player -
    builds a win64 version of your project.

  • -importPackage -
    imports the package at the given location.

  • -exportPackage -
    exports the assets at into an asset bundle at the location and name of .

Invoking Custom Methods

The Unity command line functions are often not enough to complete required tasks, which is where the ability to invoke custom methods becomes very helpful. The syntax to call a custom Unity method is as follows:

/unity.exe  ... -projectPath  -exit -batchmode -executeMethod 

Where parameter1 through parameterN are strings to pass into your Unity method.

This can be used to call a public static method where the script is saved in an “Editor” folder (resides in a folder in the assets directory with the name “Editor”). Below is an example of a generic C# class that could be called from the command line.

using UnityEngine;
using UnityEditor;
using System;

Namespace myNameSpace
   class myClass
       Public static void myMethod()
          string[] params;
          params = Environment.GetCommandLineArgs();

          //DO SOMETHING 

In order to parse the command line parameters that are passed in, the Unity class will need to use the query Environment.GetCommandLineArgs(), which will return a string array of arguments. From here you can do whatever is needed, whether making a web request or incrementing a build.

Special Notes on building with Headless Systems

When running Unity on a headless system you will need some sort of virtual frame buffer. As previously mentioned, the -nographics option will prevent any windows from loading but still loads the DX11 environment. On Linux the fix is quite simple, just use the xvfb package (on Ubuntu open a console and run “sudo apt-get install xvfb”) then append xvfb-run in front of your Unity command. For example:

 xvfb-run ../unity/Editor/unity…

This should give you a basic idea of how to use the Unity command line interface and how it can be used integrate automated tests with Unity. As your team grows, more rigorous testing will be a vital part of ensuring smooth production. Utilizing automated testing can greatly decrease the time it takes to test your software, and provides a tool that can be used consistently throughout development. Interfacing these tests with the Unity platform takes this one step further and gives your tests the flexibility and adaptability required for modern game development.

To learn more about what we are working on to help developers build better games and ship them sooner, check out our Unity multi-user scene collaboration tool, Scene Fusion

References :

Written by Bob Cao, developer at KinematicSoup.

责编内容by:KinematicSoup (源链)。感谢您的支持!


开始学习Unity 开始学习Unity了. 3D时代来临,作为一个程序员,一定是在考虑自已的未来方向。其实Cocos3D也好,Unity也罢,都不过是一个工具,做游戏开发的关键还是解决问题的思路,方法。 我们团队在这一年来,一直在使用Cocos开发各类3D游戏,也积累了很多经验。说心里话,我是希望咱中国自...
Unity 2017.1 feature spotlight: Playable API It’s time. It’s been a long run, but here we are: in Unity 2017.1 the Playable API is out of experimental. The graph-like Playable API allows you to...
Unite 2016 针对移动设备端的Unity应用优化... 今天参加Unite 2016听下来最有收货的一个talk,虽然一半以上都是老生常谈…整理如下 ps. 我个人觉得比较有价值的在于 资源审查 这一部分,关于各类资源的常用方法都提出了一些很有实用价值的建议和规范 如何获得足够好的数据 iOS: Instruments ...
Using Paint 3D to design 3D models for your Hologr... In this article, you will learn, how we can make a basic 3D Model using Paint 3D app and then using it as a Holograms in your holographic application...
This week in Mixed Reality: Issue 9 This week the team is continously working on new features, making improvements and fixing bugs. Next week, the team will be in San Francisco for ...