midwestjs zero to testing

60
Zero to Testing in JavaScript Basics of testing, in JS

Upload: pamselle

Post on 15-May-2015

139 views

Category:

Technology


2 download

DESCRIPTION

Basics of testing, in JavaScript. A high level overview of testing tools, walking through a basic why and how of testing.

TRANSCRIPT

Page 1: MidwestJS Zero to Testing

Zero to Testing in JavaScriptBasics of testing, in JS

Page 2: MidwestJS Zero to Testing

About me

• Blog: thewebivore.com

• Twitter: @pamasaur

• Co-organizer of Philadelphia JavaScript Developers

• Podcasting on Turing Incomplete (@turingcool)

• Testing fanatic

@pamasaur | #midwestjs | thewebivore.com

Page 3: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Welcome to the testing track?

Page 4: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

*pause* … A TESTING TRACK!

Page 5: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Let’s kick this off right!

Page 6: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Agenda

• My testing story

• Testing theory

• Basic test walk-through

• Testing frameworks

• Other forms of testing

• Working testing into your workflow

• Minishop (workshop)

Page 7: MidwestJS Zero to Testing

My testing story

Page 8: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

When I first started writing tests

Code Retreat

• TDD

• Pairing

• Throw-away code

Page 9: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

What about tests in my projects?

Page 10: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

previously:

“Tests* are doing the dishes”* and writing docs and fixing bad code

Page 11: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

aka:

“Tests are work for people you don’t like”

Page 12: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

The other piece that I would add to this is, and I’ve said this before at talks, that testing isn’t something that you do because it is good to be testing. It’s not your lettuce. You do it because it increases your confidence in your code. And it increases your ability to release quickly and make changes without worrying that something is going to break. And so, if you’re paranoid like me, that’s a good incentive to do testing. And if you have a whole lot of self-confidence, maybe you’re just a really great coder. But maybe it’s a little bit more likely that you’re going to make mistakes and you might pay for it a little bit later. Julie Ralph, Protractor

http://javascriptjabber.com/106-jsj-protractor-with-julie-ralph/

Page 13: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Page 14: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

now:

GOOD developers write tests

Page 15: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Literally.

Page 16: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

-b for bad coder

Page 17: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

What do you call code without tests?

Legacy code.

Page 18: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Testing basics

1. When you break it, it breaks!

2. Secondary line of documentation defense

3. Design tool

Page 19: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

When you break it, it breaks!

• Testing is required to enable refactoring

• Regularly run tests

• Use test results to make decisions

Page 20: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Documentation defense

• Tests describe behavior

• Read tests to understand behavior

• Write tests to describe behavior

• Write tests to validate behavior

Page 21: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Tests as design tool

• Tests describe functionality

• Write functionality description (test)

• Write code to make it work

Page 22: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

BDD/TDD

• Behavior Driven Development

• Test-Driven Development

“Write tests to inform how you write your code”

Page 23: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

BDD

• describe

• it

• before

• after

• beforeEach

• afterEach

Page 24: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

TDD

• suite

• test

• setup

• teardown

Page 25: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Test Ever vs. Test Never

Page 26: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Types of testing

• Unit

• Integration

• Functional

• E2E

Page 27: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Unit

Testing functionality in isolation

“When I call addNumbers, it returns the value of the added numbers”

Page 28: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Unit testing takeaway:

Test your code

Page 29: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

How to only test your code?

• Isolate

• Faking

Page 30: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Fake things: Spies, stubs, and mocks

• Spy: an object that records its interactions

• Stubs: fake objects

• Mocks: fake objects with expected behavior

Generally, you can SPY on a function, STUB an object, and MOCK a service.

Page 31: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Integration

• Multiple pieces of code together

“Is the router setting up my models correctly?”

Page 32: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Functional

• Does your code work as product expects it to? (functional requirements)

“When I click the login button, it should log me in”

Page 33: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

E2E (end-to-end)

• Functional, but fully integrated with external services, simulate real-time situations.

“When I click ‘Login with Google,’ it logs me in”

Page 34: MidwestJS Zero to Testing

JavaScript test walk-through

Page 35: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

JavaScript test walk-through

• Setting up your base

• Writing the test/expectations*

• Making it pass*

* The order of these is debatable

Page 36: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Jasmine

• jasmine.github.io

• Download a standalone version on GitHub from pivotal/jasmine

• Node: jasmine-node (fork of karma-jasmine)

Page 37: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Mocha

• visionmedia.github.io/mocha

• Node!

• npm install –g mocha

Page 38: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Anatomy of a test

Describe [thing you’re testing]

It [does something you expect it to do]

Rinse and repeat.

Page 39: MidwestJS Zero to Testing

Example test walk-throughwith Mocha

Page 40: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

var assert = require('assert');

describe("An area of my application", function() {

it("should know that 2 and 2 is 4", function(){

assert.equal(4, 2+2);

});

});

Page 41: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

var assert = require('assert');

describe("An area of my application", function() {

it("should know that 2 and 2 is 4", function(){

assert.equal(4, 2+2);

});

});

Page 42: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

var assert = require('assert');

describe("An area of my application", function() {

it("should know that 2 and 2 is 4", function(){

assert.equal(4, 2+2);

});

});

Page 43: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

var assert = require('assert');

describe("An area of my application", function() {

it("should know that 2 and 2 is 4", function(){

assert.equal(4, 2+2);

});

});

Page 44: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

var assert = require('assert');

describe("An area of my application", function() {

it("should know that 2 and 2 is 4", function(){

assert.equal(2+2, 4);

});

});

Page 45: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Page 46: MidwestJS Zero to Testing

Testing Tools

Page 47: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Test describers

• Jasmine

• Mocha

• QUnit

• node-tap

• YUI Test

Page 49: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Spies, stubs, and mocks

• Sinon.js

• Jest from Facebook

Page 51: MidwestJS Zero to Testing

Bringing testing into the fold3 tips for making testing a regular part of your world

Page 52: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

#1: Teach testing

• Attend talks like this!

• Practice (ex. Code Retreat)

• Pair programming

Page 53: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

#2: Code coverage

• Istanbul

• Blanket.js

Page 54: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

#3: Code review

• Quality assurance

• Mentoring

• Don’t accept without tests!

Page 55: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

What’d we learn?

• Basics of testing

• Writing a JavaScript test

• Tools in JavaScript for testing

• Ways to create a testing culture

Page 56: MidwestJS Zero to Testing

Minishop(workshop, playshop …)

Page 57: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

TODO

• Set up karma (our test runner)

• With Jasmine (our assertion framework)

Page 58: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

TODO

• Write a failing test

• Correct the failing test

Page 59: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

TODO

• Write another test

• Setup in beforeEach

Page 60: MidwestJS Zero to Testing

@pamasaur | #midwestjs | thewebivore.com

Thank you!

• Find me online at• @pamasaur

• thewebivore.com (blog)

• turing.cool (podcast)

• thewebivore.com/book (book!)