javascript unittesting with js-test-driver

109
Javascript Unittesting with js-test-driver Jakob Westhoff <[email protected]> @jakobwesthoff PHPBarcamp.at May 2, 2010 http://westhoffswelt.de jakob@westhoffswelt.de slide: 1 / 31

Upload: westhoff

Post on 28-Jun-2015

5.881 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Javascript Unittesting with js-test-driver

Javascript Unittesting with js-test-driver

Jakob Westhoff <[email protected]>@jakobwesthoff

PHPBarcamp.atMay 2, 2010

http://westhoffswelt.de [email protected] slide: 1 / 31

Page 2: Javascript Unittesting with js-test-driver

About Me

Jakob Westhoff

PHP developer for several years

Computer science student at the TU Dortmund

Co-Founder of the PHP Usergroup Dortmund

Active in different Open Source projects

http://westhoffswelt.de [email protected] slide: 2 / 31

Page 3: Javascript Unittesting with js-test-driver

Who is actively using TDD/Unittesting?

Who uses unittesting?

Who uses TDD?

Who unittests javascript code?

http://westhoffswelt.de [email protected] slide: 3 / 31

Page 4: Javascript Unittesting with js-test-driver

Who is actively using TDD/Unittesting?

Who uses unittesting?

Who uses TDD?

Who unittests javascript code?

http://westhoffswelt.de [email protected] slide: 3 / 31

Page 5: Javascript Unittesting with js-test-driver

Who is actively using TDD/Unittesting?

Who uses unittesting?

Who uses TDD?

Who unittests javascript code?

http://westhoffswelt.de [email protected] slide: 3 / 31

Page 6: Javascript Unittesting with js-test-driver

Goals of this session

1 Getting to know js-test-driver

2 Unittesting javascript

Synchronous testsAsynchronous tests

3 Run these tests

From within a terminalFrom within Eclipse

4 Create javascript code coverage reports

http://westhoffswelt.de [email protected] slide: 4 / 31

Page 7: Javascript Unittesting with js-test-driver

Goals of this session

1 Getting to know js-test-driver

2 Unittesting javascript

Synchronous testsAsynchronous tests

3 Run these tests

From within a terminalFrom within Eclipse

4 Create javascript code coverage reports

http://westhoffswelt.de [email protected] slide: 4 / 31

Page 8: Javascript Unittesting with js-test-driver

Goals of this session

1 Getting to know js-test-driver

2 Unittesting javascript

Synchronous testsAsynchronous tests

3 Run these tests

From within a terminalFrom within Eclipse

4 Create javascript code coverage reports

http://westhoffswelt.de [email protected] slide: 4 / 31

Page 9: Javascript Unittesting with js-test-driver

Goals of this session

1 Getting to know js-test-driver

2 Unittesting javascript

Synchronous testsAsynchronous tests

3 Run these tests

From within a terminalFrom within Eclipse

4 Create javascript code coverage reports

http://westhoffswelt.de [email protected] slide: 4 / 31

Page 10: Javascript Unittesting with js-test-driver

Goals of this session

1 Getting to know js-test-driver

2 Unittesting javascript

Synchronous testsAsynchronous tests

3 Run these tests

From within a terminalFrom within Eclipse

4 Create javascript code coverage reports

http://westhoffswelt.de [email protected] slide: 4 / 31

Page 11: Javascript Unittesting with js-test-driver

Goals of this session

1 Getting to know js-test-driver

2 Unittesting javascript

Synchronous testsAsynchronous tests

3 Run these tests

From within a terminalFrom within Eclipse

4 Create javascript code coverage reports

http://westhoffswelt.de [email protected] slide: 4 / 31

Page 12: Javascript Unittesting with js-test-driver

Goals of this session

1 Getting to know js-test-driver

2 Unittesting javascript

Synchronous testsAsynchronous tests

3 Run these tests

From within a terminalFrom within Eclipse

4 Create javascript code coverage reports

http://westhoffswelt.de [email protected] slide: 4 / 31

Page 13: Javascript Unittesting with js-test-driver

Goals of this session

1 Getting to know js-test-driver

2 Unittesting javascript

Synchronous testsAsynchronous tests

3 Run these tests

From within a terminalFrom within Eclipse

4 Create javascript code coverage reports

http://westhoffswelt.de [email protected] slide: 4 / 31

Page 14: Javascript Unittesting with js-test-driver

What is js-test-driver

Javascript testrunner and framework

Client/Server approach

Server: js-test-driverClient: BrowserClient: Testrunner

Assertion framework

Mostly xUnit compatibleassertEqualsassertSameassertTrue...

http://westhoffswelt.de [email protected] slide: 5 / 31

Page 15: Javascript Unittesting with js-test-driver

What is js-test-driver

Javascript testrunner and framework

Client/Server approach

Server: js-test-driverClient: BrowserClient: Testrunner

Assertion framework

Mostly xUnit compatibleassertEqualsassertSameassertTrue...

http://westhoffswelt.de [email protected] slide: 5 / 31

Page 16: Javascript Unittesting with js-test-driver

What is js-test-driver

Javascript testrunner and framework

Client/Server approach

Server: js-test-driverClient: BrowserClient: Testrunner

Assertion framework

Mostly xUnit compatibleassertEqualsassertSameassertTrue...

http://westhoffswelt.de [email protected] slide: 5 / 31

Page 17: Javascript Unittesting with js-test-driver

What is js-test-driver

Javascript testrunner and framework

Client/Server approach

Server: js-test-driverClient: BrowserClient: Testrunner

Assertion framework

Mostly xUnit compatibleassertEqualsassertSameassertTrue...

http://westhoffswelt.de [email protected] slide: 5 / 31

Page 18: Javascript Unittesting with js-test-driver

What is js-test-driver

Javascript testrunner and framework

Client/Server approach

Server: js-test-driverClient: BrowserClient: Testrunner

Assertion framework

Mostly xUnit compatibleassertEqualsassertSameassertTrue...

http://westhoffswelt.de [email protected] slide: 5 / 31

Page 19: Javascript Unittesting with js-test-driver

What is js-test-driver

Javascript testrunner and framework

Client/Server approach

Server: js-test-driverClient: BrowserClient: Testrunner

Assertion framework

Mostly xUnit compatibleassertEqualsassertSameassertTrue...

http://westhoffswelt.de [email protected] slide: 5 / 31

Page 20: Javascript Unittesting with js-test-driver

What is js-test-driver

Javascript testrunner and framework

Client/Server approach

Server: js-test-driverClient: BrowserClient: Testrunner

Assertion framework

Mostly xUnit compatibleassertEqualsassertSameassertTrue...

http://westhoffswelt.de [email protected] slide: 5 / 31

Page 21: Javascript Unittesting with js-test-driver

What is js-test-driver

Javascript testrunner and framework

Client/Server approach

Server: js-test-driverClient: BrowserClient: Testrunner

Assertion framework

Mostly xUnit compatibleassertEqualsassertSameassertTrue...

http://westhoffswelt.de [email protected] slide: 5 / 31

Page 22: Javascript Unittesting with js-test-driver

What comes next?

Architecture

http://westhoffswelt.de [email protected] slide: 6 / 31

Page 23: Javascript Unittesting with js-test-driver

Architecture of js-test-driver

http://westhoffswelt.de [email protected] slide: 6 / 31

Page 24: Javascript Unittesting with js-test-driver

Architecture of js-test-driver

http://westhoffswelt.de [email protected] slide: 6 / 31

Page 25: Javascript Unittesting with js-test-driver

Architecture of js-test-driver

http://westhoffswelt.de [email protected] slide: 6 / 31

Page 26: Javascript Unittesting with js-test-driver

Architecture of js-test-driver

http://westhoffswelt.de [email protected] slide: 6 / 31

Page 27: Javascript Unittesting with js-test-driver

Architecture of js-test-driver

http://westhoffswelt.de [email protected] slide: 6 / 31

Page 28: Javascript Unittesting with js-test-driver

Architecture of js-test-driver

http://westhoffswelt.de [email protected] slide: 6 / 31

Page 29: Javascript Unittesting with js-test-driver

Architecture of js-test-driver

http://westhoffswelt.de [email protected] slide: 6 / 31

Page 30: Javascript Unittesting with js-test-driver

Advantages of this design

Run from possibly any IDE

CommandlineEclipse

Parallel execution of tests in multiple browsers

Test execution on different machines with possibly differentoperating systems

Website DOM not used for any testrunner output

Browser independent code coverage reports (planned for afuture release)

http://westhoffswelt.de [email protected] slide: 7 / 31

Page 31: Javascript Unittesting with js-test-driver

Advantages of this design

Run from possibly any IDE

CommandlineEclipse

Parallel execution of tests in multiple browsers

Test execution on different machines with possibly differentoperating systems

Website DOM not used for any testrunner output

Browser independent code coverage reports (planned for afuture release)

http://westhoffswelt.de [email protected] slide: 7 / 31

Page 32: Javascript Unittesting with js-test-driver

Advantages of this design

Run from possibly any IDE

CommandlineEclipse

Parallel execution of tests in multiple browsers

Test execution on different machines with possibly differentoperating systems

Website DOM not used for any testrunner output

Browser independent code coverage reports (planned for afuture release)

http://westhoffswelt.de [email protected] slide: 7 / 31

Page 33: Javascript Unittesting with js-test-driver

Advantages of this design

Run from possibly any IDE

CommandlineEclipse

Parallel execution of tests in multiple browsers

Test execution on different machines with possibly differentoperating systems

Website DOM not used for any testrunner output

Browser independent code coverage reports (planned for afuture release)

http://westhoffswelt.de [email protected] slide: 7 / 31

Page 34: Javascript Unittesting with js-test-driver

Advantages of this design

Run from possibly any IDE

CommandlineEclipse

Parallel execution of tests in multiple browsers

Test execution on different machines with possibly differentoperating systems

Website DOM not used for any testrunner output

Browser independent code coverage reports (planned for afuture release)

http://westhoffswelt.de [email protected] slide: 7 / 31

Page 35: Javascript Unittesting with js-test-driver

Advantages of this design

Run from possibly any IDE

CommandlineEclipse

Parallel execution of tests in multiple browsers

Test execution on different machines with possibly differentoperating systems

Website DOM not used for any testrunner output

Browser independent code coverage reports (planned for afuture release)

http://westhoffswelt.de [email protected] slide: 7 / 31

Page 36: Javascript Unittesting with js-test-driver

Advantages of this design

Run from possibly any IDE

CommandlineEclipse

Parallel execution of tests in multiple browsers

Test execution on different machines with possibly differentoperating systems

Website DOM not used for any testrunner output

Browser independent code coverage reports (planned for afuture release)

http://westhoffswelt.de [email protected] slide: 7 / 31

Page 37: Javascript Unittesting with js-test-driver

What comes next?

Example

http://westhoffswelt.de [email protected] slide: 8 / 31

Page 38: Javascript Unittesting with js-test-driver

A simple example

Javascript application/function

Testcase

js-test-driver configuration file

http://westhoffswelt.de [email protected] slide: 8 / 31

Page 39: Javascript Unittesting with js-test-driver

A simple example

Javascript application/function

Testcase

js-test-driver configuration file

http://westhoffswelt.de [email protected] slide: 8 / 31

Page 40: Javascript Unittesting with js-test-driver

A simple example

Javascript application/function

Testcase

js-test-driver configuration file

http://westhoffswelt.de [email protected] slide: 8 / 31

Page 41: Javascript Unittesting with js-test-driver

A simple example - Javascript

Really simple hello world function:

1 myApp = {} ;23 myApp . He l loWor ld = f u n c t i o n ( ) {} ;45 myApp . He l loWor ld . p r o t o t yp e . s a yHe l l o = f u n c t i o n ( ) {6 re tu rn ” He l l o World ! ” ;7 }

http://westhoffswelt.de [email protected] slide: 9 / 31

Page 42: Javascript Unittesting with js-test-driver

A simple example - Testcase

Our first js-test-driver testcase:

1 He l l oWor ldTes t = TestCase ( ’ He l l oWor ld ’ ) ;23 He l l oWor ldTes t . p r o t o t yp e . setUp = f u n c t i o n ( ) {4 t h i s . h e l l o F i x t u r e = new myApp . He l loWor ld ( ) ;5 }67 He l l oWor ldTes t . p r o t o t yp e . t e s tOutpu t = f u n c t i o n ( ) {8 a s s e r t E q u a l s (9 ” He l l o World ! ” ,

10 t h i s . h e l l o F i x t u r e . s a yHe l l o ( )11 ) ;12 }

http://westhoffswelt.de [email protected] slide: 10 / 31

Page 43: Javascript Unittesting with js-test-driver

Digression about test lifecycle

The js-test-driver test lifecycle follows the JUnit lifecycle

1 Instantiate new Testcase implementation

2 Execute the setUp() method

3 Execute the next testWhatever() method

4 Execute the tearDown() method

5 While tests left in testcase jump to Step 1

http://westhoffswelt.de [email protected] slide: 11 / 31

Page 44: Javascript Unittesting with js-test-driver

Digression about test lifecycle

The js-test-driver test lifecycle follows the JUnit lifecycle

1 Instantiate new Testcase implementation

2 Execute the setUp() method

3 Execute the next testWhatever() method

4 Execute the tearDown() method

5 While tests left in testcase jump to Step 1

http://westhoffswelt.de [email protected] slide: 11 / 31

Page 45: Javascript Unittesting with js-test-driver

Digression about test lifecycle

The js-test-driver test lifecycle follows the JUnit lifecycle

1 Instantiate new Testcase implementation

2 Execute the setUp() method

3 Execute the next testWhatever() method

4 Execute the tearDown() method

5 While tests left in testcase jump to Step 1

http://westhoffswelt.de [email protected] slide: 11 / 31

Page 46: Javascript Unittesting with js-test-driver

Digression about test lifecycle

The js-test-driver test lifecycle follows the JUnit lifecycle

1 Instantiate new Testcase implementation

2 Execute the setUp() method

3 Execute the next testWhatever() method

4 Execute the tearDown() method

5 While tests left in testcase jump to Step 1

http://westhoffswelt.de [email protected] slide: 11 / 31

Page 47: Javascript Unittesting with js-test-driver

Digression about test lifecycle

The js-test-driver test lifecycle follows the JUnit lifecycle

1 Instantiate new Testcase implementation

2 Execute the setUp() method

3 Execute the next testWhatever() method

4 Execute the tearDown() method

5 While tests left in testcase jump to Step 1

http://westhoffswelt.de [email protected] slide: 11 / 31

Page 48: Javascript Unittesting with js-test-driver

Digression about test lifecycle

The js-test-driver test lifecycle follows the JUnit lifecycle

1 Instantiate new Testcase implementation

2 Execute the setUp() method

3 Execute the next testWhatever() method

4 Execute the tearDown() method

5 While tests left in testcase jump to Step 1

http://westhoffswelt.de [email protected] slide: 11 / 31

Page 49: Javascript Unittesting with js-test-driver

A simple example - js-test-driver configuration

Every project needs a js-test-driver configuration file.

Written in YAMLCalled jsTestDriver.conf by default

js-test-driver configuration for our example:

1 s e r v e r : h t tp : // l o c a l h o s t :422423 l oad :4 − s r c / h e l l o w o r l d . j s5 − t e s t s / h e l l o w o r l d . j s

http://westhoffswelt.de [email protected] slide: 12 / 31

Page 50: Javascript Unittesting with js-test-driver

A simple example - js-test-driver configuration

Every project needs a js-test-driver configuration file.

Written in YAMLCalled jsTestDriver.conf by default

js-test-driver configuration for our example:

1 s e r v e r : h t tp : // l o c a l h o s t :422423 l oad :4 − s r c / h e l l o w o r l d . j s5 − t e s t s / h e l l o w o r l d . j s

http://westhoffswelt.de [email protected] slide: 12 / 31

Page 51: Javascript Unittesting with js-test-driver

A simple example - js-test-driver configuration

Every project needs a js-test-driver configuration file.

Written in YAMLCalled jsTestDriver.conf by default

js-test-driver configuration for our example:

1 s e r v e r : h t tp : // l o c a l h o s t :422423 l oad :4 − s r c / h e l l o w o r l d . j s5 − t e s t s / h e l l o w o r l d . j s

http://westhoffswelt.de [email protected] slide: 12 / 31

Page 52: Javascript Unittesting with js-test-driver

A simple example - js-test-driver configuration

Every project needs a js-test-driver configuration file.

Written in YAMLCalled jsTestDriver.conf by default

js-test-driver configuration for our example:

1 s e r v e r : h t tp : // l o c a l h o s t :422423 l oad :4 − s r c / h e l l o w o r l d . j s5 − t e s t s / h e l l o w o r l d . j s

http://westhoffswelt.de [email protected] slide: 12 / 31

Page 53: Javascript Unittesting with js-test-driver

Run js-test-driver - Server

Run the js-test-driver server

j a v a − j a r j s T e s tD r i v e r . j a r −−po r t 4224

Capture the all browsers

ht tp : // l o c a l h o s t :4224/ cap tu r e

Execute the testrunner

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s a l l

http://westhoffswelt.de [email protected] slide: 13 / 31

Page 54: Javascript Unittesting with js-test-driver

Run js-test-driver - Browser

Run the js-test-driver server

j a v a − j a r j s T e s tD r i v e r . j a r −−po r t 4224

Capture the all browsers

ht tp : // l o c a l h o s t :4224/ cap tu r e

Execute the testrunner

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s a l l

http://westhoffswelt.de [email protected] slide: 13 / 31

Page 55: Javascript Unittesting with js-test-driver

Run js-test-driver - Testrunner

Run the js-test-driver server

j a v a − j a r j s T e s tD r i v e r . j a r −−po r t 4224

Capture the all browsers

ht tp : // l o c a l h o s t :4224/ cap tu r e

Execute the testrunner

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s a l l

http://westhoffswelt.de [email protected] slide: 13 / 31

Page 56: Javascript Unittesting with js-test-driver

A simple example - Live Demo

Hello World example

Live Demo!

http://westhoffswelt.de [email protected] slide: 14 / 31

Page 57: Javascript Unittesting with js-test-driver

Eclipse testrunner

A testrunner plugin for Eclipse is available

Available through eclipse plugin manager

http://code.google.com/p/js-test-driver/wiki/UsingTheEclipsePlugin

http://westhoffswelt.de [email protected] slide: 15 / 31

Page 58: Javascript Unittesting with js-test-driver

Eclipse testrunner

A testrunner plugin for Eclipse is available

Available through eclipse plugin manager

http://code.google.com/p/js-test-driver/wiki/UsingTheEclipsePlugin

http://westhoffswelt.de [email protected] slide: 15 / 31

Page 59: Javascript Unittesting with js-test-driver

Eclipse testrunner

A testrunner plugin for Eclipse is available

Available through eclipse plugin manager

http://code.google.com/p/js-test-driver/wiki/UsingTheEclipsePlugin

http://westhoffswelt.de [email protected] slide: 15 / 31

Page 60: Javascript Unittesting with js-test-driver

What comes next?

Asynchronous tests

http://westhoffswelt.de [email protected] slide: 16 / 31

Page 61: Javascript Unittesting with js-test-driver

Test asynchronous implementations

Asynchronous timeouts

setTimeout, setInterval

Asynchronous callbacks

XMLHttpRequest, onReady

”[...] JS test driver does not support asynchronous test. This wasa conscious decision, as we want to make sure that the testsremain fast.”

– Misko Hevery on Google groups

http://westhoffswelt.de [email protected] slide: 16 / 31

Page 62: Javascript Unittesting with js-test-driver

Test asynchronous implementations

Asynchronous timeouts

setTimeout, setInterval

Asynchronous callbacks

XMLHttpRequest, onReady

”[...] JS test driver does not support asynchronous test. This wasa conscious decision, as we want to make sure that the testsremain fast.”

– Misko Hevery on Google groups

http://westhoffswelt.de [email protected] slide: 16 / 31

Page 63: Javascript Unittesting with js-test-driver

Test asynchronous implementations

Asynchronous timeouts

setTimeout, setInterval

Asynchronous callbacks

XMLHttpRequest, onReady

”[...] JS test driver does not support asynchronous test. This wasa conscious decision, as we want to make sure that the testsremain fast.”

– Misko Hevery on Google groups

http://westhoffswelt.de [email protected] slide: 16 / 31

Page 64: Javascript Unittesting with js-test-driver

Asynchronous timeouts

window . setTimeout (f u n c t i o n ( ) { . . . } ,3000

) ;

Use jsUnitMockTimeout.jshttp://jsunit.net

Mocks setTimeout, setInterval, clearTimeout andclearInterval

Timeflow can be controlled manually in tests

http://westhoffswelt.de [email protected] slide: 17 / 31

Page 65: Javascript Unittesting with js-test-driver

Asynchronous timeouts

window . setTimeout (f u n c t i o n ( ) { . . . } ,3000

) ;

Use jsUnitMockTimeout.jshttp://jsunit.net

Mocks setTimeout, setInterval, clearTimeout andclearInterval

Timeflow can be controlled manually in tests

http://westhoffswelt.de [email protected] slide: 17 / 31

Page 66: Javascript Unittesting with js-test-driver

Asynchronous timeouts

window . setTimeout (f u n c t i o n ( ) { . . . } ,3000

) ;

Use jsUnitMockTimeout.jshttp://jsunit.net

Mocks setTimeout, setInterval, clearTimeout andclearInterval

Timeflow can be controlled manually in tests

http://westhoffswelt.de [email protected] slide: 17 / 31

Page 67: Javascript Unittesting with js-test-driver

Asynchronous timeouts

window . setTimeout (f u n c t i o n ( ) { . . . } ,3000

) ;

Use jsUnitMockTimeout.jshttp://jsunit.net

Mocks setTimeout, setInterval, clearTimeout andclearInterval

Timeflow can be controlled manually in tests

http://westhoffswelt.de [email protected] slide: 17 / 31

Page 68: Javascript Unittesting with js-test-driver

Asynchronous timeouts

window . setTimeout (f u n c t i o n ( ) { . . . } ,3000

) ;

Use jsUnitMockTimeout.jshttp://jsunit.net

Mocks setTimeout, setInterval, clearTimeout andclearInterval

Timeflow can be controlled manually in tests

http://westhoffswelt.de [email protected] slide: 17 / 31

Page 69: Javascript Unittesting with js-test-driver

jsUnitMockTimeout example - Stopwatch

Simple stopwatch example:

1 Stopwatch . s t a r t = f u n c t i o n ( ) {2 Stopwatch . c l e a r ( ) ;3 Stopwatch . t ime r = s e t I n t e r v a l (4 Stopwatch . advance ,5 10006 ) ;7 }89 Stopwatch . advance = f u n c t i o n ( ) { . . . }

10 Stopwatch . s top = f u n c t i o n ( ) { . . . }11 Stopwatch . c l e a r = f u n c t i o n ( ) { . . . }12 Stopwatch . r e s u l t = f u n c t i o n ( ) { . . . }

http://westhoffswelt.de [email protected] slide: 18 / 31

Page 70: Javascript Unittesting with js-test-driver

jsUnitMockTimeout example - Test

Test using jsUnitMockTimeout:

1 StopwatchTest . p r o t o t yp e . t e s tT im ing = f u n c t i o n ( ) {2 Clock . r e s e t ( ) ;3 Stopwatch . s t a r t ( ) ;4 a s s e r t E q u a l s (0 , Stopwatch . r e s u l t ( ) ) ;56 Clock . t i c k (5000) ;7 a s e r t E q u a l s (5 , Stopwatch . r e s u l t ( ) ) ;8 }

http://westhoffswelt.de [email protected] slide: 19 / 31

Page 71: Javascript Unittesting with js-test-driver

Asynchronous callbacks

Generalized callback mocks do not exist

Mock the used function manually, calling the callbackimmediately

1 xhr . send = f u n c t i o n ( data ) {2 // C a l l onReady wi th a p p r o p r i a t e r e s u l t s

d i r e c t l y3 t h i s . onReady ( someData ) ;4 }

http://westhoffswelt.de [email protected] slide: 20 / 31

Page 72: Javascript Unittesting with js-test-driver

Asynchronous callbacks

Generalized callback mocks do not exist

Mock the used function manually, calling the callbackimmediately

1 xhr . send = f u n c t i o n ( data ) {2 // C a l l onReady wi th a p p r o p r i a t e r e s u l t s

d i r e c t l y3 t h i s . onReady ( someData ) ;4 }

http://westhoffswelt.de [email protected] slide: 20 / 31

Page 73: Javascript Unittesting with js-test-driver

Asynchronous callbacks

Generalized callback mocks do not exist

Mock the used function manually, calling the callbackimmediately

1 xhr . send = f u n c t i o n ( data ) {2 // C a l l onReady wi th a p p r o p r i a t e r e s u l t s

d i r e c t l y3 t h i s . onReady ( someData ) ;4 }

http://westhoffswelt.de [email protected] slide: 20 / 31

Page 74: Javascript Unittesting with js-test-driver

What comes next?

Debugging

http://westhoffswelt.de [email protected] slide: 21 / 31

Page 75: Javascript Unittesting with js-test-driver

Debugging

Use whatever javascript debugging technique you like!

Firebug (Firefox)Visual Studio (IE)Web Inspector (Safari)...

1 Run the test you want to debug:

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s He l l oWor ldTes t .t e s tOutpu t

2 Set breakpoints

3 Rerun the test using the command above.

http://westhoffswelt.de [email protected] slide: 21 / 31

Page 76: Javascript Unittesting with js-test-driver

Debugging

Use whatever javascript debugging technique you like!

Firebug (Firefox)Visual Studio (IE)Web Inspector (Safari)...

1 Run the test you want to debug:

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s He l l oWor ldTes t .t e s tOutpu t

2 Set breakpoints

3 Rerun the test using the command above.

http://westhoffswelt.de [email protected] slide: 21 / 31

Page 77: Javascript Unittesting with js-test-driver

Debugging

Use whatever javascript debugging technique you like!

Firebug (Firefox)Visual Studio (IE)Web Inspector (Safari)...

1 Run the test you want to debug:

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s He l l oWor ldTes t .t e s tOutpu t

2 Set breakpoints

3 Rerun the test using the command above.

http://westhoffswelt.de [email protected] slide: 21 / 31

Page 78: Javascript Unittesting with js-test-driver

Debugging

Use whatever javascript debugging technique you like!

Firebug (Firefox)Visual Studio (IE)Web Inspector (Safari)...

1 Run the test you want to debug:

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s He l l oWor ldTes t .t e s tOutpu t

2 Set breakpoints

3 Rerun the test using the command above.

http://westhoffswelt.de [email protected] slide: 21 / 31

Page 79: Javascript Unittesting with js-test-driver

Debugging

Use whatever javascript debugging technique you like!

Firebug (Firefox)Visual Studio (IE)Web Inspector (Safari)...

1 Run the test you want to debug:

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s He l l oWor ldTes t .t e s tOutpu t

2 Set breakpoints

3 Rerun the test using the command above.

http://westhoffswelt.de [email protected] slide: 21 / 31

Page 80: Javascript Unittesting with js-test-driver

Debugging

Use whatever javascript debugging technique you like!

Firebug (Firefox)Visual Studio (IE)Web Inspector (Safari)...

1 Run the test you want to debug:

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s He l l oWor ldTes t .t e s tOutpu t

2 Set breakpoints

3 Rerun the test using the command above.

http://westhoffswelt.de [email protected] slide: 21 / 31

Page 81: Javascript Unittesting with js-test-driver

Debugging - Using the console

A lot of javascript debuggers provide a console object

c on s o l e . l o g ( ” He l l o World ! ” ) ;

Redirect this output to js-test-driver

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s a l l −−cap tu r eCon so l e

Note: Does not work in Firefox

Use jstestdriver.console directly

j s t e s t d r i v e r . c o n s o l e . l o g ( ” He l l o World ! ” ) ;

http://westhoffswelt.de [email protected] slide: 22 / 31

Page 82: Javascript Unittesting with js-test-driver

Debugging - Using the console

A lot of javascript debuggers provide a console object

c on s o l e . l o g ( ” He l l o World ! ” ) ;

Redirect this output to js-test-driver

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s a l l −−cap tu r eCon so l e

Note: Does not work in Firefox

Use jstestdriver.console directly

j s t e s t d r i v e r . c o n s o l e . l o g ( ” He l l o World ! ” ) ;

http://westhoffswelt.de [email protected] slide: 22 / 31

Page 83: Javascript Unittesting with js-test-driver

Debugging - Using the console

A lot of javascript debuggers provide a console object

c on s o l e . l o g ( ” He l l o World ! ” ) ;

Redirect this output to js-test-driver

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s a l l −−cap tu r eCon so l e

Note: Does not work in Firefox

Use jstestdriver.console directly

j s t e s t d r i v e r . c o n s o l e . l o g ( ” He l l o World ! ” ) ;

http://westhoffswelt.de [email protected] slide: 22 / 31

Page 84: Javascript Unittesting with js-test-driver

Debugging - Using the console

A lot of javascript debuggers provide a console object

c on s o l e . l o g ( ” He l l o World ! ” ) ;

Redirect this output to js-test-driver

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s a l l −−cap tu r eCon so l e

Note: Does not work in Firefox

Use jstestdriver.console directly

j s t e s t d r i v e r . c o n s o l e . l o g ( ” He l l o World ! ” ) ;

http://westhoffswelt.de [email protected] slide: 22 / 31

Page 85: Javascript Unittesting with js-test-driver

Debugging - Using the console

A lot of javascript debuggers provide a console object

c on s o l e . l o g ( ” He l l o World ! ” ) ;

Redirect this output to js-test-driver

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s a l l −−cap tu r eCon so l e

Note: Does not work in Firefox

Use jstestdriver.console directly

j s t e s t d r i v e r . c o n s o l e . l o g ( ” He l l o World ! ” ) ;

http://westhoffswelt.de [email protected] slide: 22 / 31

Page 86: Javascript Unittesting with js-test-driver

Debugging - Using the console

A lot of javascript debuggers provide a console object

c on s o l e . l o g ( ” He l l o World ! ” ) ;

Redirect this output to js-test-driver

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s t s a l l −−cap tu r eCon so l e

Note: Does not work in Firefox

Use jstestdriver.console directly

j s t e s t d r i v e r . c o n s o l e . l o g ( ” He l l o World ! ” ) ;

http://westhoffswelt.de [email protected] slide: 22 / 31

Page 87: Javascript Unittesting with js-test-driver

Debugging - Live Demo

Debugging example

Live Demo!

http://westhoffswelt.de [email protected] slide: 23 / 31

Page 88: Javascript Unittesting with js-test-driver

What comes next?

Automation and CI

http://westhoffswelt.de [email protected] slide: 24 / 31

Page 89: Javascript Unittesting with js-test-driver

Test automation and continuous integration

--testOutput parameter generates JUnit XML compatibletest logs

One XML for every used browser

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s tOutpu t someD i r e c to r y−−t e s t s a l l

--browser parameter runs and captures browsersautomatically

j a v a − j a r j s T e s tD r i v e r . j a r −−browse r e x e cu tab l e1 ,e x e cu tab l e2 , . . . −−po r t 4224

http://westhoffswelt.de [email protected] slide: 24 / 31

Page 90: Javascript Unittesting with js-test-driver

Test automation and continuous integration

--testOutput parameter generates JUnit XML compatibletest logs

One XML for every used browser

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s tOutpu t someD i r e c to r y−−t e s t s a l l

--browser parameter runs and captures browsersautomatically

j a v a − j a r j s T e s tD r i v e r . j a r −−browse r e x e cu tab l e1 ,e x e cu tab l e2 , . . . −−po r t 4224

http://westhoffswelt.de [email protected] slide: 24 / 31

Page 91: Javascript Unittesting with js-test-driver

Test automation and continuous integration

--testOutput parameter generates JUnit XML compatibletest logs

One XML for every used browser

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s tOutpu t someD i r e c to r y−−t e s t s a l l

--browser parameter runs and captures browsersautomatically

j a v a − j a r j s T e s tD r i v e r . j a r −−browse r e x e cu tab l e1 ,e x e cu tab l e2 , . . . −−po r t 4224

http://westhoffswelt.de [email protected] slide: 24 / 31

Page 92: Javascript Unittesting with js-test-driver

Test automation and continuous integration

--testOutput parameter generates JUnit XML compatibletest logs

One XML for every used browser

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s tOutpu t someD i r e c to r y−−t e s t s a l l

--browser parameter runs and captures browsersautomatically

j a v a − j a r j s T e s tD r i v e r . j a r −−browse r e x e cu tab l e1 ,e x e cu tab l e2 , . . . −−po r t 4224

http://westhoffswelt.de [email protected] slide: 24 / 31

Page 93: Javascript Unittesting with js-test-driver

Test automation and continuous integration

--testOutput parameter generates JUnit XML compatibletest logs

One XML for every used browser

j a v a − j a r j s T e s tD r i v e r . j a r −−t e s tOutpu t someD i r e c to r y−−t e s t s a l l

--browser parameter runs and captures browsersautomatically

j a v a − j a r j s T e s tD r i v e r . j a r −−browse r e x e cu tab l e1 ,e x e cu tab l e2 , . . . −−po r t 4224

http://westhoffswelt.de [email protected] slide: 24 / 31

Page 94: Javascript Unittesting with js-test-driver

Test automation and continuous integration

--port and --tests can be combined to automate serverstartup and testrun

j a v a − j a r j s T e s tD r i v e r . j a r −−browse r e x e cu tab l e1 ,e x e cu tab l e2 , . . . −−po r t 4224 −−t e s t s a l l

http://westhoffswelt.de [email protected] slide: 25 / 31

Page 95: Javascript Unittesting with js-test-driver

Test automation and continuous integration

--port and --tests can be combined to automate serverstartup and testrun

j a v a − j a r j s T e s tD r i v e r . j a r −−browse r e x e cu tab l e1 ,e x e cu tab l e2 , . . . −−po r t 4224 −−t e s t s a l l

http://westhoffswelt.de [email protected] slide: 25 / 31

Page 96: Javascript Unittesting with js-test-driver

What comes next?

Code Coverage

http://westhoffswelt.de [email protected] slide: 26 / 31

Page 97: Javascript Unittesting with js-test-driver

Code coverage

Code coverage may be an indicator for untested code paths

js-test-driver plugin allows to create code coverage reports

Possible output formats

Textual output on terminalLCOV compatible info file

http://westhoffswelt.de [email protected] slide: 26 / 31

Page 98: Javascript Unittesting with js-test-driver

Code coverage

Code coverage may be an indicator for untested code paths

js-test-driver plugin allows to create code coverage reports

Possible output formats

Textual output on terminalLCOV compatible info file

http://westhoffswelt.de [email protected] slide: 26 / 31

Page 99: Javascript Unittesting with js-test-driver

Code coverage

Code coverage may be an indicator for untested code paths

js-test-driver plugin allows to create code coverage reports

Possible output formats

Textual output on terminalLCOV compatible info file

http://westhoffswelt.de [email protected] slide: 26 / 31

Page 100: Javascript Unittesting with js-test-driver

Code coverage

Code coverage may be an indicator for untested code paths

js-test-driver plugin allows to create code coverage reports

Possible output formats

Textual output on terminalLCOV compatible info file

http://westhoffswelt.de [email protected] slide: 26 / 31

Page 101: Javascript Unittesting with js-test-driver

Code coverage

Code coverage may be an indicator for untested code paths

js-test-driver plugin allows to create code coverage reports

Possible output formats

Textual output on terminalLCOV compatible info file

http://westhoffswelt.de [email protected] slide: 26 / 31

Page 102: Javascript Unittesting with js-test-driver

Code coverage - Plugin

Download jar-file from js-test-driver homepage

Put the coverage.jar file into some folder relative to yourjs-text-driver.jar (eg. plugins/coverage.jar)

Add plugin to jsTestDriver.conf

1 p l u g i n :2 − name : ” cove rage ”3 j a r : ” p l u g i n s / cove r age . j a r ”4 module : ”com . goog l e . j s t e s t d r i v e r . cove r age .

CoverageModule ”

http://westhoffswelt.de [email protected] slide: 27 / 31

Page 103: Javascript Unittesting with js-test-driver

Code coverage - Plugin

Download jar-file from js-test-driver homepage

Put the coverage.jar file into some folder relative to yourjs-text-driver.jar (eg. plugins/coverage.jar)

Add plugin to jsTestDriver.conf

1 p l u g i n :2 − name : ” cove rage ”3 j a r : ” p l u g i n s / cove r age . j a r ”4 module : ”com . goog l e . j s t e s t d r i v e r . cove r age .

CoverageModule ”

http://westhoffswelt.de [email protected] slide: 27 / 31

Page 104: Javascript Unittesting with js-test-driver

Code coverage - Plugin

Download jar-file from js-test-driver homepage

Put the coverage.jar file into some folder relative to yourjs-text-driver.jar (eg. plugins/coverage.jar)

Add plugin to jsTestDriver.conf

1 p l u g i n :2 − name : ” cove rage ”3 j a r : ” p l u g i n s / cove r age . j a r ”4 module : ”com . goog l e . j s t e s t d r i v e r . cove r age .

CoverageModule ”

http://westhoffswelt.de [email protected] slide: 27 / 31

Page 105: Javascript Unittesting with js-test-driver

Using LCOV to generate HTML reports

The --testOutput commandline parameter generates codecoverage reports in LCOV compatible info file format

Use genHtml from the LCOV package to generate nice HTMLresults:

http://ltp.sourceforge.net

http://westhoffswelt.de [email protected] slide: 28 / 31

Page 106: Javascript Unittesting with js-test-driver

Using LCOV to generate HTML reports

The --testOutput commandline parameter generates codecoverage reports in LCOV compatible info file format

Use genHtml from the LCOV package to generate nice HTMLresults:

http://ltp.sourceforge.net

http://westhoffswelt.de [email protected] slide: 28 / 31

Page 107: Javascript Unittesting with js-test-driver

Code coverage - Live Demo

Code coverage example

Live Demo!

http://westhoffswelt.de [email protected] slide: 29 / 31

Page 108: Javascript Unittesting with js-test-driver

Thanks for listening

Questions, comments or annotations?

Slides: http://westhoffswelt.de/portfolio.htm

Contact: Jakob Westhoff <[email protected]>Twitter: @jakobwesthoff

Please leave comments and vote at: http://joind.in/1626

http://westhoffswelt.de [email protected] slide: 30 / 31

Page 109: Javascript Unittesting with js-test-driver

Sources

Breathe Icon Set:https://launchpad.net/breathe-icon-set, Licensedunder Creative Common Attribution-ShareAlike 3.0 License(http://creativecommons.org/licenses/by-sa/3.0)

http://westhoffswelt.de [email protected] slide: 31 / 31