Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cory Foy April 29th, 2005 MSDN Code Camp Charlotte, NC Test Driving ASP.NET Development Cory Foy, Software Engineer MobileHWY,

Similar presentations


Presentation on theme: "Cory Foy April 29th, 2005 MSDN Code Camp Charlotte, NC Test Driving ASP.NET Development Cory Foy, Software Engineer MobileHWY,"— Presentation transcript:

1 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Test Driving ASP.NET Development Cory Foy, Software Engineer MobileHWY, LLC Cory.Foy@mobilehwy.com http://www.cornetdesign.com

2 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Session Timeline Agile Manifesto Overview Test Driven Development Overview Test First UI Overview NUnitASP TFUI with NUnitASP Mocking ASP.NET Pages Summary

3 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Agile Manifesto Four Principles –Individuals and Interactions (over processes and tools) –Working Software (over comprehensive documentation) –Customer Collaboration (over contract negotiation) –Responding to change (over following a plan)

4 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Extreme Programming Automate all manual processes Do nothing without frequent feedback Don’t have a bus number of “1” –Bus Number – Number of people that can be hit by a bus that your project can’t do without –How to achieve if you are emphasizing Working Software over comprehensive documentation?

5 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Test Driven Development It’s about coding It’s about design Way of developing applications by writing tests Red-Green-Refactor –Write a failing test –Make the test pass by doing the simplest thing –Refactor duplication

6 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Test Driven Development NUnit (http://www.nunit.org)

7 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Test Driven Development Write a test Watch it fail

8 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Test Driven Development Make the test pass

9 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Test Driven Development Make it right (Refactor) –Pull information from data store Have data store be able to be reset to a common baseline Tests shouldn’t rely on the results of other tests

10 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Test First UI - Primary work done by Phlip Plumlee - Principles paper available at http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces Why is TDD for UIs naturally hard? –Should be able to write test cases that force predictable changes in UI –All libraries should submit to program control –Look and feel problems

11 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Test First UI Reveal() –Phlip’s concept to temporarily show the form under test for manual inspection when needed. Why is TDD for UI Artificially hard? –UIs have steep learning curves for extending –UI tools designed for drag-n-drop development, not for complex, robust applications –UI tools make debugging easy

12 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Test First UI Unique challenges with the web –Server side and Client side code –Different browsers render code differently –Variety of user agents (desktop, PDA, mobile)

13 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp http://nunitasp.sourceforge.net Extension to NUnit Originally created by Brian Knowles, now maintained by Jim Shore Provides NUnit with the ability to download, parse and manipulate ASP.NET pages Meant for programmers, not QA teams

14 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp Uses a Browser property for loading web pages and making assertions about various test conditions Uses AspTester objects to access properties of the controls, even complex nested or user controls

15 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp

16 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp Testable Controls System.Web.UI.WebControls Button CheckBox DataGrid DropDownList ImageButton Label LinkButton ListBox Panel RadioButton TextBox UserControl ValidationSummary System.Web.UI.HtmlControls HtmlAnchor HtmlInputCheckBox

17 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp TDD With NUnitAsp –First write failing test

18 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp Now pass the test

19 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp With the page created, we want to make sure it is working. This means that when a user selects a favorite from the list, the user gets redirected to a different page which retrieves the score for the team.

20 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp First write a test

21 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp And make it pass

22 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp Finally, refactor

23 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp NUnitAsp Tips http://codebetter.com/blogs/darrell.norton/archive/2004/01/22/5950.aspx http://codebetter.com/blogs/darrell.norton/archive/2004/01/22/5950.aspx –Test for the URL of the page you expect –Prefer tests for positive visibility – false can be misleading –Make sure your app works by browsing to it –Test only critical controls –Long wait – slower than library tests

24 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC NUnitAsp NUnitAsp Drawbacks –Can only assert HttpContxt based information like Control visibility, Control values, Cookie s, etc. –Certain Control attributes that aren’t sent to the client can’t be tested ( DataTextField, DataValueField for DropDownList s –Session and Cache values can’t be tested directly

25 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages What are some problems with our current DisplayFavorites.aspx? –Business logic embedded in page –Difficult to test NUnitAsp can only test so much NUnit testing would be extremely difficult –Code not reusable in other pages

26 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages How can we solve this? –Use interfaces to permit substitution –Extract business and presentation logic to a controller / presenter (MVP) http://www.martinfowler.com/eaaDev/ModelViewPresenter.html –Modify our page to use this controller –Create a mock that can be passed to the controller for deeper testing

27 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Building the interface –What are we trying to accomplish? Get the favorites for the user Display the favorites to the user Let the user select a favorite Act on the selection action by getting the current score for the favorite

28 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Extract the services you want the page to provide out into an interface

29 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages What is a “mock”? –http://www.mockobjects.comhttp://www.mockobjects.com –Objects that pretend to be real objects, but only contain enough logic to let us run our code

30 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Why use mock objects? –The real object has nondeterministic behavior –The real object is difficult to set up –The real object has behavior that is difficult to trigger –The real object is slow –The real object has (or is) a user interface –The test wants to ask the object about how it was used –The real object does not exist yet

31 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Why mock ASP.NET pages? –Can dig deep into what actually happened on the page –Can insert test values into Session and Cache –Can find out what methods were called, if callbacks were executed, etc. –GUI Testing is hard

32 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages How will we use the mocks? –Mock will implement the common interface –We will pass it into our controller as if it were our real page –The controller will act on it as if the mock were the real page –We can inspect the state of fields on the mock to see what the controller actually did

33 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Write tests for our mock’s behavior

34 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Create a mock object that implements our interface

35 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Extracting Business logic to controller –Model-View-Presenter pattern Related to Model-View-Controller Separates the behavior of a presentation from the view while allowing the view to receive user events Also known as Humble Dialog Box http://www.martinfowler.com/eaaDev/ModelViewPresenter.html

36 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Extracting Business logic to controller (cont) –To use MVP, we need to create a controller capable of interacting with our DisplayFavorites logic –Controller should accept the IDisplayFavorites interface and perform all business logic and presentation logic through that interface

37 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Write tests for our controller’s behavior

38 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Create a controller responsible for accepting the IDisplayFavorites view and acting on it

39 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Create a controller (continued) Note the code here was extracted from our Web page, but now resides in our controller

40 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC We will now hook our mock object up to our controller as if it is our ASP.NET page

41 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Testing our mock object in the controller

42 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages See if our tests pass (Note that our web page test is still passing because we haven’t modified it yet. Always keep the tests green!)

43 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Modify our web page to implement the interface Note that we are raising the FavoriteSelected event when the user clicks the button

44 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Make sure our tests still pass We’ve now refactored our ASP.NET to extract the common functionality to a base class, all while keeping the pages running

45 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages What have we gained by mocking the page? We can now test page members we wouldn’t normally be able to test –Session variables –Control attributes –Method calls

46 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages What else have we gained? –Since the logic is moved out of the page, implementing the interface is mostly hooking up the plumbing –This means that creating pages that make use of this functionality is just a matter of hooking up their plumbing as well

47 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Creating a Mobile version –Implement the interface

48 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Mocking ASP.NET Pages Wait! Wait, Wait Wait! Where are the tests? –NUnitAsp doesn’t currently support testing of Mobile pages –Work is being done on this – goal to have an extension written by the end of summer to do basic testing of Mobile Controls

49 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Summary Do nothing without frequent feedback Testing GUIs is hard – but can be mitigated Try to make GUI tests (and GUIs) as thin as possible Activation Energy – modules that have tests take less Activation Energy to write more tests

50 Cory Foy http://www.cornetdesign.com April 29th, 2005 MSDN Code Camp Charlotte, NC Summary Further information –http://www.testdriven.com –Yahoo! Groups TDD, TFUI, Refactoring –http://www.nunit.orghttp://www.nunit.org –http://www.testdriven.net (VS plugin)http://www.testdriven.net –http://nunitasp.sourceforge.nethttp://nunitasp.sourceforge.net –http://www.cornetdesign.com (presentation)http://www.cornetdesign.com


Download ppt "Cory Foy April 29th, 2005 MSDN Code Camp Charlotte, NC Test Driving ASP.NET Development Cory Foy, Software Engineer MobileHWY,"

Similar presentations


Ads by Google