76
Adventures in JavaScript testing Thomas Fuchs

Thomas Fuchs Presentation

Embed Size (px)

Citation preview

Page 1: Thomas Fuchs Presentation

Adventures inJavaScript testing

Thomas Fuchs

Page 2: Thomas Fuchs Presentation

Thomas Fuchs

wollzelle

script.aculo.usmir.aculo.us

Page 3: Thomas Fuchs Presentation

traditionaljavascript „testing“

Page 4: Thomas Fuchs Presentation
Page 6: Thomas Fuchs Presentation

alert(“please use me only where appropriate!”);

Page 7: Thomas Fuchs Presentation

why?

Page 8: Thomas Fuchs Presentation

"JavaScript is this thing for funny mouse trails, right?"

Page 9: Thomas Fuchs Presentation

"BUT You CAN't DEBUG JavaScript!"

Page 10: Thomas Fuchs Presentation

"WE DON'T NEED NO STINKIN' TESTS

FOR TEN LINES OF CODE!"

Page 11: Thomas Fuchs Presentation

"ANYWAY, THERE IS NO WAY TO

AUTOMATICALLY TEST JAVASCRIPT!"

Page 12: Thomas Fuchs Presentation

all of this is pure FUD

Page 13: Thomas Fuchs Presentation

unit testing javascript

Page 14: Thomas Fuchs Presentation
Page 15: Thomas Fuchs Presentation

Borrows from

Test::Unit

Page 16: Thomas Fuchs Presentation

include unittest.js(requires Prototype)

Page 17: Thomas Fuchs Presentation

a DIV is required for containing the test results

(defaults to "testlog")

Page 18: Thomas Fuchs Presentation

create a new instance ofTest.Unit.Runner…

Page 19: Thomas Fuchs Presentation

…this will excute all methods starting with

"test"

Page 20: Thomas Fuchs Presentation

assert away!

Page 21: Thomas Fuchs Presentation

(syntax strangenessfor more convenience

when calling assertions)

Page 22: Thomas Fuchs Presentation

assert(expression)

assert(true) => PASSassert(false) => FAILassert(2*2==4) => PASS

Page 23: Thomas Fuchs Presentation

assertEqual(expected, actual)

assertEqual(‘a’,’a’) => PASSassertEqual(‘a’,’b’) => FAILassertEqual(1,1) => PASSassertEqual(1,’1’) => PASS

does not compare type

Page 24: Thomas Fuchs Presentation

assertEnumEqual(expected, actual)

assertEnumEqual([1,2],[1,2]) => PASS

assertEqual does not compare enum contents

assertEqual([1,2],[1,2]) => FAIL

Page 25: Thomas Fuchs Presentation

assertNotEqual(expected, actual)

assertNotEqual(‘a’,’a’) => FAILassertNotEqual(‘a’,’b’) => PASSassertNotEqual(1,1) => FAILassertNotEqual(1,’1’) => FAIL

does not compare type

Page 26: Thomas Fuchs Presentation

assertMatch(expected, actual)

assertMatch(/a/,’a’) => PASSassertMatch(/a/,’bab’) => PASSassertMatch(/a/,’b’) => FAIL

assertMatch(/^moo$/,’moo’) => PASS

Page 27: Thomas Fuchs Presentation

assertIdentical(expected, actual)

assertIdentical(‘a’,’a’) => PASSassertIdentical(1,’1’) => FAIL

compares type

Page 28: Thomas Fuchs Presentation

assertNotIdentical(expected, actual)

assertNotIdentical(‘a’,’a’) => FAILassertNotIdentical(1,’1’) => PASS

compares type

Page 29: Thomas Fuchs Presentation

assertType(expected, actual)

assertType(String,’a’) => PASSassertType(Number,1) => PASS

assertType(Array,[1,2]) => PASS

checks for a specific constructor

Page 30: Thomas Fuchs Presentation

assertRaise(exceptionName, method)

assertRaise(‘ElementDoesNotExistError’, function(){ new Effect.Opacity(‘invalid-element’) });

related effects.js code

Page 31: Thomas Fuchs Presentation

assertRespondsTo(method, obj)

Page 32: Thomas Fuchs Presentation

assertVisible(element)

assertNotVisible(element)

also checks if any parent elements are hidden and

fails/passes on that

Page 33: Thomas Fuchs Presentation

info(message)

Displays arbitrary messages in the "Message" column of the test result

Page 34: Thomas Fuchs Presentation

assertXYZ(params, message)

Page 35: Thomas Fuchs Presentation

"WELL, WHAT ABOUTFUNCTIONS THAT

RUN ASYNCHRONOUSLY?"

Page 36: Thomas Fuchs Presentation

wait(milliseconds, method)

should be last statement in the test (but can be nested)

Page 37: Thomas Fuchs Presentation

Benchmarking

Page 38: Thomas Fuchs Presentation

"WELL, NICE. BUT IT'S TEDIOUS TO RUN ALL THOSE

TESTS MANUALLY!"

Page 39: Thomas Fuchs Presentation

rake to the rescue

Page 40: Thomas Fuchs Presentation

rake test:javascripts

Page 41: Thomas Fuchs Presentation
Page 42: Thomas Fuchs Presentation
Page 43: Thomas Fuchs Presentation

javascript_test plugin

Page 44: Thomas Fuchs Presentation

WEBrick

rake test:javascripts

2. controls browsers

1. launchesweb server

3. run tests

lists resultsSUCCESSFAILUREERROR

Page 45: Thomas Fuchs Presentation

$ script/generate javascript_test muhaha exists test/javascript create test/javascript/muhaha_test.html

Page 46: Thomas Fuchs Presentation

justadd your

tests

Page 47: Thomas Fuchs Presentation

(symlink required:app/test/javascript/assets to

app/vendor/plugins/javascript_test/assets)

Page 48: Thomas Fuchs Presentation

Browser definitions(add more!)

Page 49: Thomas Fuchs Presentation

$ script/plugin install http://dev.rubyonrails.org/svn/rails/plugins/javascript_test

Page 50: Thomas Fuchs Presentation

…and what about BDD?

Page 52: Thomas Fuchs Presentation

Borrows from

RSpec

Page 53: Thomas Fuchs Presentation

specifications

Page 54: Thomas Fuchs Presentation

add BDD methodsto any object

Page 55: Thomas Fuchs Presentation

for isSomething() methods, that

return a boolean

Page 56: Thomas Fuchs Presentation

should* assertations,directly on objects

Page 57: Thomas Fuchs Presentation

("with" ugliness no longerrequired)

Page 58: Thomas Fuchs Presentation
Page 59: Thomas Fuchs Presentation

Mapping

Page 60: Thomas Fuchs Presentation

Mixed environment

can also use"normal" assertations

Page 61: Thomas Fuchs Presentation

…and if something breaks?

Page 62: Thomas Fuchs Presentation

http://www.flickr.com/photos/teagrrl/78941282/

Firebug sliced bread

Page 63: Thomas Fuchs Presentation

Firebug

Debugger

Page 64: Thomas Fuchs Presentation

Firebug

knowyourAjax

Page 65: Thomas Fuchs Presentation

Venkman JavaScript Debugger

Comes with

Profiling!

Page 66: Thomas Fuchs Presentation

Safari Web Inspector

Page 67: Thomas Fuchs Presentation

Safari Debugging with Drosera

Page 68: Thomas Fuchs Presentation

+

http://webkit.org/

Page 69: Thomas Fuchs Presentation

Microsoft Script Debugger for IE

http://blogs.msdn.com/ie/archive/2004/10/26/247912.aspx

Page 70: Thomas Fuchs Presentation

Test with all browsers

you want to support

Page 71: Thomas Fuchs Presentation
Page 72: Thomas Fuchs Presentation

get a setup that doesn‘t suck

Page 73: Thomas Fuchs Presentation

Source: http://www.flickr.com/photos/icathing/26603225/

Page 74: Thomas Fuchs Presentation

+ +

(+ )

Page 75: Thomas Fuchs Presentation

one machine to rule them all

Page 76: Thomas Fuchs Presentation

help and ideas welcome

#prototype (freenode)and

Rails Spinoffs Google Group