Unit Testing your Silverlight Applications Using the Silverlight
Unit Testing FrameworkBy, Ben Dewey
Senior Software DevelopertwentySix New York
http://bendewey.com/bloghttp://twitter.com/bendewey
OverviewWhat is Testing/TDDSetting up the Silverlight Unit Testing Test HarnessBasic Unit TestAsynchronous Unit TestsQuestions
PrefaceUnit Testing (MSTest)
[TestMethod]public void Can_CreateCar(){ // Arrange // Act var car = new Car(); // Assert Assert.IsNotNull(car);}
Test Driven Design (TDD)Testing first and allowing your tests/requirements to drive
your design
Setting up the Test HarnessAdd References
Microsoft.Silverlight.TestingMicrosoft.VisualStudio.QualityTools.UnitTesting.Silverlight
Modify App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e){ RootVisual = UnitTestSystem.CreateTestPage();}
Asynchronous Unit Tests [TestClass] public class MainPageTests : SilverlightTest { [TestMethod, Asynchronous] public void Can_ShowHide_Windows() { // Arrange var controller = new GameController(); var mainPage = new MainPage(controller); this.TestPanel.Children.Add(mainPage); var startWindow = mainPage.FindName("StartWindow") as UIElement; var endWindow = mainPage.FindName("EndWindow") as UIElement;
// Act // Assert EnqueueDelay(500); EnqueueCallback(() => { controller.ShowStartScreen = false; Assert.AreEqual(Visibility.Collapsed, startWindow.Visibility); Assert.AreEqual(Visibility.Collapsed, endWindow.Visibility); });
EnqueueDelay(500);
EnqueueTestComplete(); } }
Links
http://silverlight.codeplex.com
http://www.jeff.wilcox.name/Jeff Wilcox – Creator of SUT
Thank YouBy, Ben Dewey
http://bendewey.com/bloghttp://twitter.com/bendewey