Unity3D & Jenkins part 3 – Unit Testing

Jenkins can do more than just doing build configuration, it can also help you catching errors. Well, that’s not entirely true, you will catch the errors Jenkins will just run your test.

So first thing you need is a test suite or a test package. My choice fell on NUnit or rather NUnitLite. NUnitLite is a Unit test framework designed for embedding in devices and is a lighter compatible version of NUnit. Furthermore Nunit plugs nicely into Jenkins Using xUnit plugin.

 NUniteLight in Unity3D

For this to work you need todo one of two things download the 1.0 version yourself here or get my modified version here. I suggest the latter, just download the project and extract my modified version. I’ll do a separate github at some point.

Modifying build script

NUnitLite has excellent commandline options, so what I did was to piggyback the command options together with the command options from Unity. I need to wrap and extract then NUitLite arguments since it will fail if the Unity3D arguments are passed along.

		[MenuItem ("Custom/CI/Do Unit Test")]
		static void DoNunitTest(){
			  //load command line arguments	
			  var arguments = System.Environment.GetCommandLineArgs();
				//Default commands in editor mode
				if(Application.isEditor){
					arguments = new string[]{"nunitargs","\"-out:nunit_out.txt -result:historyline-test-res.xml -format:nunit2 Assembly-CSharp\""};
				}
		
				var nunitArgs =new string[]{ "Assembly-CSharp","/out:nunit_out.txt","/result:historyline-test-res.xml"};
			   //find the nunit parameter, and extract
				for(int i=0;i<arguments.Length;i++){
					if(arguments[i].Contains("nunitargs")){
						nunitArgs = arguments[i+1].Replace("\"","").Split(' ');
						Debug.Log("Nunit args Application.dataPathfound: " + String.Join(",", nunitArgs));
					}
				}		
			var runner = new TextUI();
            runner.Execute(nunitArgs);			
		}





Setting up in Jenkins

If you have read the previous posts you already have the Unity3D plugin, now you need to download the XUnit plugin.

You will need the following command for the Unity3D plugin (Before you do a build).

-quit -batchmode -executeMethod BuildScript.DoNunitTest nunitargs "-out:nunit_out.txt -result:historyline-test-res.xml -format:nunit2 Assembly-CSharp"

  • Out – writes debug info to an out file
  • result – your test report
  • format -format of your xml options are nunit2 and nunit3
  • Assembly-CSharp – name of the assembly to look for test files. This is where Unity3D puts your gamecode
  • nunitargs – arguments wrapper for nunit arguments

Below you can see the settings

Plugin config - Unity3D

Plugin config – Unity3D

Settings up the report tool

This part is easy, just write in the name of the results file you specified earlier.

 

Postbuild - rapport

Postbuild – rapport

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s