Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Addressables in Editor Unit Tests

Discussion in 'Addressables' started by james7132, Aug 2, 2018.

  1. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    My project has added a few generated unit tests to validate that the indirect references we had in place, soon to be AssetReferences with Addressables, were pointing to valid assets or scenes. For example, we generated a new test case to make sure each of our characters had a portrait sprite attached to the metadata ScriptableObject. In this we, used the previous Asset Bundle Simulation to simulate loading from an asset bundle, synchronously loaded it, and validated that it the requirements we had set for it (not being null, being of the right size, etc.) This has been a nice safeguard against potentially having game-breaking asset load failures down the line: any run of our test suite added data validation. Is this supported in Addressaables?
     
  2. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    There's nothing inherent in Addressables that would block you from referencing it from your unit tests. If you can track down where the package was downloaded onto your computer, you can see our source code, which includes our (still incomplete) unit tests. You are welcome to use those as examples of how to create unit tests with Addressables.

    Thanks,
    Bill
     
  3. isaac-ashdown

    isaac-ashdown

    Joined:
    Jan 30, 2019
    Posts:
    69
    It seems that the unity Test system doesn't really handle async tests very well, and I'm having problems using addressables in tasks (I have the exact same requirement as @james7132 ). My Completed events never fire, and calls to LoadResourceLocationsAsync return an already completed AsyncOperationHandle with no Results for addresses that work correctly at runtime. Were you able to get this working, James?
     
    creepteks and RecursiveFrog like this.
  4. isaac-ashdown

    isaac-ashdown

    Joined:
    Jan 30, 2019
    Posts:
    69
    I was able to get the Addressables API working correctly for PlayMode tests, which is good enough for me.
     
    unity_bill likes this.
  5. Deleted User

    Deleted User

    Guest

    How did you get Addressables working in PlayMode tests? I can't get past error CS0234: The type or namespace name 'AddressableAssets' does not exist in the namespace 'UnityEngine' (are you missing an assembly reference?)
     
  6. isaac-ashdown

    isaac-ashdown

    Joined:
    Jan 30, 2019
    Posts:
    69
    As it says, you probably are missing an assembly reference. Ensure the assembly definition that contains your tests references Unity.Addressables and Unity.ResourceManager.
     
  7. creepteks

    creepteks

    Joined:
    Jun 2, 2017
    Posts:
    19
    Hi there. I got the same problem except my test assembly has references to Unity.Addressables and unity.ResourceManager.
    testAsmdef.png
    I have two simple tests as following:
    Code (CSharp):
    1.         [Test]
    2.         public void CharacterCardsSystem_loadAddressables_CorrectLabel()
    3.         {
    4.             var handle = Addressables.LoadAssetsAsync<CharacterAbilitiesDefinitionAsset>("characterCards", null);
    5.             handle.Completed += operationHandle =>
    6.             {
    7.                 Assert.AreEqual(handle.Status, AsyncOperationStatus.Succeeded);
    8.                 foreach (var card in operationHandle.Result)
    9.                 {
    10.                     Assert.IsNotNull(card);
    11.                 }
    12.             };
    13.         }
    and
    Code (CSharp):
    1. [Test]
    2.         public void CharacterCardsSystem_loadAddressables_WrongLabel()
    3.         {
    4.             var handle = Addressables.LoadAssetsAsync<CharacterAbilitiesDefinitionAsset>("some wrong identifier", null);
    5.             handle.Completed += operationHandle =>
    6.             {
    7.                 Assert.IsTrue(handle.Status == AsyncOperationStatus.Failed);
    8.             };
    9.         }
    and both tests pass. So far so good. But no matter what I put as the asset label when calling LoadAssetsAsync, both tests still pass, for example if I put a non-existing label inside the first test, it still passes. What I'm doing wrong??

    I figured it might be because of callbacks. So I did a UnityTest with:
    Code (CSharp):
    1.   [UnityTest]
    2.         public IEnumerator CharacterCardsSystem_loadAssetsAsync()
    3.         {
    4.             var handle = Addressables.LoadAssetsAsync<CharacterAbilitiesDefinitionAsset>("characaterCards", null);
    5.             yield return handle;
    6.             Assert.AreEqual(handle.Status, AsyncOperationStatus.Succeeded);
    7.         }
    but no matter from where I run the test (from TestRunner window in Unity or from Rider), the test gets stuck forever, not concluding with neither a fail nor a pass. The following window remains until I close it, and at that point, it fails because "test was canceled by user":
    testAsmdef.png
     
    Last edited: May 9, 2020
    zhuchun and MattLT like this.
  8. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    541
    It would be great if assets in custom UPM packages could be marked as Addressables. The inspector doesn't show the addressables field and so all testing assets need to be placed under the Assets/ folder.
     
    unity_7JndxSaDfDSu5w likes this.