Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using Resources.LoadAll() to populate [ValueSource] with prefabs

Discussion in 'Testing & Automation' started by BackgroundMover, Dec 28, 2020.

  1. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    216
    tl;dr: is this supposed to work? I'm generating tests for individual prefabs found in Resources.LoadAll()

    So an example given for ValueSource shows it pointing to a static int array. I'm guessing at compile time, this array gets traversed, and code is generated behind the scenes, with a unique [UnityTest] instance for each element in the array.

    But, instead of something pre-defined and static, I wanted to populate my tests with the dynamic results of Resources.LoadAll(). So, for each prefab in a specific resources path, I wanted a test to be created, receiving that prefab so I could instantiate it and have tests run upon it. So instead of a hand written collection of string paths to prefabs in Resources, it uses the (dynamic) results of Resources.LoadAll() with specifiers I give it like subfolders and component types.

    The only downside is that tests are only generated when the code is recompiled, not when new prefabs are added (though hooking into the prefab importer pipeline might be able to trigger something?)

    I guess my questions are:
    1- is this correct or a fluke?
    2- Am I creating references to the prefab assets themselves (maybe risking corruption?) or is Unity doing some magic behind the scenes to store these as paths, but loading them up when the tests are run? Because I'm getting actual objects being passed to my tests, not just string resource paths I have to load and then instantiate myself.
    3- Should I be using Addressables for this instead? Can I just set some criteria for which addressables to load, and tests will show up in the test runner?
     

    Attached Files:

    • code.jpg
      code.jpg
      File size:
      66.2 KB
      Views:
      298
    JesseSTG likes this.
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    No, tests are not generated at compile time; they are generated in-memory only when the list of tests is first needed (e.g. to render the test runner window) and then cached until a domain reload, after which they will be generated again as needed. So, yes, dynamic provisioning of test instances is a fully supported scenario.

    However, you should bear in mind that this means that your ValueSource code gets fully executed every time the test list is generated - i.e. opening the Test Runner window is going to cause all your prefabs to be loaded into memory, which may not be the best experience. You may be better off just querying the AssetDatabase to get the names/paths of your prefabs, which you can do without actually loading them into memory; then you'd load each prefab inside the test itself, meaning that it wouldn't happen until the test is actually running.
     
    JesseSTG and BackgroundMover like this.
  3. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    216
    Thank you for the guidance- its very cool that the test runner supports this. Now my ValueSource is being populated by "querying" the AssetDatabase, and my tests are receiving individual asset paths to load and instantiate. Pretty cool to see tests show up programmatically in the inspector based on authored content :)
     
    superpig and JesseSTG like this.