520 likes | 654 Views
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?.
E N D
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? • Before check-in • One week into the coding milestone • 1 week before shipping
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
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
The Plan • Getting Started • Unit Testing Introduction • Silverlight Unit Testing • What’s Different? • Demos • Advanced Topics • Community + Resources • Questions
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
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
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
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
Unit Test Metadata // Attributes [TestClass] [TestMethod] [ExpectedException(typeof(ArgumentException))] [Priority(1)]
Unit Test Assertions // Assertions Assert.IsTrue(x == 3, “X should be 3”); Assert.IsNotNull(myControl) StringAssert.Contains(…)
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
Silverlight Unit Test Framework • Same Visual Studio metadata and assertions • Focus on simplicity • Modern user interface & results management at runtime
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
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
Multiple CLRs • If you’re sharing code between Microsoft development platforms, you’re using multiple Common Language Runtimes today WPF Silverlight Windows Phone
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
Silverlight Unit Testing Walkthrough DEMO
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
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
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
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
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
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
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
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
Silverlight Unit Test Framework Attributes [Asynchronous] [Tag(“PropertyTest”)] [Bug(“TFS 5425”, Fixed=true)]
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.
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
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)]
Windows Phone Testing • The Silverlight 3 build of the test framework runs on the phone • Simplified experience today • Very helpful to verify NETCF vsCoreCLR
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
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
Community Assistance • Silverlight.net forums • StackOverflow • Bing
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
Questions? • Please fill out an evaluation form! • Jeff Wilcox • Email jwilcox@microsoft.com • Blog www.jeff.wilcox.name
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
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
Required Slide Complete an evaluation on CommNet and enter to win!
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