yui test the next generation (yuiconf 2010)

73
Nicholas C. Zakas Yahoo!, Inc. YUIConf November 9, 2010

Upload: nicholas-zakas

Post on 15-Jan-2015

5.976 views

Category:

Technology


0 download

DESCRIPTION

JavaScript testing has grown by leaps and bounds over the past few years. When YUI Test was first introduced in 2007, it was just the first step in a long process of bringing test-driven development to the front end. YUI Test evolved with the release of YUI 3 to introduce mock objects as feedback indicated a need. As feedback continued to come in, YUI Test continued to evolve. Learn about the next version of YUI Test, how it makes testing any JavaScript code easier, and the brand new tools that allow you to integrate your testing into a continuous integration environment.

TRANSCRIPT

Page 1: YUI Test The Next Generation (YUIConf 2010)

Nicholas C. ZakasYahoo!, Inc.

YUIConfNovember 9, 2010

Page 2: YUI Test The Next Generation (YUIConf 2010)

Who's this guy?

@captain_picard

@slicknet

Page 3: YUI Test The Next Generation (YUIConf 2010)

Who's this guy?

Principal Front End Engineer

Contributor,Creator of YUI Test

Author Lead Author Contributor Lead Author

Page 4: YUI Test The Next Generation (YUIConf 2010)

YUI TestHow'd we get here?

Page 5: YUI Test The Next Generation (YUIConf 2010)

Automated Integration Testingwith YUI Test (Talk)October 2009

YUI Test DebutJuly 2007 (YUI 2.3.0)

Async TestingTestReporterXML and JSON Test FormatsDecember 2007 (YUI 2.4.0)

Mock ObjectsFriendly Test NamesSeptember 2009 (YUI 3.0.0)

Test-Driven Developmentwith YUI Test (Talk)October 2008

YUI Test DebutJuly 2007 (YUI 2.3.0)

Getting started with YUI Test (YUIBlog)December 2008

Writing effective tests(YUIBlog)January 2009

Automation HooksJUnit XML and TAP FormatsApril 2010 (YUI 3.1.0)

ZOMG!!!Today

Yeti ReleasedAugust 2010

Page 6: YUI Test The Next Generation (YUIConf 2010)

Today

YUI TestStandalone Library

YUI TestSelenium Driver

YUI TestCoverage

Page 7: YUI Test The Next Generation (YUIConf 2010)

Today

YUI TestStandalone Library

YUI TestSelenium Driver

YUI TestCoverage

Page 8: YUI Test The Next Generation (YUIConf 2010)

YUI Test Standalone Library has no YUI dependencies

You can load it completely on its own – outside of YUI 2.x and YUI 3.x

Page 9: YUI Test The Next Generation (YUIConf 2010)

Why?To address a series of ongoing

problems

Page 10: YUI Test The Next Generation (YUIConf 2010)

ProblemInconsistencies across versions

Page 11: YUI Test The Next Generation (YUIConf 2010)

YUI Test for YUI 2.xYAHOO.tool.TestCaseYAHOO.tool.TestSuiteYAHOO.tool.TestRunnerYAHOO.tool.TestReporterYAHOO.tool.TestManagerYAHOO.tool.TestFormatYAHOO.util.AssertYAHOO.util.ArrayAssertYAHOO.util.DateAssertYAHOO.util.ObjectAssert

Page 12: YUI Test The Next Generation (YUIConf 2010)

YUI Test for YUI 3.xY.Test.CaseY.Test.SuiteY.Test.RunnerY.Test.ReporterY.Test.FormatY.AssertY.ArrayAssertY.DateAssertY.ObjectAssertY.Mock

Page 13: YUI Test The Next Generation (YUIConf 2010)

Hmmmm...Y.Test.CaseY.Test.SuiteY.Test.RunnerY.Test.Reporter

Y.Test.FormatY.AssertY.ArrayAssertY.DateAssertY.ObjectAssertY.Mock

YAHOO.tool.TestCaseYAHOO.tool.TestSuiteYAHOO.tool.TestRunnerYAHOO.tool.TestReporterYAHOO.tool.TestManagerYAHOO.tool.TestFormatYAHOO.util.AssertYAHOO.util.ArrayAssertYAHOO.util.DateAssertYAHOO.util.ObjectAssert

Page 14: YUI Test The Next Generation (YUIConf 2010)

YUI Test Standalone 1.0.0YUITest.TestCaseYUITest.TestSuiteYUITest.TestRunnerYUITest.ReporterYUITest.PageManagerYUITest.TestFormatYUITest.AssertYUITest.ArrayAssertYUITest.DateAssertYUITest.ObjectAssertYUITest.Mock

Page 15: YUI Test The Next Generation (YUIConf 2010)

ProblemI'd like to use YUI Test, but not YUI

Page 16: YUI Test The Next Generation (YUIConf 2010)
Page 17: YUI Test The Next Generation (YUIConf 2010)

ProblemEveryone else is doing it

Page 18: YUI Test The Next Generation (YUIConf 2010)
Page 19: YUI Test The Next Generation (YUIConf 2010)

What's it look like?

Page 20: YUI Test The Next Generation (YUIConf 2010)

<script type="text/javascript" src="yuitest.js"></script><script type="text/javascript">var testCase = new YUITest.TestCase({ name: "Same ol' test case",

testSomething: function(){ YUITest.Assert.isTrue(true); },

"This should work": function(){ YUITest.Assert.isTrue(works); }});

YUITest.TestRunner.add(testCase);YUITest.TestRunner.run();</script>

FamiliarSyntax!

Page 21: YUI Test The Next Generation (YUIConf 2010)

var testSuite = new YUITest.TestSuite("name");testSuite.add(testCase1);testSuite.add(testCase2);

YUITest.TestRunner.add(testSuite);YUITest.TestRunner.run();

FamiliarSyntax!

Page 22: YUI Test The Next Generation (YUIConf 2010)

Not just a port!API cleanup & bug fixes

Page 23: YUI Test The Next Generation (YUIConf 2010)

YUITest.ObjectAssertareEqual()hasKey()hasKeys()ownsKey()ownsKeys()ownsNoKeys()inheritsKey()inheritsKeys()ownsOrInheritsKey()ownsOrInheritsKeys()

Page 24: YUI Test The Next Generation (YUIConf 2010)

Friendlier Test Namesvar testCase = new YUITest.TestCase({ name: "My Test Case",

"This should be a test": function(){

//this is a test in YUI Test for YUI 3.x

}, "As is this one": function(){

//this is not a test in YUI Test for YUI 3.x //but is in YUI Test Standalone 1.0.0

}});

Page 25: YUI Test The Next Generation (YUIConf 2010)

Counting Asserts

var testCase = new YUITest.TestCase({ name: "My Test Case",

"This should be a test": function(){

//this test passes in YUI Test for YUI 3.x //but fails in YUI Test Standalone 1.0.0

}});

Page 26: YUI Test The Next Generation (YUIConf 2010)

Beyond _should

var testCase = new YUITest.TestCase({ name: "My Test Case",

_should: { //old "Another test": "Error message" },

"Another test": function(){ //new someFunction(); }});

Page 27: YUI Test The Next Generation (YUIConf 2010)

Beyond _should

var testCase = new YUITest.TestCase({ name: "My Test Case",

"Another test": function(){ //new YUITest.Assert.throwsError("Error message", someFunction); }});

Page 28: YUI Test The Next Generation (YUIConf 2010)

What about the YUI 2.x and 3.x versions?

YUI Test Standalone becomes the core implementation of both

Page 29: YUI Test The Next Generation (YUIConf 2010)

Today

YUI TestStandalone Library

YUI TestSelenium Driver

YUI TestCoverage

Page 30: YUI Test The Next Generation (YUIConf 2010)

Easy automation for continuous integration

Page 31: YUI Test The Next Generation (YUIConf 2010)

HudsonContinuous build/testing system

Used to periodically build from source and validate by running tests

http://hudson-ci.org/

Page 32: YUI Test The Next Generation (YUIConf 2010)

?JUnit

Selenium

TAPSelNG

Screen scrape

Hack

Hudson

Page 33: YUI Test The Next Generation (YUIConf 2010)

?JUnit

TAPSelNG

Screen scrape

Hack

Hudson

Selenium

Page 34: YUI Test The Next Generation (YUIConf 2010)

SeleniumWeb application testing system

Primarily used by QA organizations for functional testing

http://seleniumhq.org/

Page 35: YUI Test The Next Generation (YUIConf 2010)

http://seleniumhq.org/projects/remote-control/

Selenium Remote Control

Page 36: YUI Test The Next Generation (YUIConf 2010)

Hudson SeleniumRC

?

Screen scraping to gather results = ick

Page 37: YUI Test The Next Generation (YUIConf 2010)

Hudson SeleniumDriver

SeleniumRC

Page 38: YUI Test The Next Generation (YUIConf 2010)

Selenium DriverThree primary goals

Page 39: YUI Test The Next Generation (YUIConf 2010)

Completely handle communication between Hudson and Selenium RC

Yes, Uhura once wore yellow – focus, people

#1

Page 40: YUI Test The Next Generation (YUIConf 2010)

#2

Extract as much data as possible from testsReturn the results in a format that Hudson can consume and report on

Page 41: YUI Test The Next Generation (YUIConf 2010)

#3

Don't force already-existing tests to changeSeriously, it's just wrong

Page 42: YUI Test The Next Generation (YUIConf 2010)

Test Pages

var testCase = new YUITest.TestCase({ name: "My Test Case",

//...});

YUITest.TestRunner.add(testCase);YUITest.TestRunner.run();

Test pages must automatically beginrunning tests without additional interaction

Page 43: YUI Test The Next Generation (YUIConf 2010)

Command LineUsage: java -jar yuitest-selenium-driver-x.y.z.jar [options] [test files]

Global Options -h, --help Displays this information. --browsers <browsers> Run tests in these browsers (comma-delimited). --conf <file> Load options from <file>. --coveragedir <dir> Output coverage files to <dir>. --erroronfail Indicates that a test failure should cause an error to be reported to the console. --host <host> Use the Selenium host <host>. --port <port> Use <port> port on the Selenium host. --resultsdir <dir> Output test result files to <dir>. --silent Don't output test results to the console. --tests <file> Loads test info from <file>. -v, --verbose Display informational messages and warnings.

Page 44: YUI Test The Next Generation (YUIConf 2010)

Examplesjava -jar yuitest-selenium-driver.jar http://www.example.com/tests/test_ui

java -jar yuitest-selenium-driver.jar --host selenium.example.com --port 9080 http://www.example.com/tests/test_ui

java -jar yuitest-selenium-driver.jar --browsers *firefox,*iexplore http://www.example.com/tests/test_ui http://www.example.com/tests/test_util

java -jar yuitest-selenium-driver.jar --tests tests.xml

Page 45: YUI Test The Next Generation (YUIConf 2010)

Tests File

<?xml version="1.0"?><yuitest> <tests base="http://www.example.com/tests/" timeout="10000"> <url>test_core</url> <url timeout="30000">test_util</url> <url>test_ui</url> </tests></yuitest>

Easiest way to specify multiple tests toexecute on the same host machine

Page 46: YUI Test The Next Generation (YUIConf 2010)

Demo

(Sorry Slideshare viewers)

Page 47: YUI Test The Next Generation (YUIConf 2010)

Today

YUI TestStandalone Library

YUI TestSelenium Driver

YUI TestCoverage

Page 48: YUI Test The Next Generation (YUIConf 2010)

What is code coverage?

Page 49: YUI Test The Next Generation (YUIConf 2010)

I dunna know whathappened, all my tests

passed!

Page 50: YUI Test The Next Generation (YUIConf 2010)

What was it you weretesting, Mr. Scott?

Page 51: YUI Test The Next Generation (YUIConf 2010)

...

Page 52: YUI Test The Next Generation (YUIConf 2010)
Page 53: YUI Test The Next Generation (YUIConf 2010)

Code coverage tells you what code executed during testing

Page 54: YUI Test The Next Generation (YUIConf 2010)

Code can have multiple paths

function doSomething(){ if (condition){ doSomethingElse(); } else { doAThirdThing(); }}

The goal of unit testing is to exercise each pathThe goal of code coverage is to identify if you did

Page 55: YUI Test The Next Generation (YUIConf 2010)

doSomething()

condition

doSomethingElse() doAThirdThing()

Page 56: YUI Test The Next Generation (YUIConf 2010)

doSomething()

condition

doSomethingElse() doAThirdThing()

Page 57: YUI Test The Next Generation (YUIConf 2010)

doSomething()

condition

doAThirdThing()doSomethingElse()

Page 58: YUI Test The Next Generation (YUIConf 2010)

Step OneInstrument JavaScript

Page 59: YUI Test The Next Generation (YUIConf 2010)

Coverage covered-file.jsfile.js

Page 60: YUI Test The Next Generation (YUIConf 2010)

Command LineUsage: java -jar yuitest-coverage-x.y.z.jar [options] [file|dir]

Global Options -h, --help Displays this information. --charset <charset> Read the input file using <charset>. -d, --dir Input and output (-o) are both directories. -v, --verbose Display informational messages and warnings. -o <file|dir> Place the output into <file|dir>. Defaults to stdout.

Page 61: YUI Test The Next Generation (YUIConf 2010)

Step TwoRun tests with instrumented

JavaScript

Page 62: YUI Test The Next Generation (YUIConf 2010)

Step ThreeGenerate reports

Page 63: YUI Test The Next Generation (YUIConf 2010)

Extract Coverage Data Yourself

//object resultsresults = YUITest.TestRunner.getCoverage();

//standard JSON results formatresults = YUITest.TestRunner.getCoverage( YUITest.CoverageFormat.JSON);//alternate formatresults = YUITest.TestRunner.getCoverage( YUITest.CoverageFormat.XebugJSON);

Page 64: YUI Test The Next Generation (YUIConf 2010)

Coverage Report

Reportresults.json

Page 65: YUI Test The Next Generation (YUIConf 2010)

Command LineUsage: java -jar yuitest-coverage-report-x.y.z.jar [options] [file]

Global Options -h, --help Displays this information. --format <format> Output reports in <format>. Defaults to HTML. -v, --verbose Display informational messages and warnings. -o <file|dir> Place the output into <file|dir>.

Page 66: YUI Test The Next Generation (YUIConf 2010)

CoverageSelenium Driver

Selenium Driver automatically pulls in coverage data

Page 67: YUI Test The Next Generation (YUIConf 2010)

Complete Testing Solution

YUI TestStandalone Library

YUI TestSelenium Driver

YUI TestCoverage

JavaScript continuous integration testing system

Page 68: YUI Test The Next Generation (YUIConf 2010)

Compatibility• YUI Test Standalone Library 1.0.0• YUI Test for YUI 3.1.1+• YUI Test for YUI 2.8.1+

Page 69: YUI Test The Next Generation (YUIConf 2010)
Page 70: YUI Test The Next Generation (YUIConf 2010)

Contribute• GitHub:

http://github.com/yui/yuitest/

• YUILibrary:http://yuilibrary.com/projects/yuitest/

• YUIBlog Announcement:http://www.yuiblog.com/blog/2010/11/09/introducing-the-new-yui-test

Page 71: YUI Test The Next Generation (YUIConf 2010)

Questions?

Page 72: YUI Test The Next Generation (YUIConf 2010)

Etcetera• My blog:

www.nczonline.net

• Twitter:@slicknet

• These Slides:http://slideshare.net/nzakas/

• Rate Me:http://spkr8.com/t/4999

Page 73: YUI Test The Next Generation (YUIConf 2010)

Creative Commons Images Used• http://www.flickr.com/photos/anks/3311228772/

• http://www.flickr.com/photos/eschipul/4716821041/