16
JAVASCRIPT UNIT TESTING Pulak (@pulakb) http://www.pulakonline.com

JavaScript Unit Testing

  • Upload
    pulak

  • View
    191

  • Download
    3

Embed Size (px)

DESCRIPTION

Presentation developed for JSChannel BLR :: Meetup 8 - Unit testing AngularJS using Jasmine and Karma

Citation preview

Page 1: JavaScript Unit Testing

JAVASCRIPT UNIT TESTINGPulak (@pulakb) http://www.pulakonline.com

Page 2: JavaScript Unit Testing

What is unit testing?

Page 3: JavaScript Unit Testing

What is unit testing?

A unit test is an automated piece of code that invokes a unit of work in the system and then checks a single assumption about the behavior of that unit of work.

Page 4: JavaScript Unit Testing

What is unit testing?

A unit of work is a single logical functional use case in the system that can be invoked by some public interface (in most cases).

A unit of work can span a single method, a whole class or multiple classes working together to achieve one single logical purpose that can be verified.

Page 5: JavaScript Unit Testing

How to define good unit test?

Page 6: JavaScript Unit Testing

How to define good unit test?

Automated

Repeatable Fast

Incremental

Easy to run

Easy to understand

GOOD UNIT TEST

Page 7: JavaScript Unit Testing

Why we need unit testing?

Page 8: JavaScript Unit Testing

Why we need unit testing?

Highly stable solution

Integration of different components will be easier

New and regression defects can be minimized

Good unit testing reduces the test cycle for each phrase

Good unit testing can be a good reference for the system documentation

Page 9: JavaScript Unit Testing

Test-Driven Development

Page 10: JavaScript Unit Testing

Test-Driven Development

Write test cases for a specific part of your code

Write your code to ‘fill in’ the tests. Your code only serves to make all of your tests pass, and nothing more

Once all your tests pass, go back and clean your code (Refactoring)

TDD gives developers the power to think clearly about the specifications before they engage themselves with implementations.

Page 11: JavaScript Unit Testing

Test-Driven Development

Writing a failing test case

Run the test, Fail?

Write application code

Run the tests,

Success?

Refactor?

Refactor the code

Yes

Yes

Yes

No

No

No

Page 12: JavaScript Unit Testing

Behavior-Driven Development

Write application code first.

Test cases must be small and tests one thing. Test a small unit of code instead of the entire application

Run test cases against class or method specific for that test case

If test fails, fix the defects and test again

With BDD, you can write specifications that are small and easy to read.

Page 13: JavaScript Unit Testing

Behavior-Driven Development

Write class(es) or method(s)

Write unit tests

Run the tests, Fail?

Refactor?

Refactor the code

No

Yes

No

Fix the defects

Yes

Page 14: JavaScript Unit Testing

What is Jasmine?

Page 15: JavaScript Unit Testing

What is Jasmine?

Jasmine is a behavior-driven testing framework for JavaScript programming language.

It does not depend on any other JavaScript frameworks. It does not require a DOM.