Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing Pyramid applications

Testing Pyramid applications

Talk at Pyramid London about testing Pyramid applications. Doctest, unit testing, integration testing and functional testing all make an appearance.

James Cooke

June 04, 2013
Tweet

More Decks by James Cooke

Other Decks in Programming

Transcript

  1. Pyramid layout YourApp View YourApp Code: Models Forms etc Templating

    / render DB FS Events Routing / View Lookup WSGI App View callable
  2. doctest • Good for: ◦ Models ◦ Form schemas ◦

    Basic app logic • Modules: ◦ from mock import Mock ◦ SQLAlchemy sessions ◦ from pyramid.testing import DummyRequest
  3. doctest Pyramid layout YourApp View YourApp Code: Models Forms etc

    Templating / render DB FS Events Routing / View Lookup WSGI App View callable Now with doctest request response
  4. unit test • Good for: ◦ Basic views ◦ Models

    ◦ Basic app logic • Warning signs: ◦ Pyramid events ◦ View needs specific code to be tested
  5. unit test doctest Pyramid layout YourApp View YourApp Code: Models

    Forms etc Templating / render DB FS Events Routing / View Lookup WSGI App View callable Now with doctest and unitest
  6. integration test • Good for: ◦ Views - request in,

    response out ◦ Events! ◦ Real app logic • Warning signs: ◦ Monolithic tests ◦ Repeating code
  7. integration test unit test doctest Pyramid layout YourApp View YourApp

    Code: Models Forms etc Templating / render DB FS Events Routing / View Lookup WSGI App View callable Now with doctest, unitest, integration test
  8. unit / integration tips • Build 'good request' • Make

    sure events are wired in • Use real database and sessions • Context might not be None
  9. wish 1: better permission testing from pyramid.httpexceptions import HTTPForbidden from

    my.package import view_fn self.config.testing_securitypolicy(userid='hank', permissive=False) request = testing.DummyRequest() request.context = testing.DummyResource() self.assertRaises(HTTPForbidden, view_fn, request)
  10. wish 2: Better SQLAlchemy tm transaction.manager wrap on views to

    write data when call is finished. DetachedInstanceError: Parent instance <User at 0x3959310> is not bound to a Session; lazy load operation of attribute 'roles' cannot proceed
  11. wish 3: Routing - how to test?! • Order of

    calls to pyramid.config. Configurator.add_route() matter • This should be programmatically testable! • Currently request.route_url() and check result.
  12. functional test routing test integration test unit test doctest Pyramid

    layout YourApp View YourApp Code: Models Forms etc Templating / render DB FS Events Routing / View Lookup WSGI App View callable
  13. functional tips • Use BDD (behaviour driven development) • behave

    seems like a good library • Mako rendering exceptions can die
  14. functional test routing test integration test unit test doctest Pyramid

    testing 100% YourApp View YourApp Code: Models Forms etc Templating / render DB FS Events Routing / View Lookup WSGI App View callable