JavaScript Unit Testing

Preview:

DESCRIPTION

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

Citation preview

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

What is 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.

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.

How to define good unit test?

How to define good unit test?

Automated

Repeatable Fast

Incremental

Easy to run

Easy to understand

GOOD UNIT TEST

Why we need 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

Test-Driven Development

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.

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

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.

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

What is Jasmine?

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.