1 / 52

Unit Testing in Microsoft Silverlight

Required Slide. SESSION CODE: WEB205. Unit Testing in Microsoft Silverlight. Jeff Wilcox Senior Software Development Engineer Silverlight jwilcox@microsoft.com. How many bugs do professional developers create?. Per KLOC 0 5 15 100 150. How much does it cost to fix a bug?.

ethan
Download Presentation

Unit Testing in Microsoft Silverlight

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Required Slide SESSION CODE: WEB205 Unit Testing in Microsoft Silverlight Jeff Wilcox Senior Software Development Engineer Silverlight jwilcox@microsoft.com

  2. How many bugs do professional developers create? Per KLOC • 0 • 5 • 15 • 100 • 150

  3. How much does it cost to fix a bug? • Before check-in • One week into the coding milestone • 1 week before shipping

  4. How much does it cost to fix a bug? • Before check-in • One week into the coding milestone • 1 week before shipping • 1 week after shipping • 1 year after shipping, with a new development team • With an app in an app store

  5. Opportunities for Silverlight testing • Leave today with • Everything you need to get started • An understanding of unit test principals as they apply to Silverlight • Helpful tips & tricks • Not so much… • Performance • UI tests • Server load testing

  6. The Plan • Getting Started • Unit Testing Introduction • Silverlight Unit Testing • What’s Different? • Demos • Advanced Topics • Community + Resources • Questions

  7. Getting Started

  8. What is Unit Testing? Focus on the benefits & goal • Exercise business objects, states, validate • Cover as many code paths as possible • Keep the suits happy • Developers write unit tests

  9. Unit Tests are Assets • Tests are important to the stability of the business • Opportunity to make long-term investments in quality • Essential for framework & control developers

  10. Testability is Key • Test code • is product code • can have bugs • Testability should drive your product design • Need a plan in place for handling test failures & bad tests

  11. Microsoft Test / MSTest / VSTT Microsoft’s unit test framework • Metadata • Assertions • Will be familiar to NUnit users • Not MSTest.exe, the full framework execution environment

  12. Unit Test Metadata // Attributes [TestClass] [TestMethod] [ExpectedException(typeof(ArgumentException))] [Priority(1)]

  13. Unit Test Assertions // Assertions Assert.IsTrue(x == 3, “X should be 3”); Assert.IsNotNull(myControl) StringAssert.Contains(…)

  14. Unit Testing in Visual Studio 2010 DEMO

  15. Silverlight Unit Testing

  16. What tools do I need? • A .NET development tool targeting Silverlight • Visual Studio 2010 • Web Developer Express • Expression Blend • Silverlight 4 recommended • Silverlight Toolkit • Silverlight Unit Test Framework and Metadata • Unit Test Project Template

  17. http://silverlight.codeplex.com/

  18. Silverlight Unit Test Framework • Same Visual Studio metadata and assertions • Focus on simplicity • Modern user interface & results management at runtime

  19. Silverlight Unit Test Framework • Same Visual Studio metadata and assertions • Focus on simplicity • Modern user interface & results management at runtime • No integration with Visual Studio test features

  20. What’s different about Silverlight unit testing? • Tests run on the single UI thread • Live Silverlight runtime • Tests are serialized • Everything is in the platform sandbox FYI Silverlight Tests are Silverlight Applications

  21. Multiple CLRs • If you’re sharing code between Microsoft development platforms, you’re using multiple Common Language Runtimes today WPF Silverlight Windows Phone

  22. Where do tests run? • Any Silverlight platform • Windows • Mac OS X • Windows Phone • Many experiences • Firefox • Chrome • Safari • Windows Internet Explorer • Out-of-browser apps

  23. Updated in April 2010

  24. Silverlight Unit Testing Walkthrough DEMO

  25. Don’t try this at home… • Using Real Web Services • WebClient • HttpWebRequest • WCF • REST • Isolated Storage • Navigation Framework • Replacing the RootVisual • Hardware Devices • Accelerometer • Web Cameras • OS Dialogs • OpenFileDialog • SaveFileDialog • MessageBox • Asserts

  26. Great Application Design Practices • View + Model • DataContext is your friend • Data Binding • Inversion of Control (IoC) • Decouple your components • Create factories and inject dependencies in constructors • Consider mocking

  27. Getting data into your apps & tests • IMyDataRepository • AzureDataRepository • AmazonDataRepository • SampleDataRepository • IoC: use data repositories • Tests target the sample data repository • You’ll need to implement asynchronous data repositories • Hook Event: GetUsersCompleted • Method: GetUsersAsync • Consider using lambdas & Actions instead of the full async model

  28. Mocking • Mocking can be difficult for web requests • Without injectors, framework APIs that hit native Silverlight are hard to test • Moq has a Silverlight version

  29. Testing Web Services • Test the Silverlight stack independently • Class library, class library test project • Client app model, unit test project with IoC • May not need to your Page.xaml(View) • Test the web service stack • Web service test projects • Web service load and performance testing • Don’t try to do it all at the same time • Difficult to debug • Too many moving parts

  30. How we test on the Silverlight team • Controls and framework use inherited tests & test generation • ControlTests; ContentControlTests; ItemsControlTests • Multiple code paths for properties • XAML • Code • We have sophisticated internal frameworks for testing rendering and complete end-to-end applications • Full control test source shared in the Silverlight Toolkit

  31. The platform is difficult to test • Silverlight Load Testing • Pixel-by-pixel Rendering • Negative tests for • Isolated storage lockdown • Network connectivity changes • Security & group policy situations

  32. What else?

  33. Using Blend to write and run tests DEMO

  34. Silverlight Unit Test Framework-specific features • Built over time to help our team • Tag Expressions for test selection • [Bug] attribute for tracking known and fixed issues • [Asynchronous] dispatcher tests • Not part of the full VS Test Framework • Can permit functional testing

  35. Silverlight Unit Test Framework Attributes [Asynchronous] [Tag(“PropertyTest”)] [Bug(“TFS 5425”, Fixed=true)]

  36. Tags • Every test method and test class has an implicit set of tags • Type name (“MyControlTests”, “TestMethod6”) • Full type name (“MyNamespace.MyTestClass.MyMethod”) • Priority attribute • You can add your own tags • [Tag(“RequiresFocus”)] • Future versions will add support for Category, Bug, etc.

  37. Tag Expressions • Rich “tag expression” language permits selection • Sample tags • All • !RequiresFocus • All-(DependencyPropertyTests+WebServiceTests) • Computer Science or Math Geek? • TagManager.ExpressionEvaluator.cs • Extended Backus-Naur Form (EBNF) grammar

  38. Tag Expressions at Runtime

  39. Bug Attribute • Nice way to track known failures with associated bugs • Reverse the result of a test [Bug(“TFS 152”)] • Test fails, result is pass • Test passes, result is failure After fixing the bug… [Bug(“TFS 152”, Fixed=true)]

  40. Advanced Silverlight Unit Tests DEMO

  41. Windows Phone Testing • The Silverlight 3 build of the test framework runs on the phone • Simplified experience today • Very helpful to verify NETCF vsCoreCLR

  42. Test Automation • Tools exist in the Silverlight Toolkit and the community • Require interactive Windows sessions • CruiseControl.NET, TFS Build • We’re looking for a more solid solution

  43. Community Tools and Alternatives • Free tools in the community • StatLight: Free test runner • Moq • NUnit • For commercial tools and products • R# integration • SilverUnit, CThru and TypeMock

  44. Community Assistance • Silverlight.net forums • StackOverflow • Bing

  45. Call to Action Get the bits today http://silverlight.codeplex.com/ Participate in the community and share your thoughts on the direction of Silverlight testing

  46. Questions? • Please fill out an evaluation form! • Jeff Wilcox • Email jwilcox@microsoft.com • Blog www.jeff.wilcox.name

  47. Required Slide Track PMs will supply the content for this slide, which will be inserted during the final scrub. Track Resources • ASP.NET – http://www.asp.net/ • Silverlight – http://silverlight/ • Expression – http://www.microsoft.com/expression/ • Internet Explorer – http://www.microsoft.com/windows/internet-explorer/default.aspx

  48. Required Slide Resources Learning • Sessions On-Demand & Community • Microsoft Certification & Training Resources www.microsoft.com/teched www.microsoft.com/learning • Resources for IT Professionals • Resources for Developers • http://microsoft.com/technet • http://microsoft.com/msdn

  49. Required Slide Complete an evaluation on CommNet and enter to win!

  50. Sign up for Tech·Ed 2011 and save $500 starting June 8 – June 31st http://northamerica.msteched.com/registration You can also register at the North America 2011 kiosk located at registrationJoin us in Atlanta next year

More Related