Front-End Testing: Demystified

43
Web Applications TESTING

description

Confused by testing terms and jargon? This talk walks through testing web applications, explaining the difference between unit testing and end to end testing. Several tools and concepts are also explored through a demo section. Sample projects can be found at https://github.com/sethmcl/testing_web_applications.

Transcript of Front-End Testing: Demystified

Page 1: Front-End Testing: Demystified

Web ApplicationsTESTING

Page 2: Front-End Testing: Demystified

https://www.greendot.com

Sponsored by

Page 3: Front-End Testing: Demystified

Does my code work?

Is my app too slow?

Do users enjoy my product?

Are my users safe from attack?

Does my code work?

Page 4: Front-End Testing: Demystified

time

cost

cost of finding and fixing bugs increases over time

Page 5: Front-End Testing: Demystified

TESTING IS GOOD,MMMKAY?

Page 6: Front-End Testing: Demystified

js test driverunit testing

integration tests

end to end testing

functional tests

mocha

phantomjs

casperjsTDD

testacular

karma

venus

sinonstubs

mocks

spiesfixtures

test harnessCI

expect

jasmine

intern

selenium headless browser

hudson travis

web driver

Page 7: Front-End Testing: Demystified

js test driverunit testing

integration tests

end to end testing

functional tests

mocha

phantomjs

casperjsTDD

testacular

karma

venus

sinonstubs

mocks

spiesfixtures

test harnessCI

expect

jasmine

intern

selenium headless browser

hudson travis

web driver

???

Page 8: Front-End Testing: Demystified

PHILOSOPHIES

TDD, CI

CONSTRUCTStest harness, mocks, fixtures,

stubs, spies, headless browser, web driver

LIBRARIES

mocha, jasmine, qunit chai, expect, sinon

METHODOLOGIES

unit testing, functional tests, end to end testing, integration tests

TOOLS

phantomjs, casperjs, nightwatch, venus, js test driver, karma, protractor, testacular, intern,

hudson, jenkins, travis, selenium

Page 9: Front-End Testing: Demystified

PHILOSOPHIES

Page 10: Front-End Testing: Demystified

Test Driven Development (TDD)

write tests write code

run tests

DEVELOPER

Page 11: Front-End Testing: Demystified

Test Driven Development (TDD)

write tests write code

run tests

DEVELOPER

Page 12: Front-End Testing: Demystified

Test Driven Development (TDD)

write tests write code

run tests

DEVELOPER

Page 13: Front-End Testing: Demystified

Test Driven Development (TDD)

write tests write code

run tests

DEVELOPER

Page 14: Front-End Testing: Demystified

Test Driven Development (TDD)

write tests write code

run tests

DEVELOPER

Page 15: Front-End Testing: Demystified

Continuous Integration (CI)

commits code determines tests to run

executes tests

report test results

DEVELOPER CI SYSTEM

Page 16: Front-End Testing: Demystified

Continuous Integration (CI)

commits code determines tests to run

executes tests

report test results

DEVELOPER CI SYSTEM

Page 17: Front-End Testing: Demystified

Continuous Integration (CI)

commits code determines tests to run

executes tests

report test results

DEVELOPER CI SYSTEM

Page 18: Front-End Testing: Demystified

Continuous Integration (CI)

commits code determines tests to run

executes tests

report test results

DEVELOPER CI SYSTEM

Page 19: Front-End Testing: Demystified

Continuous Integration (CI)

commits code determines tests to run

executes tests

report test results

DEVELOPER CI SYSTEM

Page 20: Front-End Testing: Demystified

METHODOLOGIES

Page 21: Front-End Testing: Demystified

Unit Testing

start coding feature complete ship it!

Page 22: Front-End Testing: Demystified

Unit Testing

start coding feature complete ship it!

function validateInput(input) { var result; validations.some(function (validation) { result = validation.check(input); return !result.valid; }); return result; }

CODE

Page 23: Front-End Testing: Demystified

Unit Testing

start coding feature complete ship it!

function validateInput(input) { var result; validations.some(function (validation) { result = validation.check(input); return !result.valid; }); return result; }

result = validateInput('[email protected]'); expect(result.valid).to.be(true);

CODE

TEST

Page 24: Front-End Testing: Demystified

Unit Testing

start coding feature complete ship it!

Page 25: Front-End Testing: Demystified

TestingEnd to End TestingFunctionalIntegrationScenario

Page 26: Front-End Testing: Demystified

TestingEnd to End

start coding feature complete ship it!

Page 27: Front-End Testing: Demystified

TestingEnd to End

start coding feature complete ship it!

define user scenarios

automate scenarios

test the tests

run in multiple browsers

JS PYTHON RUBY …

Run locally

Run in CI

Pick key features / user actions

Page 28: Front-End Testing: Demystified

TestingEnd to End

start coding feature complete ship it!

define user scenarios

automate scenarios

test the tests

run in multiple browsers

JS PYTHON RUBY …

Run locally

Run in CI

Pick key features / user actions

Page 29: Front-End Testing: Demystified

TestingEnd to End

start coding feature complete ship it!

define user scenarios

automate scenarios

test the tests

run in multiple browsers

JS PYTHON RUBY …

Run locally

Run in CI

Pick key features / user actions

• Login!• Create Account!• Complete Purchase!• Send Message

Page 30: Front-End Testing: Demystified

TestingEnd to End

start coding feature complete ship it!

define user scenarios

automate scenarios

test the tests

run in multiple browsers

JS PYTHON RUBY …

Run locally

Run in CI

Pick key features / user actions

Page 31: Front-End Testing: Demystified

TestingEnd to End

start coding feature complete ship it!

define user scenarios

automate scenarios

test the tests

run in multiple browsers

JS PYTHON RUBY …

Run locally

Run in CI

Pick key features / user actions

Page 32: Front-End Testing: Demystified

TestingEnd to End

start coding feature complete ship it!

define user scenarios

automate scenarios

test the tests

run in multiple browsers

JS PYTHON RUBY …

Run locally

Run in CI

Pick key features / user actions

Page 33: Front-End Testing: Demystified

CONSTRUCTS

Page 34: Front-End Testing: Demystified

TEST HARNESS!A special web page which includes only the scripts and other resources necessary to run a specific unit test. !MOCK / SPY / STUB!Simulates components and functions which are not available in the isolated unit testing environment. !FIXTURE!Fake data used to simulate user input or other information needed for code to execute. !HEADLESS BROWSER!Browser with no UI, useful for testing. Popular example is PhantomJS. http://phantomjs.org/ !WEB DRIVER!API browsers can implement to support automation with tool such as Selenium. http://www.w3.org/TR/webdriver/

Unit TestingEnd to End Testing

Page 35: Front-End Testing: Demystified

LIBRARIES

Page 36: Front-End Testing: Demystified

MOCHA / JASMINE / QUNIT!Provide APIs for writing and organizing unit tests. !CHAI / EXPECT!Provide APIs for writing test assertions. Some libraries come with assertions built-in (Jasmine, QUnit), and some let you to select your own (Mocha). !SINON!Helper library which provides mocks, stubs, spies and other testing constructs. !

Unit TestingEnd to End Testing

Page 37: Front-End Testing: Demystified

TOOLS

Page 38: Front-End Testing: Demystified

PHANTOMJS / SLIMERJS!PhantomJS is a headless Webkit based browser. SlimerJS is a “mostly” headless browser based on Firefox. !CASPERJS!Higher-level abstraction over PhantomJS API, to make it easier to write end to end tests. !VENUS / KARMA / INTERN!Test runners (or, the “glue”) that ties all the other tools together. Testacular was the original name for Karma. !HUDSON / JENKINS / TRAVIS!Continuous Integration tools. Travis is available as a hosted service, with easy integration for Github repositories. It is also free for open source projects.

Unit TestingEnd to End Testing

Page 39: Front-End Testing: Demystified

SELENIUM!Collection of tools / APIs which enable programmatic control of a web browser. !NIGHTWATCH!Provides abstraction on top of Selenium API to make writing end to end tests easier. !!

Unit TestingEnd to End Testing

Page 40: Front-End Testing: Demystified

YOU’RE GONNA HAVE A BAD TIME

IF YOU DON’T HAVE A DEMO…

Page 41: Front-End Testing: Demystified

Demohttps://github.com/sethmcl/testing_web_applications

Page 42: Front-End Testing: Demystified

Seth McLaughlinwww.sethmcl.com

Page 43: Front-End Testing: Demystified

ReferencesIntroduction to writing testable JavaScript!http://www.smashingmagazine.com/2012/06/27/introduction-to-javascript-unit-testing-2/ http://www.youtube.com/watch?v=-IYqgx8JxlU&index=13&list=PLZDyxA22zzGxzzIC21pyyfaAjbtCVG-tn !Venus.js Test Runner!http://www.venusjs.org/ http://www.youtube.com/watch?v=fvHk7KWa25o !Karma Test Runner!http://www.youtube.com/watch?v=YG5DEzaQBIc http://karma-runner.github.io/0.12/index.html !Nightwatch (built on Selenium WebDriver)!http://nightwatchjs.org/ !Selenium Architecture (deep dive)!http://aosabook.org/en/selenium.html !!!!