javascript unit testing

74
JavaScript Unit Testing Tools of the trade

Upload: christian-johansen

Post on 21-Aug-2015

3.580 views

Category:

Documents


2 download

TRANSCRIPT

JavaScript Unit Testing

Tools of the trade

Christian Johansen

http://cjohansen.no/enhttp://twitter.com/cjno

http://gitorious.org/~cjohansen

http://github.com/cjohansen

Table of ContentsWhy unit test JavaScript?

How to unit test JavaScript?

Brief example of unit testing chat client

Why unit test JavaScript?

Debugging

console.log

F5, F5, F5

Which version?•IE: 6, 7, 8 ?•Firefox: 3.0, 3.5, 3.6, 3.7 ?•Safari: 4, 3.1?•Opera 10.50, 10.10, 10.0?

What about mobile?

Manual testing

It doesn't scale

Manual testing

Non reproducible

Manual testing

It's time consuming

Automated unit tests

Unit testsRun now, run later

Run in new browsers

Put the test in a file rather than Firebug

How to unit test JavaScript?

In-browser test frameworks

Headless test frameworks

Hybrids

In-Browser Frameworks

YUI TestPart of the YUI Framework

Can test any code

Typical in-browser test framework

http://developer.yahoo.com/yui/3/test/

YUI Test 2

YUI Test 3

HTML Fixture loads test

Code To Test: strftime

strftime Overview

Date formats

Unit Test (YUI 3)

The Good PartsRun in any browser

Simple to get started(don't even have to download YUI)

Many high-level assertions

The Bad PartsRequires HTML fixture

Requires internet connection(can be fixed)

Does not solve the scaling problem

Similar offeringsQUnithttp://docs.jquery.com/QUnit

JsUnitTest (Prototype.js)http://jsunittest.com/

JsUnit (Since 2001)http://jsunit.net/

Headless frameworks

JSpecBDD style testing framework

Custom grammar(Ruby-like, optional)

Run in browser, or headlessly

http://visionmedia.github.com/jspec/

HTML Fixture

JSpec Spec

Run From Command Line

Result

Headless Run

The Good PartsRich library of matchers

Runs in browsers, Rhino, Node.js

More Good PartsCommand Line Tool

•Initialize projects•Run tests•Install libraries•+++

JSpec GoodnessBuilt-in stub and mock API

Built-in mock XMLHttpRequest

Built-in mock timers

The Bad PartsRhino + env.js = Fully emulated

Browser launching still manual

Ruby-like DSL (although optional)

Hybrids

JsTestDriver

The JsTestDriver WayTests execute in browsers

Browsers listen for tests

Browsers can run locally or remote

Execute in multiple browsers simultaneously

Running Tests

Issue test runs from command line...or from IDE (Eclipse + Idea)

View results in command line, IDE

JsTestDriver Server

java -jar JsTestDriver-1.2.jar --port 4224

JsTestDriver Config

JsTestDriver Test

Run From Command Line

100% More Color

Uh-oh!

Run in Eclipse

The Good Parts

Scales!

Test any platform from your machine

FAST

More Good PartsCan integrate runner with other assertion frameworks (QUnit, YIU Test++)

Coverage tool

JUnit compatible XML output

Continuous Integration

JUnit XML Output

Hudson

Mocks and Stubs

Working on it!

http://gitorious.org/sinon

Quick Live Demo

Want More?

Out Fall 2010

Rough cuts available: http://my.safaribooksonline.com/97803216

84097

That's it!

http://cjohansen.no/enhttp://twitter.com/cjno

http://gitorious.org/~cjohansen

http://github.com/cjohansen