Search Unity

ARKit unit testing?

Discussion in 'ARKit' started by joshhw, Jan 4, 2018.

  1. joshhw

    joshhw

    Joined:
    Sep 19, 2017
    Posts:
    2
    Hey, has anyone figured out a way to do Unit testing with ARKit applications? I'm new to Unity & Unit Testing in Unity as well. I'd love to read about others approaches to testing that doesn't include building to XCode and manually testing it with a device.
     
  2. mickee

    mickee

    Joined:
    Dec 26, 2013
    Posts:
    7
    Hey ! You can use the NUnit framework to unit test your logic.

    The hard part in AR Unit Testing is that often you can't mock AR objects that you have during runtime : ARSCNView, Hit testing ... Therefore, you should extract the data from those objects and inject them in your application logic and unit test it.

    You should separate responsibilities in your AR application (e.g. by using Clean Architecture) :
    - the AR part (Controller) is the only one that handles the AR related objects/frameworks.
    - the pure logic part (Interactor or Business Logic) handles AR agnostic objects and deduce responses.
    - the display part displays some nodes given the Interactor's response.

    Examples:
    - hit test in your controller, then test your logic given ARHitTestResults
    - to UT if a given world space node is visible on camera, send (targetNode, cameraNode) to your interactor then evaluate if returned bool is correct given their space positions (again AR agnostic).

    You can read more about Clean Unity architecture applied to Unity on my blog : www.clean-unity.com.
    I applied unit testing to my logic in native iOS apps and it works well as long as you separate concerns.

    Code (CSharp):
    1. using UnityEngine;
    2. using NUnit.Framework;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5.  
    6. public class SomeARInteractorTests
    7. {
    8.     public SomeARInteractor sut;
    9.     public SomeARPresenterSpy spy;
    10.  
    11.     [SetUp]
    12.     public void setup()
    13.     {
    14.         sut = new SomeARInteractor();
    15.         spy = new SomeARPresenterSpy();
    16.         sut.presenter = spy;
    17.     }
    18.  
    19.     [TearDown]
    20.     public void teardown()
    21.     {
    22.     }
    23.  
    24.     // MARK: - Test Doubles
    25.  
    26.     public class SomeARPresenterSpy : ISomeARPresenter
    27.     {
    28.         public bool presentSomeUseCaseCalled = false;
    29.         public SomeAR.SomeUseCaseResponse lastSomeUseCaseResponse;
    30.  
    31.         void presentSomeUseCase(SomeAR.SomeUseCaseResponse response)
    32.         {
    33.             presentSomeUseCaseCalled = true;
    34.             lastSomeUseCaseResponse = response;
    35.         }
    36.     }
    37.  
    38.     // MARK: - SomeUseCase
    39.     [Test]
    40.     public void test_givenDefaultParameters_whenNormalDataSetUsed_thenSomeConditionIsMet()
    41.     {
    42.         // Build request
    43.         Assert.AreEquals(spy. lastSomeUseCaseResponse.someComputedValue, 0);
    44.     }
    45.  
    46. }