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.

B3c6f332ab773b78f4ebd72d56d7b6df?s=128

James Cooke

June 04, 2013
Tweet

Transcript

  1. Testing Pyramid applications Pyramid London // James Cooke

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

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

    Basic app logic • Modules: ◦ from mock import Mock ◦ SQLAlchemy sessions ◦ from pyramid.testing import DummyRequest
  4. 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
  5. unit test • Good for: ◦ Basic views ◦ Models

    ◦ Basic app logic • Warning signs: ◦ Pyramid events ◦ View needs specific code to be tested
  6. 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
  7. integration test • Good for: ◦ Views - request in,

    response out ◦ Events! ◦ Real app logic • Warning signs: ◦ Monolithic tests ◦ Repeating code
  8. 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
  9. unit / integration tips • Build 'good request' • Make

    sure events are wired in • Use real database and sessions • Context might not be None
  10. 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)
  11. 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
  12. 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.
  13. 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
  14. functional tips • Use BDD (behaviour driven development) • behave

    seems like a good library • Mako rendering exceptions can die
  15. 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
  16. thanks for listening • Any questions? • @jamesfublo