View Presentation


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

View Presentation

  1. 1. Testing Testing Testing <ul><li>Joakim Björklund </li></ul><ul><li>Director of IT Services Division </li></ul><ul><li>Linköpings universitet </li></ul><ul><li>Sweden </li></ul>
  2. 2. Testing testing testing <ul><li>“ You all know the need for testing, but how do you actually find time to do it? This presentation gives an outline on some of the tools you can use to perform testing of your Java application and save time while weeding out the bugs. The tools discussed include HttpUnit and Canoo.” </li></ul><ul><li>An odyssey of open source tools for testing. </li></ul>
  3. 3. Testing testing testing <ul><li>Before we get started: </li></ul><ul><li>Get a good software testing book! </li></ul><ul><li>Go a course on testing at the Computer Science department! </li></ul><ul><li>Follow a software production model </li></ul><ul><li>Automate all testing! </li></ul><ul><li>Daily builds! (With testing and test reports) </li></ul><ul><li>Documentation (JavaDocs and Wiki) </li></ul>
  4. 4. Testing testing testing <ul><li>Design patterns </li></ul><ul><li>Refactoring </li></ul><ul><li>Reusability </li></ul><ul><li>Don’t gold plate the code! </li></ul><ul><li>Spend a moment or two on system architecture </li></ul>
  5. 5. Testing Testing Testing <ul><li>You all know the need for testing? Or do you? </li></ul><ul><li>When are you supposed to take the cost for testing? - During development or during system maintance? </li></ul><ul><li>Automated testing – take the time to save time! </li></ul><ul><li>When are your application done?!? </li></ul>
  6. 6. Testing Testing Testing <ul><li>Testing is a bit more than: </li></ul><ul><li>System.out.println(“This works!”); </li></ul><ul><li>Debugger expressions </li></ul><ul><li>Small test scripts </li></ul>
  7. 7. “ Testing theory” <ul><li>Testing can be done on several “layers” of the product being developed. </li></ul><ul><li>Unit testing – domain/application logic </li></ul><ul><li>Integration testing – APIs </li></ul><ul><li>Functional testing – acceptance, top-level </li></ul><ul><li>Performance testing – the need for speed </li></ul><ul><li>&quot;Regression testing&quot; is the concept of testing that asserts that everything that worked yesterday still works today </li></ul>
  8. 8. Automatic Testing <ul><li>In order to decrease the cost, time and hassle during the testing phase, the first thing to consider is automatic testing. </li></ul><ul><li>” If a program feature lacks an automated test, we assume it doesn’t work. This seems much safer than the prevailing assumption, that if a developer assures us a program feature works, then it works now and forever.” </li></ul><ul><li>Helps regression testing (or is a prerequisite) </li></ul>
  9. 9. Tools - mentioned <ul><li>Testing tools </li></ul><ul><li>JUnit – unit testing </li></ul><ul><li>Cactus – unit/integration </li></ul><ul><li>Canoo – functional testing </li></ul><ul><li>HTTPunit – functional testing </li></ul><ul><li>JMeter – performance testing </li></ul><ul><li>Other useful tools </li></ul><ul><li>Ant – build tool </li></ul><ul><li>Bugzilla – bug tracking tool </li></ul>
  10. 10. Open Source vs Commercial <ul><li>As you noticed all tools in this presentation is Open Source. One of the reasons for this is that the cost for the testing tool should not be an obstacle for the testing process itself. </li></ul><ul><li>Access to the source code… </li></ul>
  11. 11. Ant <ul><li>Ant is a build tool that enables you to automate the build process. </li></ul><ul><li>Ant – in java for java </li></ul><ul><li>Build files in XML i.e. build.xml </li></ul><ul><li>Every build file contains one project element. A project element contains target elements. Each target consists of a set of task elements. </li></ul><ul><li>Many of the tools described in this presentation utilize Ant… </li></ul>
  12. 12. Ant <ul><li>Example of targets: </li></ul><ul><li>Test – run the tests </li></ul><ul><li>Clean – Clean out the output directories </li></ul><ul><li>Deploy – Compile the JARs, WARs etc to a exec system </li></ul><ul><li>Publish – Output the source and binaries to any distribution site </li></ul><ul><li>All – Perform clean, fetch, build, test, docs and deploy </li></ul>
  13. 13. JUnit <ul><li>JUnit is a framework for writing unit tests. </li></ul><ul><li>Test case = a related set of tests, typically every class should have a test case. </li></ul><ul><li>Test fixture – the resources needed to run the test </li></ul><ul><li>Test suite – a collection of related test cases. </li></ul>
  14. 14. JUnit <ul><li>Subclass junit.framework.TestCase </li></ul><ul><li>If you need fixture objects, override the setUp() method. </li></ul><ul><li>Define a number of tests that return void and whose method name begin with test (testAdd(), testPut() etc) </li></ul><ul><li>If you need to release resources that where part of the fixture, override the tearDown() method </li></ul><ul><li>If needed define a suite of tests </li></ul>
  15. 15. JUnit
  16. 16. JUnit <ul><li>To get xml-output from the test: </li></ul><ul><li><target name=”test” depends=”compile”> </li></ul><ul><li><junit printsummary=”true”> </li></ul><ul><li><formatter type=”xml” /> </li></ul><ul><li><test name=”junit.samples.TEST”> </li></ul><ul><li><classpath> </li></ul><ul><li><pathelement location=”${outdir}” /> </li></ul><ul><li></classpath> </li></ul><ul><li></junit> </li></ul><ul><li></target> </li></ul><ul><li>JUnitreport – to generate WebPages of the result </li></ul>
  17. 17. Cactus <ul><li>A tool for tests on servlets and other J2EE components. </li></ul><ul><li>Cactus provides ”redirectors”, which serve as points of entry to the container. </li></ul><ul><li>The redirectors execute test cases written for Cactus in the container and provide access to container objects (HttpServletRequest, PageContext etc) </li></ul><ul><li>Cactus integrates with JUnit </li></ul>
  18. 18. Example servlet Public class LiUServlet extends javax.servlet.http.HttpServlet { Public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { Response.setContentType(”text/html”); PrintWriter writer=response.getWriter(); Writer.println(”<a href=’’>”); Writer.println(”Linköpings universitet</a>”); }}
  19. 19. Cactus Private LiUServlet servlet; Public void setUp() throws Exception { servlet = new LiUServlet(); servlet.init(config); } /** Test of doGet method **/ public void testDoGet() throws Exception { servlet.doGet(request, response); }
  20. 20. Cactus Public void endDoGet(com.meterware.httpunit.WebResponse response) throws Exception { WebLink link = response.getLinkWith(” Linköpings universitet”); assertNotNull(link); System.out.println(response.getText()); org.w3c.dom.Node = response.getDOM(); }
  21. 21. Canoo <ul><li>Canoo is a web application testing suite. </li></ul><ul><li>Canoo is not restricted to only testing Java applications </li></ul><ul><li>Uses other Open Source packages (JUnit, JTidy) </li></ul><ul><li>Fairly easy to use, can the customer write tests? </li></ul>
  22. 22. Canoo <ul><li>As usual a project tag </li></ul><ul><li><project name=&quot;anstp&quot; basedir=&quot;.&quot; default=&quot;test&quot;> </li></ul><ul><li>Properties, esp canoo.dir </li></ul><ul><li><property name=&quot;base.dir&quot; value=&quot;${basedir}&quot;/> </li></ul><ul><li><property name=&quot;canoo.dir&quot; value=&quot;/service/canoo&quot;/> </li></ul><ul><li><property file=&quot;${basedir}/../../../tomcat-src/; /> </li></ul><ul><li><property file=&quot;${basedir}/../../../tomcat-src/; /> </li></ul>
  23. 23. Canoo <ul><li>The following target is needed to call canoo: </li></ul><ul><li><taskdef name=&quot;testSpec&quot; classname=&quot;com.canoo.webtest.ant.TestSpecificationTask&quot;> </li></ul><ul><li><classpath> </li></ul><ul><li><fileset dir=&quot;${canoo.dir}“ includes = &quot;**/lib/*.jar&quot;/> </li></ul><ul><li></classpath> </li></ul><ul><li></taskdef> </li></ul>
  24. 24. Canoo <ul><li>Lastly the target for the testing itself </li></ul><ul><li><target name=&quot;test&quot; > </li></ul><ul><li>Detta target ska innehålla tasken testSpec (med ett eventuellt name): </li></ul><ul><li><testSpec name=“Template for Canoo tests&quot;> </li></ul><ul><li>In testSpec you need some config parameters: </li></ul><ul><li><config host=&quot;${SERVER_HOST}&quot; port=&quot;${HTTP11_PORT}&quot; protocol=&quot;${SERVER_PROTOCOL}&quot; basepath=&quot;${SERVER_BASEPATH}&quot; verbose=&quot;${webtest.verbose}&quot; resultpath=&quot;${base.dir}&quot; resultfile=&quot;${webtest.resultfile}&quot; showhtmlparseroutput=&quot;${webtest.showhtmlparseroutput}&quot; summary=&quot;${webtest.summary}&quot; saveresponse=&quot;${webtest.saveresponse}&quot; /> </li></ul>
  25. 25. Canoo <ul><li>A suitable sequence of test steps </li></ul><ul><li><steps> <!– test steps below --> </li></ul><ul><li><invoke stepid=“Fetch start page&quot; url=&quot;render.uP&quot; /> </li></ul><ul><li><verifytitle stepid=“Verify page title&quot; text=&quot;Mitt LiU&quot; /> </li></ul><ul><li><setinputfield stepid=“Set username&quot; name=&quot;userName&quot; value=&quot;xgugro828&quot; /> </li></ul><ul><li><setinputfield stepid=“Set password&quot; name=&quot;password&quot; value=&quot;kisen&quot; /> </li></ul><ul><li><clickbutton stepid=“Press login button&quot; name=&quot;Login&quot; /> </li></ul><ul><li><verifytext stepid=“Verify that we came to Gusten Grodslukares start page in the portal&quot; text=&quot;/Gusten Grodslukare&quot; /> </li></ul><ul><li><!– Test steps above --> </li></ul><ul><li></steps> </li></ul>
  26. 26. Canoo <ul><li>Closing tags: </li></ul><ul><li></testSpec> </li></ul><ul><li></target> </li></ul><ul><li></project> </li></ul>
  27. 27. HttpUnit <ul><li>HttpUnit provides a Java framework for functional testing. </li></ul><ul><li>HttpUnit is a solid ground to build your tests on. </li></ul><ul><li>But it might comes with the price of duplication. </li></ul>
  28. 28. HttpUnit <ul><li>WebResponse response = converstion.getResponse( ); </li></ul><ul><li>WebLink link = response.getLinkWith(” Linköpings universitet”); </li></ul><ul><li>WebRequest request = link.getRequest(); </li></ul><ul><li>response = conversion.getResponse(request); </li></ul>
  29. 29. Performance <ul><li>Performance: Number of simultaneous users, size of web pages returned... </li></ul><ul><li>“ the user experience” – It is the end users experience that are key to the application performance. </li></ul><ul><li>Locate the bottlenecks as early as possible – refactor and re-architect </li></ul>
  30. 30. JMeter <ul><li>JMeter a 100% pure java desktop application. </li></ul><ul><li>Test: HTTP, FTP, RDBMS… </li></ul><ul><li>Extensible… write your own test </li></ul><ul><li>Simulate heavy load (application, server and network) </li></ul><ul><li>Gives instant visual feedback </li></ul>
  31. 31. JMeter <ul><li>You must have a TestPlan </li></ul><ul><li>A TestPlan consists of one to many ThreadGroups. </li></ul><ul><li>A thread can be seen as a simulated user </li></ul><ul><li>A timer is added to a ThreadGroup (Constant, gaussian random, uniform random) </li></ul><ul><li>Controller </li></ul><ul><li>Listener </li></ul>
  32. 32. Bugzilla <ul><li> </li></ul><ul><li>A bug-tracking tool, since naturally there will be bugs and there is an actual need to follow progress of the software. </li></ul><ul><li>Consider adding bugs automatically… </li></ul>
  33. 33. Bugzilla
  34. 34. Links <ul><li>JUnit – http:// junit .org </li></ul><ul><li>Canoo – http:// webtest . canoo .com </li></ul><ul><li>HttpUnit - http:// httpunit . sourceforge .net </li></ul><ul><li>Bugzilla – http://www. bugzilla .org </li></ul><ul><li>Ant, Cactus, JMeter - http:// jakarta </li></ul>
  35. 35. Summary <ul><li>Automate the testing! </li></ul><ul><li>Testing is not rocket science </li></ul><ul><li>Include testing from the beginning </li></ul><ul><li>Use the web </li></ul>
  36. 36. Testing Testing Testing <ul><li>Q & A </li></ul><ul><li>Joakim Björklund </li></ul><ul><li>[email_address] </li></ul>