40
Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

TDD, unit testing and java script testing frameworks workshop

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: TDD, unit testing and java script testing frameworks workshop

Boutique product development companyIt is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

Page 2: TDD, unit testing and java script testing frameworks workshop

Boutique product development companyIt is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

Unit Testing, Test Driven Development and JavaScript Testing Frameworks

Sikandar Ahmed | Lead Software Engineer

Ahmad Awais | Software Engineer

Page 3: TDD, unit testing and java script testing frameworks workshop

Unit Testing increases the code quality

Unit Testing, TDD andJavaScript Testing Frameworks

Testing Types

Overview of Unit Testing

Best Practices

Some Difficult Scenarios

Levels of TDD

Test Driven Development

TDD and Agile

Extreme Programming

Comparison of available JS Testing frameworks

Introduction to Jasmine Framework

Examples

Jasmine Framework practical's

Karma Framework practical’s

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 4: TDD, unit testing and java script testing frameworks workshop

WHY Unit Testing

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 5: TDD, unit testing and java script testing frameworks workshop

Types of Tests

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 6: TDD, unit testing and java script testing frameworks workshop

Overview

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

What is Unit Test

–Verifies an atomic piece of code

–Test on specific behavior

–Each test is autonomous

Page 7: TDD, unit testing and java script testing frameworks workshop

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 8: TDD, unit testing and java script testing frameworks workshop

Why Unit Testing

Consider building a Consider building a car from start to car from start to finish, each of the finish, each of the parts which make parts which make the engine, the the engine, the chassis, the wheels, chassis, the wheels, must be individually must be individually verified to be in verified to be in working order before working order before they are to be they are to be assembled into a assembled into a 'car'.'car'.

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 9: TDD, unit testing and java script testing frameworks workshop

Unit Tests are written by developers!

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Increase confidence in code

Fearlessly change your code

Discover usability issues early

Page 10: TDD, unit testing and java script testing frameworks workshop

Test is not a Unit if

Interact over parts of system.

Take too much time to execute (>0.01 Sec).

Require Manual Setup.

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 11: TDD, unit testing and java script testing frameworks workshop

Unit Test Best Practices

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

1. Consistent

2. Atomic

3. Single Responsibility

4. Self Descriptive

Page 12: TDD, unit testing and java script testing frameworks workshop

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 13: TDD, unit testing and java script testing frameworks workshop

Single Responsibility

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

1. One condition per test

2. One reason to change

Page 14: TDD, unit testing and java script testing frameworks workshop

Unit Test Best Practices

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 15: TDD, unit testing and java script testing frameworks workshop

Difficult Scenarios

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 16: TDD, unit testing and java script testing frameworks workshop

Difficult Scenarios

Page 17: TDD, unit testing and java script testing frameworks workshop

Continue..

– Mocks

– Stubs

– Fake

Page 18: TDD, unit testing and java script testing frameworks workshop

Levels of TDD

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

– Acceptance TDD(ATDD)

– Developer TDD

Page 19: TDD, unit testing and java script testing frameworks workshop

Test Driven Development

•Write TestWrite Test•Fail the testFail the test

•Write Minimum CodeWrite Minimum Code•Pass the testPass the test

•Re factor CodeRe factor Code•Meet standardsMeet standards

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 20: TDD, unit testing and java script testing frameworks workshop

Why Test Driven Development

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 21: TDD, unit testing and java script testing frameworks workshop

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

TDD is not about Testing

TDD is about

–Design and Development

•By testing first you design your code

Page 22: TDD, unit testing and java script testing frameworks workshop

Unit Testing and TDD

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 23: TDD, unit testing and java script testing frameworks workshop

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 24: TDD, unit testing and java script testing frameworks workshop

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Rules for Extreme Programming

1. User stories (planning)2. Small releases3. Metaphor (standardized naming schemes)4. Collective ownership5. Coding standard:6. Simple design7. Refactoring8. TDD9. Pair programming10.Continuous integration11.40-hour workweek12.On-site customer

Page 25: TDD, unit testing and java script testing frameworks workshop

XP– Contd.

Page 26: TDD, unit testing and java script testing frameworks workshop

Comparison of JS Unit Testing Frameworks

Framework Suitable forDirect access to

JavaScript DOM APIRemote control

File watching

File preprocessing

Tests written in

Karmaunit yes yes yes yes yes any

JSTestDriverunit yes yes yes no no JS

Seleniume2e no yes yes no no any

WebDrivere2e no yes yes no no any

Jasmineunit yes yes no no no JS

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 27: TDD, unit testing and java script testing frameworks workshop

Jasmine Framework

• Jasmine is a behavior-driven development framework for testing your JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM.

Jasmine API includes features such as:

• A more natural BDD syntax for organizing the test logic than JUnit style assertion test frameworks• Asynchronous testing• Mocks• Easy to create custom matchers• Ability to share or isolate behaviors between tests within a spec encapsulating parts of your spec.• Continuous integration support

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 28: TDD, unit testing and java script testing frameworks workshop

Jasmine Framework: Syntax

Jasmine aims to be easy to read. A simple hello world test looks like this:

describe('Hello world', function() { it('says hello', function() { expect(helloWorld()).toEqual("Hello world!"); });});

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 29: TDD, unit testing and java script testing frameworks workshop

Jasmine Framework: Test Suite and Test Cases

describe('MyApp Test Suite:', function() { });

• The above “Describe” block defines the test suite in Jasmine, in test suite single or multiple test cases can be written.

it('Should contain no JavaScript coding errors!', function() { expect(errorCount).toBe(0); });

• This above “It” block is representing the test cases which can written in Describe block. Expect showing the expected result for the particular test case.

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 30: TDD, unit testing and java script testing frameworks workshop

Example

Page 31: TDD, unit testing and java script testing frameworks workshop

Example

Page 32: TDD, unit testing and java script testing frameworks workshop

Jasmine Framework: Spec and JS Code

A Jasmine test case is written as follows:

// your applications custom code

function addValues( a, b ) {     return a + b; };

// the Jasmine test code

describe("addValues(a, b) function", function() {     it("should equal 3", function(){         expect( addValues(1, 2) ).toBe( 3 );     });     it("should equal 3.75", function(){         expect( addValues(1.75, 2) ).toBe( 3.75 );      });     it("should NOT equal '3' as a String", function(){         expect( addValues(1, 2) ).not.toBe( "3" );     }); });

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 33: TDD, unit testing and java script testing frameworks workshop

Jasmine Framework: Spec Runner (Running the Unit Tests)

Launching the SpecRunner.html file in your local browser runs the tests. Jasmine provides a nice view of the test results.

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 34: TDD, unit testing and java script testing frameworks workshop

Jasmine Framework: Pros and Cons

Pros:• Should not be tied to any browser, framework, platform, or host language.• Should have idiomatic and unsurprising syntax.• Should work anywhere JavaScript can run, including browsers, servers, phones, etc.• Shouldn't intrude in your application's territory (e.g. by cluttering the global namespace).• Should play well with IDEs (e.g. test code should pass static analysis).• It should integrate easily with continuous build systems.• It should be simple to get started with.

Cons: Not Much examples available

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 35: TDD, unit testing and java script testing frameworks workshop

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Basic general test cases

Rainy Days

Sencha Project unit tests

Karma Practical App – ToDo App

Practical Example

Page 36: TDD, unit testing and java script testing frameworks workshop

Jasmine can Use with

• Ruby (with or without Rails)• Spider Monkey• Node.js• JS Test Driver• Java (with Maven)• DOT NET• PERL• Scala

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 37: TDD, unit testing and java script testing frameworks workshop

Testing in Angular JS

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 38: TDD, unit testing and java script testing frameworks workshop
Page 39: TDD, unit testing and java script testing frameworks workshop

References

http://evanhahn.com/how-do-i-jasmine/

http://pivotal.github.io/jasmine/

http://stackoverflow.com/questions/300855/javascript-unit-test-tools-for-tdd

http://docs.angularjs.org/tutorial

http://edspencer.net/2013/07/28/jasmine-and-jenkins-continuous-integration/

http://stackoverflow.com/questions/3459287/whats-the-difference-between-a-mock-stub

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer

Page 40: TDD, unit testing and java script testing frameworks workshop

Q&A

Thanks for Attending

Sikandar Ahmed | Lead Software EngineerAhmad Awais | Software Engineer