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

API Testing

Cymen Vig
February 19, 2014

API Testing

Cymen Vig

February 19, 2014
Tweet

More Decks by Cymen Vig

Other Decks in Programming

Transcript

  1. overview • what is an API • testing API •

    tests as documentation • tools — Apiary.io and literate-jasmine • live example — github.com/cymen/apiary-todo • going forward — Bob, Brian, platform team and you!
  2. Application Programming Interface • “how components should interact with each

    other” • between the consumer (JavaScript in browser) and the provider (Java on server) • convention • assumptions • risky — we should test!
  3. How to test APIs? • tests that run on continuous

    integration (CI) server • tests that developers can run
  4. More than tests? • documentation — both internal and external

    • proxy requests via service that can compare expected exchange versus actual exchange (more later)
  5. Tests as documentation • markdown is a simple syntax that

    can be transformed easily to HTML • typically, files have .md or .markdown extension • look at blueprint.md and api.md on Github (automatic simple transformation)
  6. Two tools • Apiary.io — API-specific testing tool currently focused

    100% on the interface • literate-jasmine — developed by us (platform), open source, covers gaps in Apiary.io
  7. Apiary.io gaps • remember interface? in a JSON exchange between

    client and service, it is purely focused on the keys not the values • cannot test values • cannot have variables (for example, create a record then use the id from the record you just created to some operation on it — maybe delete it)
  8. What do I mean about keys? GET /nodes/20 {
  

    id:  20,     someKey:  value     complex:  {       key:  something     }   } client server no, only “root” keys
  9. Apiary.io parts
 (github.com/apiaryio) • blueprint is key piece • document

    written by us in markdown • open standard • tools (operate on blueprint) • dredd - run on CI against API, open source • libraries — parsing, code sample generator, validator, etc…
  10. Apiary.io service • API proxy • allows inspecting communication taking

    place via API • allows validating API requests and responses against the blueprint • slick documentation (code samples)
  11. literate-jasmine • markdown document written by us • jasmine tests

    • why? • can use variables • can test values • can test API as we want to test it — Apiary.io adding functionality but…
  12. Apiary.io vs literate-jasmine • markdown • blueprint code • focused

    on interface but… • try to use first and do document DELETE with it… • markdown • Jasmine/JavaScript • open focus so free to test as desired • fall back to when need variables, test values or do multiple operations on record
  13. Running API tests on production or shared QA environments… •

    potential for deleting records completely unrelated to the snapshot being tested (permissions of test account) • document DELETE in Apiary.io • dredd has command line option to opt-in to specific HTTP verbs (GET, PUT, POST, DELETE, etc) • literate-jasmine for full Create Read Update Delete (CRUD) tests
  14. literate-jasmine and CRUD 1. create a record 2. read record

    using record id from 1,
 validate full response including known values 3. update record using id from 1 4. delete record using id from 1 variable! value! var var
  15. example: todo API • github.com/cymen/apiary-todo • clone and “npm install”

    • start server with “node server.js” • simple todo server where you can create (POST), read (GET) and delete (DELETE)
  16. example & Apiary.io • npm install -g dredd • dredd

    blueprint.md http://localhost:3000/ • can limit HTTP verb with one or more “-­‐-­‐method   VERB” so “-­‐-­‐method  GET” to only perform GET requests…
  17. Going forward… • Test APIs • Document APIs • We

    may find service like Apiary.io useful for outside developers… • Refactor APIs • correct response types • login via API • better resource locaters that for example might include snapshot id being operated on when editing an LPN node… • Need you to be onboard with these changes (cue Bob? cue Brian?)
  18. Sometimes even the wisest of men and machines can be

    in error!
 " Transform API!
 
 Roll out!