Instantiate prefab in Play Mode tests

I’ve just started with Unity Test Framework and I would like to run some tests on my AI enemy character. There is a prefab that I would like to instantiate a gameobject from, then use that in various tests that require play mode (checking the AI sees other game objects and that sort of thing).

This snippet below doesn’t work because AssetDatabase can only be used in the editor. How else could I approach this?

public class AnnaTests
{
    private GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/SurvivalAnnaNew.prefab");

    [UnityTest]
    public IEnumerator Spawns()
    {
        var prefabInstance = Object.Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
        // Assert some stuff...
        yield return null;
    }
}

While running in the editor, even play mode tests have access to the Unity editor APIs.
Is the code you posted above failing to compile ? what error are you getting ?

If you’re looking to run tests in the player, then for loading resources you have to use the same techniques as you would use in the player usually: loading a scene, loading from an AssetBundle, or loading from the Resources folder.

@liortal Ah, right, I see. I added using UnityEditor; and it allowed me to run the play mode tests.
@superpig Would you advise against using UnityEditor and AssetDatabase? I guess I could make a scene with the game object already inside and load that

It depends on whether you want to be able to run your tests on-device or not. If you just want to run the playmode tests in-Editor, then using UnityEditor and AssetDatabase is perfectly fine.

1 Like

I wasn’t aware that the tests can be run on-device. How does that work @superpig ?

See Workflow: How to run a Play Mode test in player | Test Framework | 1.1.33

A build of your game is made which includes the test execution engine, and can then be deployed to device and run there, with the results fed back to the Editor (using the same connection as is used for e.g. profiling).

Thanks for your help Superpig! What a guy!

I came to this thread (and maybe others will as well) in trying to answer the question: How can I load prefab variants/scenes/other assets to my tests in a way which will NOT gum up my main (non-test) build by loading test-specific assets?

E.g. to load a scene, I’d have to put it in my build, which will be unnecessary content I don’t actually want in my game, right?

1 Like

Ordinarily that’s correct, but you can inject the scene only for test builds, using the TestPlayerBuildModifier attribute. If you take a look at the first example on that page, you’ll see it is customizing the ‘options’ member of the BuildPlayerOptions object, but there is also a scenes property that you can use to customize the list of scenes to include. Not only can you add test-only scenes this way, you can also remove scenes which you don’t need for testing, speeding up the build process for your test players.

3 Likes