SlideShare a Scribd company logo
1 of 18
Download to read offline
JavaScript
                           Test-Driven Development
                                  with QUnit
                                       @tasanakorn
                                http://www.tasanakorn.com




                                   Barcamp Bangkhen, 2012-09-16
Tuesday, 18 September 12
Agenda


                           Concept

                           QUnit

                           Example

                           Discussion




Tuesday, 18 September 12
Software Development
                             Process

                           Plain Old Unit Testing

                             Test-Last

                           Test-Driven Development (TDD)

                             Test-First




Tuesday, 18 September 12
Test-Driven Development
                           (TDD)

                           Test

                           Code

                           Refactor




Tuesday, 18 September 12
Development Style


                           Keep It Simple, Stupid (KISS)

                           You ain’t gonna need it (YAGNI)

                           Fake it till you make it




Tuesday, 18 September 12
Unit Test




Tuesday, 18 September 12
QUnit

                           QUnit is a powerful, easy-to-use JavaScript
                           unit test suite. It's used by the jQuery,
                           jQuery UI and jQuery Mobile projects and is
                           capable of testing any generic JavaScript
                           code, including itself!

                           http://qunitjs.com




Tuesday, 18 September 12
QUnit: Assert


                           ok(truthy [, message])

                           equal(actual, expected [, message])

                           deepEqual(actual, expected [, message])




Tuesday, 18 September 12
Example
                           Validate Telephone Number




Tuesday, 18 September 12
QUnit : HTML test suite
   <!DOCTYPE html>
   <html>
   <head>
     <meta charset="utf-8">
     <title>QUnit Example</title>
     <link rel="stylesheet" href="/resources/qunit.css">
   </head>
   <body>
     <div id="qunit"></div>
     <script src="/resources/qunit.js"></script>
     <script src="/resources/tests.js"></script>
   </body>
   </html>




Tuesday, 18 September 12
QUnit: Add a test

   // tests.js

   // Test
   module("isPhoneNumber");

   test("Wrong number", function () {
       equal(isPhoneNumber("abcdefg"), false, "All alphabet");
   });




Tuesday, 18 September 12
QUnit: Run all test and
                  see if the new one fails




Tuesday, 18 September 12
QUnit: Write some code

   // Code Under Test
   isPhoneNumber = function(a) {
       return false;
   }



   // Test
   module("isPhoneNumber");

   test("Wrong number", function () {
       equal(isPhoneNumber("abcdefg"), false, "All alphabet");
   });




Tuesday, 18 September 12
QUnit: Run automated tests
                  and see them succeed




Tuesday, 18 September 12
QUnit: Refactor code

   // Code Under Test
   isPhoneNumber = function(input) {
       return false;
   }



   // Test
   module("isPhoneNumber");

   test("Wrong number", function () {
       equal(isPhoneNumber("abcdefg"), false, "All alphabet");
   });




Tuesday, 18 September 12
QUnit: Re-running test
                             cases




Tuesday, 18 September 12
QUnit: Repeat
   // Code Under Test
   isPhoneNumber = function(input) {
       reg = /^d+$/;
       if (reg.test(input)) {
           return true;
       }
       return false;
   }

   // Test
   module("isPhoneNumber");

   test("Wrong number", function () {
       equal(isPhoneNumber("abcdefg"), false, "All alphabet");
       equal(isPhoneNumber("021234567"), true, "input = 021234567");
       equal(isPhoneNumber("0819876543"), true, "input = 021234567");
   });

Tuesday, 18 September 12
Related Topics

                           JsUnit

                           JSMock, JSmockito

                           Selenium

                           JSpec, Jasmine, JSSpec

                           ...



Tuesday, 18 September 12

More Related Content

Similar to JavaScript Test-Driven Development (TDD) with QUnit

03 Advanced JavaScript
03 Advanced JavaScript03 Advanced JavaScript
03 Advanced JavaScriptYnon Perek
 
Creating Maintainable Automated Acceptance Tests
Creating Maintainable Automated Acceptance TestsCreating Maintainable Automated Acceptance Tests
Creating Maintainable Automated Acceptance TestsJez Humble
 
Automated tests workshop
Automated tests workshopAutomated tests workshop
Automated tests workshopdrewz lin
 
Test Infected Presentation
Test Infected PresentationTest Infected Presentation
Test Infected Presentationwillmation
 
Integrate Spring MVC with RequireJS & Backbone.js & Spring Data JPA
Integrate Spring MVC with RequireJS & Backbone.js & Spring Data JPAIntegrate Spring MVC with RequireJS & Backbone.js & Spring Data JPA
Integrate Spring MVC with RequireJS & Backbone.js & Spring Data JPACheng Ta Yeh
 
A Big Look at MiniTest
A Big Look at MiniTestA Big Look at MiniTest
A Big Look at MiniTestMark
 
Patroklos Papapetrou: Holding Down Your Technical Debt With SonarQube at I T....
Patroklos Papapetrou: Holding Down Your Technical Debt With SonarQube at I T....Patroklos Papapetrou: Holding Down Your Technical Debt With SonarQube at I T....
Patroklos Papapetrou: Holding Down Your Technical Debt With SonarQube at I T....Mozaic Works
 
ITAKE Unconference - Holding down your technical debt with Sonarqube
ITAKE Unconference - Holding down your technical debt with SonarqubeITAKE Unconference - Holding down your technical debt with Sonarqube
ITAKE Unconference - Holding down your technical debt with SonarqubePatroklos Papapetrou (Pat)
 
Ruby meetup 7_years_in_testing
Ruby meetup 7_years_in_testingRuby meetup 7_years_in_testing
Ruby meetup 7_years_in_testingDigital Natives
 
BDD, ATDD, Page Objects: The Road to Sustainable Web Testing
BDD, ATDD, Page Objects: The Road to Sustainable Web TestingBDD, ATDD, Page Objects: The Road to Sustainable Web Testing
BDD, ATDD, Page Objects: The Road to Sustainable Web TestingJohn Ferguson Smart Limited
 

Similar to JavaScript Test-Driven Development (TDD) with QUnit (12)

03 Advanced JavaScript
03 Advanced JavaScript03 Advanced JavaScript
03 Advanced JavaScript
 
Creating Maintainable Automated Acceptance Tests
Creating Maintainable Automated Acceptance TestsCreating Maintainable Automated Acceptance Tests
Creating Maintainable Automated Acceptance Tests
 
Automated tests workshop
Automated tests workshopAutomated tests workshop
Automated tests workshop
 
Test Infected Presentation
Test Infected PresentationTest Infected Presentation
Test Infected Presentation
 
JavaScript QA Tools
JavaScript QA ToolsJavaScript QA Tools
JavaScript QA Tools
 
Integrate Spring MVC with RequireJS & Backbone.js & Spring Data JPA
Integrate Spring MVC with RequireJS & Backbone.js & Spring Data JPAIntegrate Spring MVC with RequireJS & Backbone.js & Spring Data JPA
Integrate Spring MVC with RequireJS & Backbone.js & Spring Data JPA
 
Testing AngularJS
Testing AngularJSTesting AngularJS
Testing AngularJS
 
A Big Look at MiniTest
A Big Look at MiniTestA Big Look at MiniTest
A Big Look at MiniTest
 
Patroklos Papapetrou: Holding Down Your Technical Debt With SonarQube at I T....
Patroklos Papapetrou: Holding Down Your Technical Debt With SonarQube at I T....Patroklos Papapetrou: Holding Down Your Technical Debt With SonarQube at I T....
Patroklos Papapetrou: Holding Down Your Technical Debt With SonarQube at I T....
 
ITAKE Unconference - Holding down your technical debt with Sonarqube
ITAKE Unconference - Holding down your technical debt with SonarqubeITAKE Unconference - Holding down your technical debt with Sonarqube
ITAKE Unconference - Holding down your technical debt with Sonarqube
 
Ruby meetup 7_years_in_testing
Ruby meetup 7_years_in_testingRuby meetup 7_years_in_testing
Ruby meetup 7_years_in_testing
 
BDD, ATDD, Page Objects: The Road to Sustainable Web Testing
BDD, ATDD, Page Objects: The Road to Sustainable Web TestingBDD, ATDD, Page Objects: The Road to Sustainable Web Testing
BDD, ATDD, Page Objects: The Road to Sustainable Web Testing
 

Recently uploaded

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Recently uploaded (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

JavaScript Test-Driven Development (TDD) with QUnit

  • 1. JavaScript Test-Driven Development with QUnit @tasanakorn http://www.tasanakorn.com Barcamp Bangkhen, 2012-09-16 Tuesday, 18 September 12
  • 2. Agenda Concept QUnit Example Discussion Tuesday, 18 September 12
  • 3. Software Development Process Plain Old Unit Testing Test-Last Test-Driven Development (TDD) Test-First Tuesday, 18 September 12
  • 4. Test-Driven Development (TDD) Test Code Refactor Tuesday, 18 September 12
  • 5. Development Style Keep It Simple, Stupid (KISS) You ain’t gonna need it (YAGNI) Fake it till you make it Tuesday, 18 September 12
  • 6. Unit Test Tuesday, 18 September 12
  • 7. QUnit QUnit is a powerful, easy-to-use JavaScript unit test suite. It's used by the jQuery, jQuery UI and jQuery Mobile projects and is capable of testing any generic JavaScript code, including itself! http://qunitjs.com Tuesday, 18 September 12
  • 8. QUnit: Assert ok(truthy [, message]) equal(actual, expected [, message]) deepEqual(actual, expected [, message]) Tuesday, 18 September 12
  • 9. Example Validate Telephone Number Tuesday, 18 September 12
  • 10. QUnit : HTML test suite <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>QUnit Example</title> <link rel="stylesheet" href="/resources/qunit.css"> </head> <body> <div id="qunit"></div> <script src="/resources/qunit.js"></script> <script src="/resources/tests.js"></script> </body> </html> Tuesday, 18 September 12
  • 11. QUnit: Add a test // tests.js // Test module("isPhoneNumber"); test("Wrong number", function () { equal(isPhoneNumber("abcdefg"), false, "All alphabet"); }); Tuesday, 18 September 12
  • 12. QUnit: Run all test and see if the new one fails Tuesday, 18 September 12
  • 13. QUnit: Write some code // Code Under Test isPhoneNumber = function(a) { return false; } // Test module("isPhoneNumber"); test("Wrong number", function () { equal(isPhoneNumber("abcdefg"), false, "All alphabet"); }); Tuesday, 18 September 12
  • 14. QUnit: Run automated tests and see them succeed Tuesday, 18 September 12
  • 15. QUnit: Refactor code // Code Under Test isPhoneNumber = function(input) { return false; } // Test module("isPhoneNumber"); test("Wrong number", function () { equal(isPhoneNumber("abcdefg"), false, "All alphabet"); }); Tuesday, 18 September 12
  • 16. QUnit: Re-running test cases Tuesday, 18 September 12
  • 17. QUnit: Repeat // Code Under Test isPhoneNumber = function(input) { reg = /^d+$/; if (reg.test(input)) { return true; } return false; } // Test module("isPhoneNumber"); test("Wrong number", function () { equal(isPhoneNumber("abcdefg"), false, "All alphabet"); equal(isPhoneNumber("021234567"), true, "input = 021234567"); equal(isPhoneNumber("0819876543"), true, "input = 021234567"); }); Tuesday, 18 September 12
  • 18. Related Topics JsUnit JSMock, JSmockito Selenium JSpec, Jasmine, JSSpec ... Tuesday, 18 September 12