I want to write tests to test a subsystem, but I couldn’t find a way of creating the subsystem in the test scope. CreateNew doesn’t work and I don’t know how to even get the game instance so I can access it. And I also would prefer that I could access the subsystem directly instead of having to access the game instance to get it.
In order to access a subsystem from an automated test, you first have to construct a game instance which will create the subsystem. Use the following code:
This will create a gameinstance and get the subsystem from it. You may set the game instance to be your production game instance, or just use UGameInstance as that will create the subsystems the same way.
Extra note: you may have to call Init() on the new game instance before calling GetSubsystem. At least I had to when I tried this.
UGameInstance* GameInstance = NewObject();
GameInstance->Init();
UMySubsystem* manager = GameInstance->GetSubsystem();
I also found that GameInstance->Shutdown() was needed at the end of the test, else something would go horribly wrong when the game instance was garbage collected (related to deinitialising the subsystems).