Wednesday, December 23, 2015

Unit Testing FSharp for Unity3D

I'm going to try using FSharp in Unity, and I want to unit test my code before I drop my compiled dll into Unity's Asset folder. Testing is one thing that MonoDevelop is pretty good at. After installing mono-develop, I installed NUnit:

sudo apt-get install monodevelop-nunit.

You can also install it from MonoDevelop via the menu: Tools->Add-in Manager.

So in the project view, I right click on my Solution Items, and select Add->New Project...
I don't see any templates for FSharp Unit Testing, so I'll do this from scratch. I create a FSharp Library Project, and name it Test. For Unity compatability, we need to change the taget, so select Project -> Test Options -> Build -> General and change the target framework to Mono/.NET 3.5.

Next, I install NUnit. Right click on the new project, Test, and select Add Nuget Packages. I'll look for and install NUnit. And here I have a problem. NuGet won't install the correct version for my target (3.5), but it will tell me that it installed the wrong one... and that's why I don't use NuGet.

So instead, I visit, and grab the Previous Release: NUnit 2.6.4. I unzip it to my ~/Applications folder, and then reference the dll in my project: In the project view, right click on References and select Edit References... From here I select the .Net Assemblies tab, and browse for ~/Applications/NUnit-2.6.4/bin/nunit.framework.dll \

Now I'm ready to write tests, so I create a simple class:

open System    
open NUnit.Framework
open Entitas

type Tests() =

    let world = new World(64)

    [<Test>]//1st entity - no components
    member this.Test001() =

        let e1 = world.CreateEntity("Playerz")
        Assert.AreEqual(1, e1.Id)

Save my work, right click on my Test project, and select Run Item to run my tests. I can also reference Unity data types by referencing the UnityEngine.dll:

Right click on References and select Edit References... From here I select the .Net Asseblies tab, and browse (on my system) for /opt/Unity/Editor/Data/Managed/UnityEngine.dll

Now, I add 

open UnityEngine

    [<Test>]//2nd test - one conponent
    member this.Test002() =

        let e2 = world.CreateEntity("Playery")
        e2.AddComponent(1, new Vector3(1,0f, 1.0f, 1.0f))
When I'm satisfied that my basic functionality is working, I move the dll into my Unity projects Asset folder. I do this by selecting my game project (not the testing project) in the project view, go to Project -> Project Options. Select the Build->Output and browse to my Unity project Asset folder. I create a new folder - Assets/Libraries, and select it. Now when I build my game project, the dll is generated directly into my Unity project. I can switch to the Unity Editor, and click on Play to see my code at work.

1 comment:

  1. My brother intrested read IT realted blog. I already shared your content link because i not intrested this type blog because I am MBA qualified and i am intested in Mba Assignment help and I have more than seven years experience in content writing and also i have experienced content writer available. Avail this offer in 2024.