SlideShare a Scribd company logo
Boutique product development company
It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.
Boutique product development company
It 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
Unit Testing increases the code quality
Unit Testing, TDD and
JavaScript 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 Engineer
Ahmad Awais | Software Engineer
WHY Unit Testing
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Types of Tests
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Overview
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
What is Unit Test
–Verifies an atomic piece of code
–Test on specific behavior
–Each test is autonomous
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Why Unit Testing
Consider building a car
from start to finish,
each of the parts which
make the engine, the
chassis, the wheels,
must be individually
verified to be in working
order before they are to
be assembled into a
'car'.
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Unit Tests are written by developers!
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
 Increase confidence in code
 Fearlessly change your code
 Discover usability issues early
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 Engineer
Ahmad Awais | Software Engineer
Unit Test Best Practices
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
1. Consistent
2. Atomic
3. Single Responsibility
4. Self Descriptive
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Single Responsibility
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
1. One condition per test
2. One reason to change
Unit Test Best Practices
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Difficult Scenarios
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Difficult Scenarios
Continue..
– Mocks
– Stubs
– Fake
Levels of TDD
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
– Acceptance TDD(ATDD)
– Developer TDD
Test Driven Development
•Write Test
•Fail the test
•Write Minimum Code
•Pass the test
•Re factor Code
•Meet standards
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Why Test Driven Development
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
TDD is not about Testing
TDD is about
–Design and Development
•By testing first you design your code
Unit Testing and TDD
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Rules for Extreme Programming
1. User stories (planning)
2. Small releases
3. Metaphor (standardized naming schemes)
4. Collective ownership
5. Coding standard:
6. Simple design
7. Refactoring
8. TDD
9. Pair programming
10.Continuous integration
11.40-hour workweek
12.On-site customer
XP– Contd.
Comparison of JS Unit Testing Frameworks
Framework Suitable for
Direct access to
JavaScript DOM API
Remote
control
File
watching
File
preprocessing
Tests written
in
Karma
unit yes yes yes yes yes any
JSTestDriver
unit yes yes yes no no JS
Selenium
e2e no yes yes no no any
WebDriver
e2e no yes yes no no any
Jasmine
unit yes yes no no no JS
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
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 Engineer
Ahmad Awais | Software Engineer
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 Engineer
Ahmad Awais | Software Engineer
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 Engineer
Ahmad Awais | Software Engineer
Example
Example
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 Engineer
Ahmad Awais | Software Engineer
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 Engineer
Ahmad Awais | Software Engineer
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 Engineer
Ahmad Awais | Software Engineer
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
Basic general test cases
Rainy Days
Sencha Project unit tests
Karma Practical App – ToDo App
Practical Example
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 Engineer
Ahmad Awais | Software Engineer
Testing in Angular JS
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer
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 Engineer
Ahmad Awais | Software Engineer
Q&A
Thanks for Attending
Sikandar Ahmed | Lead Software Engineer
Ahmad Awais | Software Engineer

More Related Content

What's hot (20)

PPTX
Amazon CodeGuru - Automate Code review and Code performance monitoring
Olawale Olaleye
 
PPTX
Behaviour Driven Development
Richard Ruiter
 
ODP
Test Automation Framework using Cucumber BDD overview (part 1)
Mindfire Solutions
 
PPTX
Bdd – with cucumber and gherkin
Arati Joshi
 
PPSX
Cucumber & gherkin language
selvanathankapilan
 
PPTX
Introduction to Bdd and cucumber
Nibu Baby
 
PPTX
PHPConf.asia 2016 - BDD with Behat for Beginners
Adam Englander
 
PPT
Ria Made Easier With Zend
Roy Ganor
 
PPTX
Understanding Why Testing is Importaint
Sana Nasar
 
PPTX
What Is Cucumber?
QATestLab
 
PDF
Behavior Driven Testing - A paradigm shift
Aspire Systems
 
PPTX
Test Automation Framework with BDD and Cucumber
Rhoynar Software Consulting
 
PPTX
BDD approach with Selenium RC
Mykola Kolisnyk
 
PPTX
Behavior Driven Development - TdT@Cluj #15
Tabăra de Testare
 
PPTX
Bdd and spec flow
Charles Nurse
 
PPTX
Bdd in action
Kien Nguyen
 
PPTX
Testing with cucumber testing framework
AIMDek Technologies
 
PDF
Test and Behaviour Driven Development (TDD/BDD)
Lars Thorup
 
PPTX
Cypress test techniques cucumber bdd framework,tdd,api tests course
Narayanan Palani
 
PPTX
Cypress Test Techniques-Cucumber BDD Framework,TDD,API Tests
HiraQureshi22
 
Amazon CodeGuru - Automate Code review and Code performance monitoring
Olawale Olaleye
 
Behaviour Driven Development
Richard Ruiter
 
Test Automation Framework using Cucumber BDD overview (part 1)
Mindfire Solutions
 
Bdd – with cucumber and gherkin
Arati Joshi
 
Cucumber & gherkin language
selvanathankapilan
 
Introduction to Bdd and cucumber
Nibu Baby
 
PHPConf.asia 2016 - BDD with Behat for Beginners
Adam Englander
 
Ria Made Easier With Zend
Roy Ganor
 
Understanding Why Testing is Importaint
Sana Nasar
 
What Is Cucumber?
QATestLab
 
Behavior Driven Testing - A paradigm shift
Aspire Systems
 
Test Automation Framework with BDD and Cucumber
Rhoynar Software Consulting
 
BDD approach with Selenium RC
Mykola Kolisnyk
 
Behavior Driven Development - TdT@Cluj #15
Tabăra de Testare
 
Bdd and spec flow
Charles Nurse
 
Bdd in action
Kien Nguyen
 
Testing with cucumber testing framework
AIMDek Technologies
 
Test and Behaviour Driven Development (TDD/BDD)
Lars Thorup
 
Cypress test techniques cucumber bdd framework,tdd,api tests course
Narayanan Palani
 
Cypress Test Techniques-Cucumber BDD Framework,TDD,API Tests
HiraQureshi22
 

Viewers also liked (20)

PDF
Angular testing
Raissa Ferreira
 
PPT
AngularJS Testing Strategies
njpst8
 
PDF
Angular 2 - What's new and what's different
Priscila Negreiros
 
PPTX
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
PDF
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
KEY
Javascript Tests with Jasmine for Front-end Devs
Chris Powers
 
PDF
Introduction to Javascript Unit Testing With xUnit.js
Salesforce Developers
 
PPT
Testing Javascript with Jasmine
Tim Tyrrell
 
PPTX
JavaScript Unit Testing
L&T Technology Services Limited
 
PDF
Client side unit tests - using jasmine & karma
Adam Klein
 
PDF
Story about module management with angular.js
David Amend
 
PDF
EasyTest Test Automation Tool Introduction
Zhu Zhong
 
PPTX
Testing angular js
galan83
 
PPTX
Slaven tomac unit testing in angular js
Slaven Tomac
 
PDF
Test-Driven Development with TypeScript+Jasmine+AngularJS
SmartOrg
 
PDF
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
PDF
AngularJS Testing
Ahmed Elmehri
 
PDF
Test-Driven Development of AngularJS Applications
FITC
 
PDF
AngularJS Unit Test
Chiew Carol
 
PDF
Advanced Jasmine - Front-End JavaScript Unit Testing
Lars Thorup
 
Angular testing
Raissa Ferreira
 
AngularJS Testing Strategies
njpst8
 
Angular 2 - What's new and what's different
Priscila Negreiros
 
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
Javascript Tests with Jasmine for Front-end Devs
Chris Powers
 
Introduction to Javascript Unit Testing With xUnit.js
Salesforce Developers
 
Testing Javascript with Jasmine
Tim Tyrrell
 
JavaScript Unit Testing
L&T Technology Services Limited
 
Client side unit tests - using jasmine & karma
Adam Klein
 
Story about module management with angular.js
David Amend
 
EasyTest Test Automation Tool Introduction
Zhu Zhong
 
Testing angular js
galan83
 
Slaven tomac unit testing in angular js
Slaven Tomac
 
Test-Driven Development with TypeScript+Jasmine+AngularJS
SmartOrg
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
AngularJS Testing
Ahmed Elmehri
 
Test-Driven Development of AngularJS Applications
FITC
 
AngularJS Unit Test
Chiew Carol
 
Advanced Jasmine - Front-End JavaScript Unit Testing
Lars Thorup
 
Ad

Similar to TDD, unit testing and java script testing frameworks workshop (20)

PDF
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
PDF
Front end unit testing using jasmine
Gil Fink
 
PDF
Quick Tour to Front-End Unit Testing Using Jasmine
Gil Fink
 
PPTX
Jasmine Testing to the Rescue!
Christopher Steele
 
PPTX
Tools for Software Testing
Mohammed Moishin
 
ODP
Dot Net Notts Js Unit Testing at Microlise
Jonathan Gregory
 
PDF
How to write Testable Javascript
ColdFusionConference
 
PDF
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
Gavin Pickin
 
PPT
Jasmine - A BDD test framework for JavaScript
Sumanth krishna
 
ODP
Js unit testingpresentation
Jonathan Gregory
 
PPTX
How do I write Testable Javascript so I can Test my CF API on Server and Client
Gavin Pickin
 
PDF
Unit testing (eng)
Anatoliy Okhotnikov
 
PPTX
Testing JavaScript with Jasmine in Rails Applications
Hector Correa
 
PDF
How do I Write Testable Javascript so I can Test my CF API on Server and Client
ColdFusionConference
 
PPT
Test Automation Framework Designs
Test Automaton
 
PDF
Unit-testing and E2E testing in JS
Michael Haberman
 
PPT
Jasmine presentation Selenium Camp 2013
dimakovalenko
 
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
Front end unit testing using jasmine
Gil Fink
 
Quick Tour to Front-End Unit Testing Using Jasmine
Gil Fink
 
Jasmine Testing to the Rescue!
Christopher Steele
 
Tools for Software Testing
Mohammed Moishin
 
Dot Net Notts Js Unit Testing at Microlise
Jonathan Gregory
 
How to write Testable Javascript
ColdFusionConference
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
Gavin Pickin
 
Jasmine - A BDD test framework for JavaScript
Sumanth krishna
 
Js unit testingpresentation
Jonathan Gregory
 
How do I write Testable Javascript so I can Test my CF API on Server and Client
Gavin Pickin
 
Unit testing (eng)
Anatoliy Okhotnikov
 
Testing JavaScript with Jasmine in Rails Applications
Hector Correa
 
How do I Write Testable Javascript so I can Test my CF API on Server and Client
ColdFusionConference
 
Test Automation Framework Designs
Test Automaton
 
Unit-testing and E2E testing in JS
Michael Haberman
 
Jasmine presentation Selenium Camp 2013
dimakovalenko
 
Ad

Recently uploaded (20)

PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 

TDD, unit testing and java script testing frameworks workshop

  • 1. Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.
  • 2. Boutique product development company It 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
  • 3. Unit Testing increases the code quality Unit Testing, TDD and JavaScript 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 Engineer Ahmad Awais | Software Engineer
  • 4. WHY Unit Testing Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 5. Types of Tests Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 6. Overview Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer What is Unit Test –Verifies an atomic piece of code –Test on specific behavior –Each test is autonomous
  • 7. Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 8. Why Unit Testing Consider building a car from start to finish, each of the parts which make the engine, the chassis, the wheels, must be individually verified to be in working order before they are to be assembled into a 'car'. Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 9. Unit Tests are written by developers! Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer  Increase confidence in code  Fearlessly change your code  Discover usability issues early
  • 10. 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 Engineer Ahmad Awais | Software Engineer
  • 11. Unit Test Best Practices Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer 1. Consistent 2. Atomic 3. Single Responsibility 4. Self Descriptive
  • 12. Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 13. Single Responsibility Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer 1. One condition per test 2. One reason to change
  • 14. Unit Test Best Practices Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 15. Difficult Scenarios Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 18. Levels of TDD Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer – Acceptance TDD(ATDD) – Developer TDD
  • 19. Test Driven Development •Write Test •Fail the test •Write Minimum Code •Pass the test •Re factor Code •Meet standards Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 20. Why Test Driven Development Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 21. Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer TDD is not about Testing TDD is about –Design and Development •By testing first you design your code
  • 22. Unit Testing and TDD Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 23. Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 24. Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer Rules for Extreme Programming 1. User stories (planning) 2. Small releases 3. Metaphor (standardized naming schemes) 4. Collective ownership 5. Coding standard: 6. Simple design 7. Refactoring 8. TDD 9. Pair programming 10.Continuous integration 11.40-hour workweek 12.On-site customer
  • 26. Comparison of JS Unit Testing Frameworks Framework Suitable for Direct access to JavaScript DOM API Remote control File watching File preprocessing Tests written in Karma unit yes yes yes yes yes any JSTestDriver unit yes yes yes no no JS Selenium e2e no yes yes no no any WebDriver e2e no yes yes no no any Jasmine unit yes yes no no no JS Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 27. 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 Engineer Ahmad Awais | Software Engineer
  • 28. 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 Engineer Ahmad Awais | Software Engineer
  • 29. 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 Engineer Ahmad Awais | Software Engineer
  • 32. 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 Engineer Ahmad Awais | Software Engineer
  • 33. 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 Engineer Ahmad Awais | Software Engineer
  • 34. 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 Engineer Ahmad Awais | Software Engineer
  • 35. Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer Basic general test cases Rainy Days Sencha Project unit tests Karma Practical App – ToDo App Practical Example
  • 36. 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 Engineer Ahmad Awais | Software Engineer
  • 37. Testing in Angular JS Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer
  • 40. Q&A Thanks for Attending Sikandar Ahmed | Lead Software Engineer Ahmad Awais | Software Engineer