【C#】【NUnit】 NUnitForms ~ あれこれ編 ~

■ はじめに

今回行う事項

【1】GroupBoxやPanelなどの上のコントロールを取得するには
【2】タブにあるコントロールをテストするには 
【3】DataGridViewなどサポートされていないコントロールをテストするには

【1】GroupBoxやPanelなどの上のコントロールを取得するには

 * 対象コントロールを右クリックし、「'XXXXX'の選択」となっているので、
   定義を指定する際に順々にこれを参考に順々に指定してやる

サンプル

* [groupBox1]の上に「radioButton1(Checked : True)」、「radioButton2(Checked : False)」がある場合
RadioButtonTester radioButtonTester1 = new RadioButtonTester("groupBox1.radioButton1"); // 定義
RadioButtonTester radioButtonTester2 = new RadioButtonTester("groupBox1.radioButton2"); // 定義

Assert.IsTrue(radioButtonTester1.Checked); // Checkされているかを確認
Assert.IsFalse(radioButtonTester2.Checked); // Checkされていないかを確認

【2】タブにあるコントロールをテストするには

 * 対象コントロールを右クリックし、「'XXXXX'の選択」となっているので、
   定義を指定する際に順々にこれを参考に順々に指定してやるだけではなく、そのタブをアクティブにする必要がある

サンプル

* タブtabControl1が2つあって、2つ目のタブページ上に、チェックボックスcheckBox1がある場合
[Test]
public void Test4()
{
    CheckBoxTester checkBox1Tester = new CheckBoxTester("tabControl1.tabPage2.checkBox1");
    TabControlTester tabControl1Tester = new TabControlTester("tabControl1");

    tabControl1Tester.SelectTab(1); // これをやっておかないと、checkBox1Tester.Click()でエラーになる
    Assert.IsTrue(checkBox1Tester.Properties.Enabled);
    Assert.IsFalse(checkBox1Tester.Checked);
    checkBox1Tester.Click();
    Assert.IsTrue(checkBox1Tester.Checked);
}

【3】DataGridViewなどサポートされていないコントロールをテストするには

 * ControlFinderでコンポーネントインスタンスが取得できる
 * 標準テスタクラスが提供されていないコントロールは、ControlTester()を使用する。
   (DataGridViewで試したがキャストできなかった。なぜ?)

サンプル

[Test]
public void Test5()
{
    ControlFinder finder = new ControlFinder("dataGridView1");
    DataGridView dataGridViewTester = (DataGridView)finder.Find();
    Assert.IsTrue(dataGridViewTester.Enabled);
}

関連記事

NUnitForms ~ Hello World編 ~

https://blogs.yahoo.co.jp/dk521123/21001385.html

NUnitForms ~ サンプル編 ~

http://blogs.yahoo.co.jp/dk521123/21118440.html

C#】DataGridViewのイベントに関する単体試験

https://blogs.yahoo.co.jp/dk521123/23688719.html