Search code examples
unreal-engine4unreal-engine5

How to unit test with data assets C++ Unreal Engine


I am adding some unit tests to my C++ plugin for Unreal Engine. I am currently using the Automation System for that. I already have working unit tests for the structs and some simple UObjects.

However I am stumbling on a case I don't really know how to handle correctly. I have an UObject (say UEntity) which is instantiated at runtime and is setup with a UDataAsset (say UEntityData) that the designers can create in the editor to fill some data inside. The UEntityData contains an array and the UEntity access it for some behavior.

My problem here is: How can I provide the UEntityData to the UEntity in the unit test?

I have some solutions in mind but since I can't find anything on the Internet relating this case I don't know the best approach.

  • I could maybe create the data assets with NewObject<>() inside the unit test and filling its members before providing them to the UEntity instances. (I don't know if it's correct behaviour to use NewObject<>() with UDataAsset classes, and also I am wondering if it's going beyond the unit testing since I'm relying on some other class constructor and accessors).
  • I could create some data asset files in the editor and loading them in the unit tests, however I think it's not a reliable way because the assets are outside the unit tests environment (maybe I'm wrong with this feeling).
  • I could create another way to mock up those data assets without using the UEntityData class but I'm feeling that this is breaking the purpose of the unit test since it is not really testing the real behavior and cumbersome to duplicate the members and functions between the data assets and the non-data asset mocks. (maybe I'm wrong also here)

So I'm asking here if anybody can lead me to the best way to deal with data assets in unit tests?


Solution

  • Creatinig UDataAssets using templated UObject macros such as NewObject, NewNamedObject, ConstructObject is totally fine, and seems like the best approach, for reasons you have noted: indeed, it is best to keep all tests self-contained and not rely on external assets such as ones created in Editor. It also good practice to use the exact classes you will use for the real-world use case.

    Data Asset can be treated as plain UObject in code, meaning it is supported by UPROPERTY, can have/be Outer for other objects, rely on reference-based garbage collection, etc. All properties of the Data Asset can safely be assigned/changed in code.