# Mocking for functional and unit tests

**URL:** <https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [December 16, 2014, 6:20pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770 "2014-12-16T18:20:01Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hexerei](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Hexerei](https://forums.unrealengine.com/u/Hexerei)\
**Post date:** [December 16, 2014, 6:20pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/1 "2014-12-16T18:20:01Z")

</div>

I’ve looked over the automation guide pages, but I don’t see anything about mocking, stubbing, spying, etc. Are these supported when adding tests? Are there any project examples that include tests with mocking?

My testing experience comes from Node.JS and PHP. Is there something else (not part of the UE 4 code) I should use for mocking in C++ that will work with the UE automation? What about when testing blueprints?

---

<div class="post-metadata">

**Author:** ![Davixe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Davixe](https://forums.unrealengine.com/u/Davixe)\
**Post date:** [February 17, 2015, 10:49am UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/2 "2015-02-17T10:49:02Z")

</div>

I have the same question! any updates?

---

<div class="post-metadata">

**Author:** ![xhallix](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@xhallix](https://forums.unrealengine.com/u/xhallix)\
**Post date:** [March 9, 2016, 10:36pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/3 "2016-03-09T22:36:18Z")

</div>

_bump_ - same here

---

<div class="post-metadata">

**Author:** ![jimmyt1988](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jimmyt1988](https://forums.unrealengine.com/u/jimmyt1988)\
**Post date:** [August 21, 2016, 5:22pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/4 "2016-08-21T17:22:48Z")

</div>

_bump_ - same here. I’ve been looking at the Unreal Tournment tests and cannot find anything obvious to denote if there is a built in mocking framework. It seems there isn’t. I’m going down the route of trying out: [GitHub - eranpeer/FakeIt: C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.](https://github.com/eranpeer/FakeIt) - I will write instructions on how to implement it if it’s possible.

---

<div class="post-metadata">

**Author:** ![morgoth32169](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@morgoth32169](https://forums.unrealengine.com/u/morgoth32169)\
**Post date:** [November 16, 2021, 2:52am UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/5 "2021-11-16T02:52:05Z")

</div>

Would be nice to have an official answer on this

---

<div class="post-metadata">

**Author:** ![LordBongoCat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/lordbongocat/32/695516_2.png) [@LordBongoCat](https://forums.unrealengine.com/u/LordBongoCat)\
**Post date:** [August 10, 2022, 4:39pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/6 "2022-08-10T16:39:27Z")

</div>

There is no official framework at this time. We are actively making improvements to our automated testing tools and while mocking has come up in discussions it hasn’t been put on the roadmap at this time.

---

<div class="post-metadata">

**Author:** ![NorthWeapon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NorthWeapon](https://forums.unrealengine.com/u/NorthWeapon)\
**Post date:** [August 17, 2022, 5:35pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/7 "2022-08-17T17:35:37Z")

</div>

Really glad a developer has replied to this.

But **please please please** add more support for unit testing!!! Mocking especially, and examples with how to unit test basic behavior like testing if a character moves forward, jumps, etc.

Thank you!

---

<div class="post-metadata">

**Author:** ![LordBongoCat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/lordbongocat/32/695516_2.png) [@LordBongoCat](https://forums.unrealengine.com/u/LordBongoCat)\
**Post date:** [August 17, 2022, 7:02pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/8 "2022-08-17T19:02:07Z")

</div>

We’re definitely aware there’s tons of room for improvement in our automation and testing tools. It will take some time, but improvements are coming.

---

<div class="post-metadata">

**Author:** ![dangoes2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dangoes2](https://forums.unrealengine.com/u/dangoes2)\
**Post date:** [November 21, 2023, 5:53pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/9 "2023-11-21T17:53:13Z")

</div>

Hi @LordBongoCat, bumping into this as I’m digging around for a good solution to speed up writing mocks for my unit tests in Unreal. Our project is fairly unit-test driven, with hundreds of them right now, and it’s getting to be a bear to write and maintain all of these mocks when there are great mock generation tools. Any update or word on more tools coming our way for mock creation?

---

<div class="post-metadata">

**Author:** ![Ramon\_Janousch](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ramon_janousch/32/468566_2.png) [@Ramon\_Janousch](https://forums.unrealengine.com/u/Ramon_Janousch)\
**Post date:** [November 29, 2023, 9:42am UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/10 "2023-11-29T09:42:09Z")

</div>

Hi @dangoes2 ,  
We are currently working on a new framework for automated testing and will release this plugin on Github in a few weeks.  
It will also support unit tests for blueprints.  
Would you be interested in testing the plugin?

Could you share more details about your current workflow, specifically how you organize, write and maintain your mocks? The insights from your experience would be of great benefit to the development of our framework and could lead to more efficient testing procedures for Unreal Engine projects. In addition, any tools or methods you have found to be effective would be valuable to know.

I look forward to your response and the possibility of a mutually beneficial exchange of ideas.

---

<div class="post-metadata">

**Author:** ![Floating-Island](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/floating-island/32/903101_2.png) [@Floating-Island](https://forums.unrealengine.com/u/Floating-Island)\
**Post date:** [February 26, 2024, 7:42pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/11 "2024-02-26T19:42:49Z")

</div>

@Ramon_Janousch  
Do you have any update on the plugin?

I’m very interested in TDD in Unreal Engine and in the past I wrote some articles on using unreal tests in a project taking into account ticks, PIE sessions, editor clicks, networking, etc.

Maybe some of the utilities mentioned there are useful for you and your team to use so I’ll leave them here 😉

[Jenkins, CI and Test-Driven Development](https://unrealcommunity.wiki/jenkins-ci-amp-test-driven-development-6912tx0c) → this one is about having a simple CI solution using Jenkins. Also with a [repo](https://github.com/Floating-Island/UE4-TDD-CI_Testing)

And then these talking about things to take into account when testing:

[Considerations On Testing UE4 Classes Part I: Worlds, Ticks and Forces](https://unrealcommunity.wiki/considerations-on-testing-ue4-classes-y45ygvk0)

[Considerations On Testing UE4 Classes Part II: Editor clicks and Gamepad/Keyboard button presses](https://unrealcommunity.wiki/considerations-on-testing-ue4-classes:-part-ii-8b09e4)

[Considerations On Testing UE4 Classes Part III: Replication](https://unrealcommunity.wiki/considerations-on-testing-ue4-classes:-part-iii-replication-2d68d4)

This is a repo with a small game using the things mentioned in the articles above: [ProjectR](https://github.com/Floating-Island/ProjectR)

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [February 26, 2024, 7:51pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/12 "2024-02-26T19:51:52Z")

</div>

> [@dangoes2](#):
>
> it’s getting to be a bear to write and maintain all of these mocks

I have a slightly different opinion on what to mock:  
You should mock things that are non-deterministic, and nothing else.

You need a mock clock. You need a mock file system (probably, although with proper test isolation maybe not.) You need a mock network. You need a mock database. You may need a mock graphics device, and you almost certainly need a mock input device.

However, you don’t need a mock for the service that runs on the other side of the network. Just run the actual code of that service, and just plumb it through using the mock network. It’s all in process, it’s all deterministic, it’s all testable and debuggable.

You don’t need a mock texture streaming manager – run the real thing, just have it talking to the mock file system and the mock graphics device (and maybe mock clock, for time based purging.)

You then write simple functional tests for each of the mocked subsystems, run the same tests on both the mock and the real device to verify they behave the same, and then all the rest of your code tests against the small set of mocks. Because RAM and CPUs are deterministic, you should run your tests with _as much code as possible_ in the loop.

Some people call this “integration testing,” but I feel that it’s not, in the sense that it’s deterministic and debuggable and fast – anything that sits in CPU and RAM should not take a meaningful amount of time in a test.

People who come from dynamically-patchable languages tend to not have this approach, and there’s often a fair bit of friction and discussion to get on the same page, but in practice, you get much better test coverage (and thus, much earlier detection when expectations get violated) when you keep the mocks to a bare minimum.

---

<div class="post-metadata">

**Author:** ![FlakyLlama85](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FlakyLlama85](https://forums.unrealengine.com/u/FlakyLlama85)\
**Post date:** [April 24, 2024, 1:08pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/13 "2024-04-24T13:08:42Z")

</div>

We could certainly debate what should/shouldn’t be mock in tests, but at the end of the day there really isn’t any debate that at least _some_ things should be mocked.

I think it would be uncontroversial to suggest that https requests need to be mocked. Yet I don’t find any obvious way to do this using FHttpModule.

I’d really love help figuring out how to test objects which make https requests without requiring the tests to actually make, and depend on, actual https requests.  
Thanks!

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [April 24, 2024, 2:39pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/14 "2024-04-24T14:39:52Z")

</div>

> [@jwatte](#):
>
> you don’t need a mock for the service that runs on the other side of the network. Just run the actual code of that service, and just plumb it through using the mock network

Yeah, the closest you can come right now is to spin up a separate HTTP server that binds to a local port and provide that address as the host for the URLs. There are small embeddable C++ HTTP servers you could use for this, but it’s a bit cruftier than a clean “this is what a HTTP request looks like” interface at the code level.

---

<div class="post-metadata">

**Author:** ![dangoes2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dangoes2](https://forums.unrealengine.com/u/dangoes2)\
**Post date:** [May 3, 2024, 8:29pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/15 "2024-05-03T20:29:13Z")

</div>

I think there’s value in this approach, but this is just one strategy for testing, and my experience guides me quite differently. I started to dive in, but I don’t reeaalllly want to write up a justification for why defining the granularity of the unit-under-test at the class level for deterministic behavior is so useful. Basically, I don’t agree, and I still really, really want a mocking framework 😄

---

<div class="post-metadata">

**Author:** ![jimmyt1988](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jimmyt1988](https://forums.unrealengine.com/u/jimmyt1988)\
**Post date:** [June 17, 2024, 5:48pm UTC](https://forums.unrealengine.com/t/mocking-for-functional-and-unit-tests/301770/16 "2024-06-17T17:48:48Z")

</div>

> **[GitHub - jimmyt1988/UE-Testing: A test UE project set up for working out testing](https://github.com/jimmyt1988/UE-Testing)**
>
> A test UE project set up for working out testing

You can use Inversion of Control.

Unreal Engine actually has documentation of this (mentioned in the TTypeContainer write up) under a cpp file. [It can be found here](https://github.com/EpicGames/UnrealEngine/blob/7d9919ac7bfd80b7483012eab342cb427d60e8c9/Engine/Source/Runtime/Core/Private/Tests/Misc/TypeContainerTest.cpp).

Reach out if you need any help.

> [@Where do i declare my IOC Container?](https://forums.unrealengine.com/t/where-do-i-declare-my-ioc-container/405121/2):
>
> For people still coming across this - I have prepared a github example of IOC in Unreal Engine - [https://github.com/jimmyt1988/UE-Testing](https://github.com/jimmyt1988/UE-Testing)

Please note that the guide is out of date in terms of it not using `describe` and `it`. Here’s an example of how to do that:

> **[Automated Testing With Specs in Unreal Engine](https://minifloppy.it/posts/2024/automated-testing-specs-ue5/)**
>
> An introduction to spec tests, my favorite way of writing unit, integration and functional automated tests in Unreal.

And also please note that you may also need your own fixture class to add Actors etc into a virtual world and environment for the unit test:

WorldFixture.h

```auto
#pragma once

#include "CoreMinimal.h"
#include "EngineUtils.h"

/**
 * https://minifloppy.it/posts/2024/automated-testing-specs-ue5/
 */
class LOYLY_API FWorldFixture
{
public:

    explicit FWorldFixture(const FURL& URL = FURL())
    {
        if (GEngine != nullptr)
        {
            static uint32 WorldCounter = 0;
            const FString WorldName = FString::Printf(TEXT("WorldFixture_%d"), WorldCounter++);

            if (UWorld* World = UWorld::CreateWorld(EWorldType::Game, false, *WorldName, GetTransientPackage()))
            {
                FWorldContext& WorldContext = GEngine->CreateNewWorldContext(EWorldType::Game);
                WorldContext.SetCurrentWorld(World);

                World->InitializeActorsForPlay(URL);
                if (IsValid(World->GetWorldSettings()))
                {
                    // Need to do this manually since world doesn't have a game mode
                    World->GetWorldSettings()->NotifyBeginPlay();
                    World->GetWorldSettings()->NotifyMatchStarted();
                }
                World->BeginPlay();

                WeakWorld = MakeWeakObjectPtr(World);
            }
        }
    }

    UWorld* GetWorld() const { return WeakWorld.Get(); }

    ~FWorldFixture()
    {
        UWorld* World = WeakWorld.Get();
        if (World != nullptr && GEngine != nullptr)
        {
            World->BeginTearingDown();

            // Make sure to cleanup all actors immediately
            // DestroyWorld doesn't do this and instead waits for GC to clear everything up
            for (auto It = TActorIterator<AActor>(World); It; ++It)
            {
                It->Destroy();
            }

            GEngine->DestroyWorldContext(World);
            World->DestroyWorld(false);
        }
    }

private:

    TWeakObjectPtr<UWorld> WeakWorld;
};

```

Example usage:

```auto
#include "CoreMinimal.h"
#include "Actors/BeerActor.h"
#include "Characters/MainCharacter.h"
#include "Controllers/MainPlayerController.h"
#include "PlayerStates/MainPlayerState.h"
#include "Misc/AutomationTest.h"
#include "Tests/Fixtures/WorldFixture.h"

BEGIN_DEFINE_SPEC(BeerActorDrinkBeerSpec, "Loyly.Actors.BeerActor.DrinkBeerSpec", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter)

TUniquePtr<FWorldFixture> WorldFixture;
ABeerActor* Actor;
AMainPlayerController* Controller;
AMainCharacter* Character;
AMainPlayerState* PlayerState;

END_DEFINE_SPEC(BeerActorDrinkBeerSpec)

void BeerActorDrinkBeerSpec::Define()
{
	Describe("DrinkBeer", [this]()
	{
		BeforeEach([this]()
		{
			WorldFixture = MakeUnique<FWorldFixture>();

			Actor = WorldFixture->GetWorld()->SpawnActor<ABeerActor>();
			Controller = WorldFixture->GetWorld()->SpawnActor<AMainPlayerController>();
			Character = WorldFixture->GetWorld()->SpawnActor<AMainCharacter>();
			PlayerState = NewObject<AMainPlayerState>();
			
			WorldFixture->GetWorld()->AddController(Controller);
			Character->SetPlayerState(PlayerState);
			Controller->Possess(Character);
		});

		It("should decrease the character temperature", [this]()
		{
			// Arrange
			Actor->DecreaseTemperatureBy = 0.2;
			PlayerState->Temperature = 1;

			// Act
			Actor->DrinkBeer();

			// Assert
			TestEqual("Temperature value decreased as expected", PlayerState->Temperature, 0.8f);
		});
	});
}

```

Also, you can test code outside of unreal engine being run which is handy if your computer sucks like mine.

# Running via command line:

`"C:\Program Files\Epic Games\UE_5.2\Engine\Binaries\Win64\UnrealEditor-Cmd.exe" "C:\Users\james\Unreal Projects\Loyly\Loyly.uproject" -execcmds="Automation RunTests Loyly;Quit" -stdout -unattended -NOSPLASH -NullRHI`

- `execcmds="Automation RunTests Loyly;Quit"`: This tells the Unreal Engine command-line editor to run automation tests that match the “Loyly” filter, and then quit the editor once the tests have been completed. It does not require an additional specification for EAutomationTestFlags::EditorContext because the test itself defines the context (and other flags) necessary for execution.
- `stdout`: Forces the engine to output logs to the standard output, which can be very useful for CI systems to capture and analyze the output directly.
- `unattended`: Runs the engine in a mode that does not require user interaction, suitable for automated tasks like continuous integration.
- `NOSPLASH`: Disables the splash screen on startup, which is generally preferred for automated tasks to reduce overhead and potential graphical issues.
- `NullRHI`: This is a crucial flag for running tests in environments without a dedicated GPU or where you do not want to initialize the full rendering hardware interface. It makes the tests run with a null renderer, which can prevent rendering-related operations but allows most other engine functionality to be tested.
