$30 off During Our Annual Pro Sale. View Details »

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. Testing Pyramid
    applications
    Pyramid London // James Cooke

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  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)

    View Slide

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

    View Slide

  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.

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

  16. thanks for listening
    ● Any questions?
    ● @jamesfublo

    View Slide