2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unreal C++でRubyのRSpecっぽくテストを書く

Posted at

よくあるUnreal C++でのテスト

Unreal Engineにもテスト機構があります。
http://historia.co.jp/archives/698/

テスト結果や実行範囲をリッチなGUIで選択できるのは素晴らしいのですが下記のような不満がありました。

  • テスト名には当然説明的な名前を採用したいがクラス名では難しい。特に冠詞とか
  • 実行順序が不定かつ前処理、後処理がないのでグローバルを使ってでさえ同じ記述を何度もすることになる
  • テストの最小単位を書くのが面倒なので1つのテストに何個ものテストを書いてしまう

RSpecっぽいテスト

そんな不満の解決策を探していた所、下記のような実装を見つけました。

github.com/EpicGames/UnrealEngine/blob/4.19/Engine/Plugins/Tests/RuntimeTests/Source/RuntimeTests/Private/AutomationSpec.spec.cpp
// BEGINとENDの間の変数がDescribe中保持される
BEGIN_DEFINE_SPEC(AutomationSpec, "System.Automation.Spec", /* 省略 */)
	bool Foo;
	FString RunOrder; 
END_DEFINE_SPEC(AutomationSpec)

void AutomationSpec::Define()
{
	// 1つのテスト定義中にテストを複数書けるので実質的にスイートとして機能させることができる
	Describe(/* 省略 */);
	Describe("A spec using BeforeEach and AfterEach", [this]()
	{
		BeforeEach([this]()
		{
			RunOrder = TEXT("A");
		});

		It("will run code before each spec in the Describe and after each spec in the Describe", [this]()
		{
			TestEqual("RunOrder", RunOrder, TEXT("A"));
		});

		AfterEach([this]()
		{
			RunOrder += TEXT("Z");
			TestEqual("RunOrder", RunOrder, TEXT("AZ"));
		});
	});
}

内部実装でもほとんど使われていないテスト方法なので、いろいろ不具合があるかもしれませんが、ちょっと使っていってみようかと思います。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?