NUnit에서 시스템 테스트를 작성하려고하며 ms의 UI Automation을 사용하여 UI를 호출하려고합니다.NUnit 테스트에서 WPF 텍스트 상자에 대한 setter 호출 방법
내 호출이 실패합니다. 온라인에서 컴파일 테스트를 작성할 수 있지만 내 주장이 실패하는 상태에 이르는 힌트가 온라인에서 발견되었습니다.
다음은 컴파일 가능한 최소 예입니다. 내 문제는 예제에서 실패한 테스트입니다.
출원 XAML
<Application x:Class="InvokeTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:InvokeTest"
Startup="Application_Startup"/>
출원 CS
using System.Windows;
namespace InvokeTest
{
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
var view = new MainWindow();
var viewmodel = new MainWindowViewModel();
view.DataContext = viewmodel;
view.Show();
}
}
}
윈도우 XAML
<Window x:Class="InvokeTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:InvokeTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<TextBox x:Name="MyTextBox" x:FieldModifier="public" Text="{Binding TextProperty, UpdateSourceTrigger=PropertyChanged}" />
</Window>
창 CS
using NUnit.Framework;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
namespace InvokeTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class MainWindowViewModel
{
string textfield;
public string TextProperty
{
get { DebugLog("getter"); return textfield; }
set { textfield = value; DebugLog("setter"); }
}
private void DebugLog(string function)
{
Debug.WriteLine(ToString() + " " + nameof(TextProperty) + " " + function + " was called. value: '" + textfield ?? "<null>" + "'");
}
[TestFixture, Apartment(ApartmentState.STA)]
public class WPFTest
{
MainWindow view;
MainWindowViewModel viewmodel;
[SetUp]
public void SetUp()
{
view = new MainWindow();
viewmodel = new MainWindowViewModel();
view.DataContext = viewmodel;
}
[Test]
public void SetTextBox_NoAutomation()
{
string expected = "I want to set this";
view.MyTextBox.Text = expected;
Assert.AreEqual(expected, viewmodel.TextProperty);
/*
Test Name: SetTextBox_NoAutomation
Test Outcome: Failed
Result Message:
Expected: "I want to set this"
But was: null
*/
}
[Test]
public void SetTextBox_UIAutomation()
{
string expected = "I want to set this";
SetValue(view.MyTextBox, expected);
Assert.AreEqual(expected, viewmodel.TextProperty);
/*
Test Name: SetTextBox_UIAutomation
Test Outcome: Failed
Result Message:
Expected: "I want to set this"
But was: null
*/
}
private static void SetValue(TextBox textbox, string value)
{
TextBoxAutomationPeer peer = new TextBoxAutomationPeer(textbox);
IValueProvider provider = peer.GetPattern(PatternInterface.Value) as IValueProvider;
provider.SetValue(value);
}
}
}
}
EDIT # 1 : @Nkosi가 내 xaml에 바인딩 오류가 있다고 지적했습니다.
EDIT # 2 : 수동 테스트가 가능한 보일러가 추가되었으며 자동 실행 기능이없는 동작을 보여주는 유스 케이스가 추가되었습니다. 그것은 단지 부업이며, 자문은이 질문의 핵심입니다.
이미 알고 계실지 모르지만 단위 테스트는 실제로 UI를 테스트하도록 설계되지 않았습니다. 그것은 아마도 작업에 "해킹"될 수 있지만, 당신이가는 동안 당신은 시스템을 싸우게 될 것입니다. UI 테스트는 일반적으로 코드화 된 UI 테스트 및 스크립팅 된 통합 테스트의 일부로 수행됩니다. –
@BradleyUffner 예. 알고 있습니다. 실제로 코드화 된 UI 테스트를 사용하지 않고도 자동으로 Google auto를 사용할 수 없습니다. 코드화 된 UI 테스트가 우수하다고 생각하지만 실제로는 좀 더 자세히 설명하고 싶습니다. – Johannes