Scryent: Plone - Hone Your Test Fu


Published on

Learn about the basics of test-driven development using Plone. Presented by Jordan Baker from Scryent at the Plone Symposium East 2009.

Published in: Technology, Education
1 Like
  • Be the first to comment

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

No notes for slide

Scryent: Plone - Hone Your Test Fu

  1. 1. <ul><li>Hone Your TestFu </li></ul><ul><li>A Test-First Approach </li></ul><ul><li>Presented By: Jordan Baker, </li></ul><ul><li>Plone Symposium East, Penn State 2009 </li></ul><ul><li>Image CC Encyclopedia Britannica </li></ul>
  2. 2. Why test? <ul><li>prevents software regressions </li></ul><ul><li>improves code quality </li></ul><ul><li>provides the best developer documentation </li></ul><ul><li>tests passing is proof that you delivered what you were contracted to produce </li></ul><ul><li>business measurement help prevent scope creep </li></ul><ul><li>finding bugs early makes them cheaper to fix </li></ul>
  3. 3. Test First! <ul><li>writing tests first forces you to get clear about requirements up front </li></ul><ul><li>generally results in more modular and reusable code </li></ul><ul><li>facilitates a conversation with your product owners </li></ul><ul><li>manual testing is slow and error-prone! The longer you put off writing tests the less efficient you are. </li></ul>
  4. 4. Test-Code Cycle
  5. 5. Testing Pyramid UI / acceptance functional / integration unit
  6. 6. Unit Tests <ul><li>fast </li></ul><ul><li>tests implementation of a sliver of behaviour </li></ul><ul><li>doesn't rely on any external resources (so use mocks or fakes for external resources) </li></ul><ul><li>lots of these </li></ul><ul><li>use unittest or doctest modules for this </li></ul>
  7. 7. Integration / Functional Tests <ul><li>test the integration of various components and the flow in and out of your code </li></ul><ul><li>use PloneTestCase </li></ul>
  8. 8. UI Tests <ul><li>End to end tests </li></ul><ul><li>Typically black-box tests that act as an end-user would </li></ul><ul><li>inherently brittle as the UI often changes </li></ul><ul><li>zope.testbrowser, selenium, windmill, etc </li></ul>
  9. 9. Python and Plone testing frameworks + tools <ul><li>Python unittest module </li></ul><ul><li>Plone Test Case </li></ul><ul><li>plone.reload </li></ul><ul><li>roadrunner </li></ul><ul><li>Mocker </li></ul><ul><li>zope.testbrowser </li></ul>
  10. 10. Requirements <ul><li>User can visit the demo page and type in their name </li></ul><ul><li>The user will receive a greeting in the format &quot;Hello, <name>&quot; </li></ul>
  11. 11. Where to start? <ul><li>Time to write your first test. </li></ul><ul><li>There are some excellent docs on working with Plone from Python: </li></ul><ul><li>Take your time, write out what you’re trying to in English first before coding. </li></ul>
  12. 12. Writing your first test <ul><li>Let’s write a unit test for this feature: The user will receive a greeting in the format &quot;Hello, <name>&quot; </li></ul><ul><li>To unit test this we will instantiate the View class which represents the data for this page, pass the username and check the result. </li></ul>
  13. 13. Using Zope's testtrunner <ul><li>TIP: run only the test you need to run $ bin/instance test -s your.module -t test_something </li></ul><ul><li>By using -s you can specify the module to look for tests. </li></ul><ul><li>If you want to run only one particular test method then use -t to specify which one. </li></ul><ul><li>In our case the command will be: $ bin/instance test -s pse09.demo </li></ul>
  14. 14. Make it faster... enter roadrunner <ul><li>Roadrunner is a tool that preloads Plone to make running your integration tests much much faster. </li></ul><ul><li>Its easy to use too. Just add roadrunner to your buildout.cfg like so: </li></ul><ul><li>Roadrunner accepts the same command line arguments as Zope testrunner. So we can run our tests like this: $ bin/roadrunner -s pse.demo </li></ul>
  15. 15. Pdb-fu (debugging) <ul><li>Getting test failures from Zope Testrunner? Use -D to drop you into Pdb using post-mortem debugging </li></ul><ul><li>Use pdb’s jump command to re-run certain lines of codes. Watch out for side-effects. </li></ul><ul><li>Special variable __return__ is set. Use “continue” to go to the end of the method and then you can easily examine the return result. </li></ul><ul><li>Breakpoint variables. Looping through 1000 items, set a breakpoint with an expression. </li></ul><ul><li>Missing source code? This could help, add to your buildout.cfg: [buildout] unzip = true </li></ul>
  16. 16. Additional Requirements <ul><li>If a user named “Plone” submits their name they will see the message “You ROCK, Plone” </li></ul><ul><li>Use plone.reload to try out these changes live, writing more tests not necessary. </li></ul>
  17. 17. using zope.testbrowser <ul><li>Test something like so: </li></ul><ul><li>Go to the page @@greet </li></ul><ul><li>Type in your name: Bob </li></ul><ul><li>Expect to see “Hello, Bob” </li></ul>
  18. 18. Don’t Test Too Much <ul><li>Tests are software too and must be maintained. </li></ul><ul><li>Too many tests makes running your entire test suite slow. </li></ul><ul><li>Remember the pyramid. Just like the food pyramid you want to focus on having lots of tests from the lower levels which run quickly, and fewer from the top of the pyramid. </li></ul><ul><li>Some people are opportunistic testers . They will write a minimal test for the main path of their code and then only write additional tests if regressions are found. </li></ul>
  19. 19. Some Resources <ul><li>Plone Testing tutorial: </li></ul><ul><li>roadrunner: </li></ul><ul><li>plone.reload: </li></ul><ul><li>zope.testbrowser: </li></ul>
  20. 20. Thanks <ul><li>Your Feedback is appreciated: [email_address] </li></ul><ul><li>Good luck and happy testing! </li></ul>