# How do I manage UObjects during unit testing?

**URL:** <https://forums.unrealengine.com/t/how-do-i-manage-uobjects-during-unit-testing/558906>\
**Category:** C++\
**Tags:** Automation-Spec, unreal-engine, UE4-26, question, CPP\
**Created:** [May 17, 2022, 2:38am UTC](https://forums.unrealengine.com/t/how-do-i-manage-uobjects-during-unit-testing/558906 "2022-05-17T02:38:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_2a077b82](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2a077b82](https://forums.unrealengine.com/u/anonymous_user_2a077b82)\
**Post date:** [May 17, 2022, 2:38am UTC](https://forums.unrealengine.com/t/how-do-i-manage-uobjects-during-unit-testing/558906/1 "2022-05-17T02:38:57Z")

</div>

I’m trying to write unit tests for the first time, using “Automation Spec” ([Automation Spec | Unreal Engine 4.27 Documentation](https://docs.unrealengine.com/4.27/en-US/TestingAndOptimization/Automation/AutomationSpec/)).

The code that I want to test defines several UCLASS types inheriting from UObject, but none of the code requires a scene or anything weird to execute. It’s just manipulating array data. The main reason it’s a UCLASS is so that it’s accessible to Blueprints.

However, during unit tests, there’s nothing I can attach these UObjects to, which means they could be destroyed by the GC while the tests are still running. So, how do I create and manage transient UObjects during unit tests?

---

<div class="post-metadata">

**Author:** ![PEYMAAN](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PEYMAAN](https://forums.unrealengine.com/u/PEYMAAN)\
**Post date:** [May 17, 2022, 9:42am UTC](https://forums.unrealengine.com/t/how-do-i-manage-uobjects-during-unit-testing/558906/2 "2022-05-17T09:42:32Z")

</div>

I want to know this too.  
What I have been doing (which I don’t know if it’s correct or not) is creating a new map in BeforeEach and destoying it in AfterEach:

```auto
UWorld* World = FAutomationEditorCommonUtils::CreateNewMap();

```

Then you can create actors, objects, etc.

But I don’t know if this approach has any limitations or whatnot.

---

<div class="post-metadata">

**Author:** ![UnrealEverything](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@UnrealEverything](https://forums.unrealengine.com/u/UnrealEverything)\
**Post date:** [May 17, 2022, 3:39pm UTC](https://forums.unrealengine.com/t/how-do-i-manage-uobjects-during-unit-testing/558906/3 "2022-05-17T15:39:30Z")

</div>

You should be able to add the UObjects as root objects so they won’t be garbage collected. Just make sure you have some kind of RAII structure in place that removes the objects from the root set again at the end of the test.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2a077b82](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2a077b82](https://forums.unrealengine.com/u/anonymous_user_2a077b82)\
**Post date:** [May 17, 2022, 7:24pm UTC](https://forums.unrealengine.com/t/how-do-i-manage-uobjects-during-unit-testing/558906/4 "2022-05-17T19:24:43Z")

</div>

My friend introduced me to TStrongObjectPtr, which appears to be a shared pointer for UObjects. This might provide what we want.

> **[TStrongObjectPtr](https://docs.unrealengine.com/4.26/en-US/API/Runtime/CoreUObject/UObject/TStrongObjectPtr/)**
>
> Specific implementation of \[FGCObject\](API\\Runtime\\CoreUObject\\UObject\\FGCObject) that prevents a single UObject-based pointer from being GC'd while this guard is in scope.

_UnrealEverything_ also mentioned that we can simply mark the UObjects as “root” objects when creating them, so that the GC doesn’t try to collect them. However, this also means we have to remember to clean them up afterwards.

---

<div class="post-metadata">

**Author:** ![xi57](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xi57/32/1194660_2.png) [@xi57](https://forums.unrealengine.com/u/xi57)\
**Post date:** [May 17, 2022, 7:30pm UTC](https://forums.unrealengine.com/t/how-do-i-manage-uobjects-during-unit-testing/558906/5 "2022-05-17T19:30:33Z")

</div>

FWIW I don’t think there is anything wrong with creating new maps for each test, particularly if your tests might interact with the world. Doing so ensures that each test has a consistent world state every time it runs.

The `TStrongObjectPtr` is a great tip, I’ll keep it in mind for the cases where a shared world isn’t risking inconsistent test results. Thanks!

---

<div class="post-metadata">

**Author:** ![PEYMAAN](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PEYMAAN](https://forums.unrealengine.com/u/PEYMAAN)\
**Post date:** [May 17, 2022, 7:39pm UTC](https://forums.unrealengine.com/t/how-do-i-manage-uobjects-during-unit-testing/558906/6 "2022-05-17T19:39:22Z")

</div>

To add to my previous answer, I think `FAutomationEditorCommonUtils` only works in editor.

Using `UWorld::CreateWorld(EWorldType::Game, false);` might be a better option to create a temporary world, spawn some things into it and destroying it using `World->DestroyWorld(false);`

---

<div class="post-metadata">

**Author:** ![edugomez102](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@edugomez102](https://forums.unrealengine.com/u/edugomez102)\
**Post date:** [March 27, 2023, 3:10pm UTC](https://forums.unrealengine.com/t/how-do-i-manage-uobjects-during-unit-testing/558906/7 "2023-03-27T15:10:28Z")

</div>

Should it be better to use `EWorldType::Inactive` as it is not being loaded to the editor?

`EngineTypes.h`:

```cpp
/** An editor world that was loaded but not currently being edited in the level editor */
Inactive

```
