How to load a map from an Automation test?

I’m writing Automation tests and I want a test that loads a map, creates a few Actors, runs it for a bit, and then inspects the state of the created Actors to make sure they did what I want them to.

My problem is that when I load the map using FLoadGameMapCommand and wait for it to finish loading with FWaitForMapToLoadCommand then the world just stops. World->GetTimeSeconds() returns the same value over and over. If I don’t add a FLoadGameMapCommand then the world time is ticking up as I expect. What do I need to do to restart world ticking?

I have tried to call World->Tick() myself from my Latent Command, FWaitForTicking below, but that caused a crash.

What is the proper way to write an Automation test that loads and runs a map?

My current setup:

struct FState
{
	UWorld* World;
};

UWorld* GetTestWorld()
{
	const TIndirectArray<FWorldContext>& Contexts = GEngine->GetWorldContexts();
	for (const FWorldContext& Context : Contexts)
	{
		if (Context.WorldType == EWorldType::Game && Context.World() != nullptr)
		{
			return Context.World();
		}
	}
	return nullptr;
}

DEFINE_LATENT_AUTOMATION_COMMAND_ONE_PARAMETER(
	FSetWorldPointer, FState&, State);
bool FSetWorldPointer::Update()
{
	State.World = GetTestWorld();
	return true;
}

DEFINE_LATENT_AUTOMATION_COMMAND_ONE_PARAMETER(
	FWaitForTicking, FState&, State);
bool FWaitForTicking::Update()
{
	UE_LOG(LogTemp, Warning,
		TEXT("Time is %f."), State.World->GetTimeSeconds());
	return State.World->GetTimeSeconds() > 10.0f;
}


IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FTickingTest, "AutomationTick.Ticking",
	EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter)
bool FTickingTest::RunTest(const FString&)
{
	static FState State;
	ADD_LATENT_AUTOMATION_COMMAND(
		FLoadGameMapCommand(TEXT("EmptyLevel")))
	ADD_LATENT_AUTOMATION_COMMAND(FWaitForMapToLoadCommand())
	ADD_LATENT_AUTOMATION_COMMAND(FSetWorldPointer(State))
	ADD_LATENT_AUTOMATION_COMMAND(FWaitForTicking(State))
	return true;
}

I run this with
./UE4Editor ~/UnrealProjects/AutomationTick/AutomationTick.uproject -NoSound -Game -NullRHI -NoSplash -Unattended -ExecCmds="Automation RunTests AutomationTick.Ticking" -TestExit="Automation Test Queue Empty"

Hello!

In case you still have that issue, I suggest you try with the method AutomationOpenMap in AutomationCommon. I believe you do not even have to wait for the map to be loaded, the method will take care of that.

In case you wonder why the wait you are performing is blocking the test execution, the answer may be that you are running it from the GameThread. Since the time is always w.r.t. the GameThread, that will cause a hang.