Slide 1

Slide 1 text

Testing Pyramid applications Pyramid London // James Cooke

Slide 2

Slide 2 text

Pyramid layout YourApp View YourApp Code: Models Forms etc Templating / render DB FS Events Routing / View Lookup WSGI App View callable

Slide 3

Slide 3 text

doctest ● Good for: ○ Models ○ Form schemas ○ Basic app logic ● Modules: ○ from mock import Mock ○ SQLAlchemy sessions ○ from pyramid.testing import DummyRequest

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

unit test ● Good for: ○ Basic views ○ Models ○ Basic app logic ● Warning signs: ○ Pyramid events ○ View needs specific code to be tested

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

integration test ● Good for: ○ Views - request in, response out ○ Events! ○ Real app logic ● Warning signs: ○ Monolithic tests ○ Repeating code

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

unit / integration tips ● Build 'good request' ● Make sure events are wired in ● Use real database and sessions ● Context might not be None

Slide 10

Slide 10 text

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)

Slide 11

Slide 11 text

wish 2: Better SQLAlchemy tm transaction.manager wrap on views to write data when call is finished. DetachedInstanceError: Parent instance is not bound to a Session; lazy load operation of attribute 'roles' cannot proceed

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

functional tips ● Use BDD (behaviour driven development) ● behave seems like a good library ● Mako rendering exceptions can die

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

thanks for listening ● Any questions? ● @jamesfublo