1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WinformのUI部品のプロパティのテストをする

Last updated at Posted at 2025-10-18

背景

これまで新規機能を追加する際、UI部品(ボタン、テキストボックス、ラベルなど)の各種設定項目について、手動でのレビューが必要でした。特に以下の点が課題となっていました:

設定漏れや設定ミスの発見に時間がかかる
レビュアーが全てのUI部品を目視確認する必要がある
人的ミスのリスクが存在する

本実装では、UI部品の設定値を自動テストで検証する仕組みを導入し、レビュー工数の削減と品質向上の両立を目指します。

構成図

成果物

プロダクトコード

画面

テキストボックスのプロパティについてテストします。

image.png

Form.vb

何も設定していないです。

Form.vb
Public Class Form1
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)

    End Sub

    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles 申請日時.ValueChanged

    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

    End Sub
End Class


TestUIAdapter.vb

TestUIAdapter.vb
' FormのUI要素のプロパティを取得するためのアダプタークラス
' UIはデフォルトでFormのインスタンスを渡す
Public Class TestUIAdapter
    Private ReadOnly _form As Form

    ' コンストラクタでFormインスタンスを受け取る
    Public Sub New(form As Form)
        _form = form
    End Sub

    ' FormのDatePickerの値を取得する
    Public Function GetDatePickerValue(datePickerName As String) As DateTime
        Dim picker = TryCast(_form.Controls(datePickerName), DateTimePicker)
        If picker IsNot Nothing Then
            Return picker.Value
        Else
            Throw New ArgumentException("指定されたDatePickerが見つかりません: " & datePickerName)
        End If
    End Function

    ' FormのTextBoxのテキストを取得する
    Public Function GetTextBox(textBoxName As String) As TextBox
        Dim textBox = TryCast(_form.Controls(textBoxName), TextBox)
        If textBox IsNot Nothing Then
            Return textBox
        Else
            Throw New ArgumentException("指定されたTextBoxが見つかりません: " & textBoxName)
        End If
    End Function

    ' 使い方例:
    ' Dim adapter As New TestUIAdapter(Me)
    ' Dim dateValue As DateTime = adapter.GetDatePickerValue("dateTimePicker1")
    ' Dim textValue As String = adapter.GetTextBoxText("textBox1")
End Class


テストコード

UIPropertyTest.vb
Imports System.Drawing
Imports TestStack.White.UIItems
Imports VBUI
Imports Xunit

Public Class UIPropertyTest
    <Fact>
    Public Sub FormのコントロールTextBoxのプロパティ情報をテストする()
        Dim ctrlName As String = "名前"

        ' Arrange 
        Dim form As New VBUI.Form1()
        Dim textBox As New System.Windows.Forms.TextBox()

        ' Act
        Dim adapter As New TestUIAdapter(form)
        Dim targetTextBox = adapter.GetTextBox(ctrlName)

        ' Assert
        Assert.Equal("Window", targetTextBox.BackColor.Name) ' 背景色
        Assert.Equal("WindowText", targetTextBox.ForeColor.Name)        ' 前景色
        Assert.Equal("MS UI Gothic", targetTextBox.Font.Name)    ' フォント名
        Assert.Equal(9.0F, targetTextBox.Font.Size)             ' フォントサイズ
        Assert.Equal(19, targetTextBox.Size.Height)              ' 高さ
    End Sub
End Class




感想

実務で十分に活用できる機能です。一度実装すれば他の機能でも使い回せるため、実装コストを大幅に削減できます。また、人間によるチェックが必要な箇所を減らせる点も大きなメリットです。今回はxunitを使用しましたが、次はMSTestでも検証してみる予定です。

他に確認したいこと

・GrapecityのInputMan、MultiRowへの適用
理論上適用可能、検証が必要

・GrapecityのActiveReportsへの適用
レポート部品の設定値検証に活用できる見込み
実装できれば品質向上につながる

参考

github コミット分
https://github.com/RYA234/WindowsForm_sample/commit/de43c71675341d15f26867c5a16598e795a0f7e2

デザインパターン Adapter
https://ja.wikipedia.org/wiki/Adapter_%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?