31
QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Embed Size (px)

DESCRIPTION

Outline for Today’s Talk What’s All This About Unit Testing? What Do You Mean By JavaScript Unit Testing? The QUnit Framework The End Result

Citation preview

Page 1: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

QUnit JavaScript Testing in the Enterprise

David LeeOct. 11, 2011

Page 2: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Getting to Know You

• How many of you work on Websites that use JavaScript?

• How many currently have a Unit Testing strategy?

• About me.

Page 3: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Outline for Today’s Talk

• What’s All This About Unit Testing?• What Do You Mean By JavaScript Unit Testing?• The QUnit Framework• The End Result

Page 4: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

What is Unit Testing?• What is all this hullaballoo about?

Page 5: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

What is Unit Testing?

When the rubber meets the road testing is…• Developers code tests that

• describe desired functionality, • access this functionality, • then verify that this functionality meets expectations

• Reporting• Should include reporting • And scheduled test runs

Page 6: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Unit Testing Takes Time

• Unit testing requires up front time• Later this saves time for development and QA• Time spent up front includes:– Developer time: • Test creation --- coding of tests

– Infrastructure Setup: • Automation of tests• Configuration of testing reports

Page 7: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Unit Testing Has Many Benefits

• Let’s see what some of these are…

Page 8: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Unit Testing Benefits

• Fewer bugs

Page 9: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Unit Testing Gets Your Attention• Immediate feedback when code goes wrong

(in a good way)

Page 10: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

A Time Saver

• Overall the process of development, testing and dealing with bugs takes less time

Page 11: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

And What else...• Intercepts errors before they happen• Keeps developers focused on the

requirements• Reduces workload on QA / Dev

Page 12: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

JavaScript Unit Testing?• Why test JavaScript?• Typical unit testing strategies• JavaScript unit testing strategies• Problems being near the UI

Page 13: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Why JavaScript Testing?• More and more technologies bring the logic

client-side:– JSON– JQuery– AJAX – HTML5– HTML5 for Mobile devices

• Richer client-side experiences lead to more JavaScript

Page 14: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Why JavaScript Unit Testing?• With more JavaScript in the client there is

more opportunity for code errors in JavaScript• Relevant behaviors remain untested when

ignoring JavaScript

Page 15: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Typical Unit Testing StrategiesTypically unit testing is done on application code residing in the middle layer

User Interface

Application Code

Data Access

Unit Testing

Page 16: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

JavaScript Unit Testing StrategiesBut JavaScriptunit testing is done on script thatresides in or near the user interface

User Interface

Application Code

Data Access

Unit Testing

JavaScript

Page 17: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Problems being near the UI

• JavaScript & HTML are hard to separate• Problems relying on HTML

Page 18: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

JavaScript &HTML are hard to separate

• Historically JavaScript & HTML are inseparable• JQuery make it harder to separate JavaScript

from HTML$(".className > div").empty();

Page 19: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Problems Relying on HTML

• Testing strategies say to test logic and not the User Interface

• If we rely on HTML it can cause problems:– HTML is fluid (changeable)• Controls, add-ins, database records, etc. all change

HTML– Designers & Programmers (two hands in the mix)

Page 20: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

How to test, but include HTML?

• Option 1: Copy HTML for our tests?• Option 2: Run our tests directly against the

web page?• Option 3: Not test JavaScript that has a

relationship to HTML• Option 4: Some sort of hybrid approach

Page 21: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Option 1: Copy HTML for our tests?

• HTML may be required, so should we copy it for our tests?

Static HTML may become old and irrelevant

Page 22: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Option 1: Copy HTML for our tests?

Copy HTML

Page 23: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Option 2: Run tests against the page

• Tests could modify or significantly change the behavior / look /feel of a web page

• Example: running a test that inserts a block of text… what happens at the end of the test.

Page 24: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Option 3: Avoid HTML Altogether

• We ruled this out because it limited the number of tests we were able to create.

• Many pages with significant JavaScript would remain untested.

Page 25: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

The QUnit Framework

• A look at QUnit examples

Page 26: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

The End Result

• Our HTML Hybrid Testing Strategy• Our JavaScript Testing Strategy

Page 27: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Our HTML Hybrid Testing Strategy

• Runs tests in an IFRAME• Copies HTML from the actual page

immediately

Page 28: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Our JavaScript Testing Strategy

• Make it easy to wire in tests• Use site-wide hooks to keep the developer’s

job easy• Put test failures and successes in plain view

Page 29: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

And Some Final Thoughts…• Unit Testing is a valuable resource for YOU!• The Use of JavaScript Unit Testing is timely and

relevant given today’s technologies.• To either deny or use HTML in your JavaScript

tests will take forethought. In many cases it is not a simple decision.

• The provided libraries and sample tests will hopefully give you a jump start into the world of JavaScript Unit Testing.

Page 30: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

Questions

• Questions?

Page 31: QUnit JavaScript Testing in the Enterprise David Lee Oct. 11, 2011

QUnit JavaScript Testing in the Enterprise

Thank you!