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

System Testing an API with NodeJS and Mocha

System Testing an API with NodeJS and Mocha

Do you test as much as you think you should? Or, even if you write tests, are you still confused about the different types of tests, how to structure and name your tests, or even what to test in the first place?

In this talk, Daniel Bostwick will walk through how he structures a NodeJS project for testing, integrates the Mocha and Chai testing frameworks, discuss the guidelines he uses for figuring out what to test, and show an examples of how to functionally test a backend API. You'll come away with a better understanding of BDD, learn how to functionally test any api, and be inspired to better test your own APIs.

Daniel Bostwick

September 24, 2015
Tweet

Other Decks in Programming

Transcript

  1. 6 You Need To Test Testing is not a requirement

    for a successful* project. *depending on your definition of success.
  2. 8 You need 100% test coverage 100% Coverage is a

    great ideal, but any coverage will help improve a project.
  3. 10 Testing proves your program is correct or free of

    bugs Unless you’re using formal verification systems, testing can prove nothing.
  4. 12 Writing tests means you can “grow” your architecture over

    time, and not make decisions up front If you don’t know how to implement something to start, following TDD will not help you figure it out. http://ravimohan.blogspot.com/2007/04/learning-from-sudoku-solvers.html
  5. 17 A test ensures that a system behavior remains constant

    even as the underlying implementation may change.
  6. “If builders built buildings the way programmers wrote programs, then

    the first woodpecker that came along would destroy civilization.” 
 - Gerald Weinberg 18 Testing inspires confidence
  7. 20 It’s not about Code Structure “use strict”
 
 module.exports

    = {
 sayHello: function(name) {
 return “Hello, “ + name;
 }
 };
 
 
 if (require.main === module) {
 console.log(sayHello(“World”));
 }
  8. 21 It’s about Behavior. When I run hello.js, I want

    to print “Hello, World.” so I can have an example for this presentation .
  9. 22 Behavior The way in which a system acts in

    response to a particular situation or input.
  10. 24 Behavior is independent of implementation. # This passes, too!


    “use strict”
 console.log(“Hello, World”);
  11. 28 Naming Tests How it’s taught. “use strict”
 
 module.exports

    = {
 sayHello: function(name) {
 return “Hello, “ + name;
 }
 }; “use strict”
 
 var myLib = require(“hello-lib”);
 
 function testSayHello() {
 assert(myLib.sayHello(“World”) ===
 “Hello, World”);
 }
  12. 31 Spec-Based Testing define(“sayHello”, function() { 
 define(“given a name”,

    function() {
 it(“returns ‘Hello, {Name}’”, …); }); 
 define(“given a number”, function() {
 it(“returns ‘Hello, {Number}’”, …); }); });
  13. 34 Use Cases for API System Tests Building your own

    API • Write your API documentation as executable tests. • Know when the API demonstrates the required behavior for implementation. • Provides a “definition of done.” Integration with a 3rd-Party API • Verify the 3rd-Party API works as the vendor says it should. • Expose and document any inconsistencies between Vendor’s docs and the actual behavior.
  14. 35 Tools 1. NodeJS 2. Mocha & Chai
 http://mochajs.org/, http://chaijs.com/

    3. request
 https://github.com/request/request
 4. Bluebird Promises
 https://github.com/petkaantonov/bluebird
  15. 36 Project Structure project-name/ [bin]/ [config]/ src/ or lib/ project-name/

    test/
 project-name/ helpers.js
 Readme.md package.json
  16. 38 Running the Tests $ # From the project dir

    $ ./node_modules/.bin/mocha test/**/* # Put this in your package.json:
 “scripts”: { “test”: “./node_modules/.bin/mocha test/**/*” } $ npm test
  17. 40 Climb Higher API GET, PUT /user POST /users GET,

    POST /sessions GET, PUT, DELETE /sessions/:sessionId POST /sessions/:sessionId/ticks DELETE /sessions/:sessionId/ticks/:tickId
  18. 46 Review • Testing is not a magic silver bullet.

    • Testing gives you confidence about behavior. • Use Job Stories to define behavior. • Name your tests based on a system’s behavior. • It’s easy to test both your own code and other’s with System tests. • Mocha and BDD give you readable, executable documentation.