Ben Pyton
Ben Pyton

Reputation: 348

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.

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

Upvotes: 0

Views: 574

Answers (1)

goose_lake
goose_lake

Reputation: 1480

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.

Upvotes: 0

Related Questions