UnitTestでテストメソッドを実行するとき、そのメソッドの開始前と終了後に自動的に実行されるメソッドを指定することができます。
これらは例えばテスト開始前にデータを入れ込んだり、テスト開始後に設定をリセットしたりするときなどに使います。
開始前に実行したいメソッドにはSetup属性を、終了後に実行したいメソッドにはTearDown属性をつけます。
[TestFixture] public class SetupTeardownTest { [SetUp] public void setup() { Debug.Log ("setup"); } [TearDown] public void teardown() { Debug.Log ("teardown"); } [Test] public void sampleFunctionTest() { Debug.Log ("sampleFunctionTest"); Assert.Pass (); } [Test] public void sampleFunctionTest2() { Debug.Log ("sampleFunctionTest2"); Assert.Pass (); } }
というクラスを作成して、SetupTeardownTestを実行した場合、コンソールには
というようにログが記録されます。
sampleFunctionTestおよびsampleFunctionTest2の実行前にはSetup属性をつけたsetup()が、実行後にはTearDown属性をつけたteardown()が実行されているのがわかります。