.NETテストプログラミング入門6
一般的にテストで難しいとされているプログラムは並行処理とGUIです。並行処理については前回述べましたので、今度はGUIのテストについての解説をします。
先ずはテスト対象となるGUIのコードを見てみましょう。
using System;
using System.Windows;
using System.Windows.Controls;
static class Program
{
[STAThread]
static void Main()
{
SampleApplication app = new SampleApplication();
app.Run();
}
}
class SampleApplication : Application
{
public SampleApplication()
{
this.Startup += ( object sender, StartupEventArgs e ) =>
{
PiyoWindow win = new PiyoWindow();
win.Show();
};
}
}
//テストの対象となるクラス
class PiyoWindow : Window
{
private StackPanel panel;
private TextBox inputValueBox;
Button piyoAddButton;
private Label outPutValueBox;
public PiyoWindow()
{
//Windowの設定
this.Title = "テスト対象ピヨ♪";
this.MinHeight = 150;
this.MaxHeight = 150;
this.MinWidth = 300;
this.MaxWidth = 300;
this.WindowStartupLocation =
WindowStartupLocation.CenterScreen;
//各種コントロールの配置
panel = new StackPanel();
this.Content = panel;
inputValueBox = new TextBox();
inputValueBox.Text = "ここに文字を入力して下さい。";
panel.Children.Add( inputValueBox );
piyoAddButton = new Button();
piyoAddButton.Content = "出力ピヨ♪";
piyoAddButton.Click += ( object sender, RoutedEventArgs e ) =>
outPutValueBox.Content = inputValueBox.Text + "ピヨ♪";
panel.Children.Add( piyoAddButton );
outPutValueBox = new Label();
panel.Children.Add( outPutValueBox );
}
}
このサンプルコードは、ボタンをクリックすると一番上のテキストボックスに入力した文字列に「ピヨ♪」を付け、一番下に表示されるラベルに表示するという実にシンプルなWPFプログラムです。このサンプルは単純ですが、GUIテストの本質を知る材料になります。答えを見る前に自分でテストコードをコーディングしてみて下さい。続く...