如何在C++ Unreal Engine中使用数据资产进行单元测试

huangapple go评论47阅读模式
英文:

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

问题

我正在为我的Unreal Engine的C++插件添加一些单元测试。
目前,我正在使用Automation System来进行测试。
我已经为结构体和一些简单的UObject编写了可工作的单元测试。

但是,我遇到了一个情况,我不太清楚如何正确处理。
我有一个UObject(假设为UEntity),它在运行时实例化,并使用设计师可以在编辑器中创建的UDataAsset(假设为UEntityData)进行设置,以填充一些数据。
UEntityData包含一个数组,而UEntity需要访问它以执行某些操作。

我的问题是:如何在单元测试中提供UEntity所需的UEntityData

我有一些解决方案,但由于在互联网上找不到与此情况相关的信息,我不知道哪种方法最好。

  • 我可能可以在单元测试中使用NewObject<>()创建数据资产,并在提供给UEntity实例之前填充其成员变量。(我不知道在UDataAsset类上使用NewObject<>()是否是正确的行为,而且我在考虑是否超出了单元测试的范围,因为我依赖于其他类的构造函数和访问器)。
  • 我可以在编辑器中创建一些数据资产文件,并在单元测试中加载它们,但我认为这不是一种可靠的方法,因为这些资产位于单元测试环境之外(也许我对这种感觉是错的)。
  • 我可以创建另一种方式来模拟这些数据资产,而不使用UEntityData类,但我觉得这违反了单元测试的目的,因为它并没有真正测试实际行为,而且在数据资产和非数据资产模拟之间复制成员变量和函数很麻烦(也许我在这里也是错的)。

所以我在这里询问是否有人可以指导我如何在单元测试中处理数据资产的最佳方法?

英文:

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&lt;&gt;() 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&lt;&gt;() 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?

答案1

得分: 0

使用诸如NewObjectNewNamedObjectConstructObject等模板化的UObject宏来创建UDataAsset是完全可以的,并且似乎是最佳方法,出于你所提到的原因:确实,最好将所有测试都保持自包含,不依赖于像在编辑器中创建的外部资产。在代码中,将数据资产视为普通的UObject,这意味着它受UPROPERTY支持,可以作为其他对象的Outer,依赖于基于引用的垃圾收集等等。数据资产的所有属性都可以在代码中安全地分配/更改。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月22日 02:35:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定