SlideShare a Scribd company logo
1 of 13
Unit Testing
the regression shield
Unit Testing, Node.JS style



What is unit testing
“What to test”, not “how to test”
Vows, NodeMock, and CommonSense™
Vows


Use vows properly
Run the test suite runner BEFORE pull requests
Check confessions server AFTER pull request
integration
Test Suite Runner


$ bin/fo test
$ bin/fo test -v (verbose)
$ bin/fo --help (kills nyancats)
Test it out...
Writing a test case

Config Object
Goal:
 Load contents from a config file
 Parse contents and return as a JSON string
 Should handle errors gracefully
See wiki/testing-example1-basic
Writing a test case


What’s the problem here?
 1 - Depends on files
 2 - Hard to setup test environment
 3 - It’s NOT unit testing
Mocks


Control Environment
Run It Anywhere™
Mock objects to the rescue!
See wiki/testing-example2-mock
NodeMock


https://github.com/arunoda/nodemock
Mocking solution made easy
No need to rewrite code for every single test
context
See wiki/testing-example3-nodemock
NodeMock


Mocking objects in test cases are messy
How to re-use mock objects
When to re-use mock objects
When NOT to re-use mock objects
Fixtures

Control environment
“Clean Slate” to run tests on
No external dependencies
Injects dependencies
Used in manufacturing’s testing procedures
See wiki/testing-example4-fixtures
Dependency Injection

WTF is DI?
Inject dependency vs Encapsulated dependency
Constructor injection, Getter/setter injection,
Service Locator, Factory & DI Containers
Dependency Injection = Easier to test
Use FO(‘...’) to auto-construct components
Code Samples


Config File: http://pastebin.com/N65mt2eh
Simple Test: http://pastebin.com/Df7ysdEH
JSON File: http://pastebin.com/Qbt52Cba
With Mock: http://pastebin.com/G0812kxz
NodeMock: http://pastebin.com/swGR379h
Fixture: http://pastebin.com/f4daFf5Z
-Q&A-

More Related Content

What's hot

Interaction testing using mock objects
Interaction testing using mock objectsInteraction testing using mock objects
Interaction testing using mock objects
Lim Chanmann
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
Richard Paul
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
Alex Su
 
GeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testingGeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testing
Jakub Marchwicki
 

What's hot (20)

Interaction testing using mock objects
Interaction testing using mock objectsInteraction testing using mock objects
Interaction testing using mock objects
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Unit testing with Spock Framework
Unit testing with Spock FrameworkUnit testing with Spock Framework
Unit testing with Spock Framework
 
Unit testing with java
Unit testing with javaUnit testing with java
Unit testing with java
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
Generamba
GenerambaGeneramba
Generamba
 
Pi j4.2 software-reliability
Pi j4.2 software-reliabilityPi j4.2 software-reliability
Pi j4.2 software-reliability
 
Scripting robot
Scripting robotScripting robot
Scripting robot
 
Erlang Common tests - Sergey Bondarchuk
Erlang Common tests  - Sergey BondarchukErlang Common tests  - Sergey Bondarchuk
Erlang Common tests - Sergey Bondarchuk
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
 
GeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testingGeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testing
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
The state of JavaScript Linting - English version
The state of JavaScript Linting - English versionThe state of JavaScript Linting - English version
The state of JavaScript Linting - English version
 
S2 e (selective symbolic execution) -shivkrishna a
S2 e (selective symbolic execution) -shivkrishna aS2 e (selective symbolic execution) -shivkrishna a
S2 e (selective symbolic execution) -shivkrishna a
 
Selenium TestNG
Selenium TestNGSelenium TestNG
Selenium TestNG
 
Tdd & unit test
Tdd & unit testTdd & unit test
Tdd & unit test
 
Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8
 
Introduction to Binary Exploitation
Introduction to Binary Exploitation	Introduction to Binary Exploitation
Introduction to Binary Exploitation
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 

Similar to Unit testing for 40 square software

Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
Ortus Solutions, Corp
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
Joe Wilson
 
Testing for Pragmatic People
Testing for Pragmatic PeopleTesting for Pragmatic People
Testing for Pragmatic People
davismr
 

Similar to Unit testing for 40 square software (20)

Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!
 
Plone Conference 2007: Acceptance Testing In Plone Using Funittest - Maik Röder
Plone Conference 2007: Acceptance Testing In Plone Using Funittest - Maik RöderPlone Conference 2007: Acceptance Testing In Plone Using Funittest - Maik Röder
Plone Conference 2007: Acceptance Testing In Plone Using Funittest - Maik Röder
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
unit test in node js - test cases in node
unit test in node js - test cases in nodeunit test in node js - test cases in node
unit test in node js - test cases in node
 
Gallio Crafting A Toolchain
Gallio Crafting A ToolchainGallio Crafting A Toolchain
Gallio Crafting A Toolchain
 
Testdriven Development using JUnit and EasyMock
Testdriven Development using JUnit and EasyMockTestdriven Development using JUnit and EasyMock
Testdriven Development using JUnit and EasyMock
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
 
Testing w-mocks
Testing w-mocksTesting w-mocks
Testing w-mocks
 
Getting Started with Selenium
Getting Started with SeleniumGetting Started with Selenium
Getting Started with Selenium
 
Writing useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildWriting useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you build
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class Design
 
Unit testing
Unit testingUnit testing
Unit testing
 
Creating Realistic Unit Tests with Testcontainers
Creating Realistic Unit Tests with TestcontainersCreating Realistic Unit Tests with Testcontainers
Creating Realistic Unit Tests with Testcontainers
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Simple testable code
Simple testable codeSimple testable code
Simple testable code
 
Nguyenvandungb seminar
Nguyenvandungb seminarNguyenvandungb seminar
Nguyenvandungb seminar
 
TDD - Test Driven Dvelopment | Test First Design
TDD -  Test Driven Dvelopment | Test First DesignTDD -  Test Driven Dvelopment | Test First Design
TDD - Test Driven Dvelopment | Test First Design
 
Testing 101
Testing 101Testing 101
Testing 101
 
DevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan Roman
DevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan RomanDevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan Roman
DevSecCon Tel Aviv 2018 - Integrated Security Testing by Morgan Roman
 
Testing for Pragmatic People
Testing for Pragmatic PeopleTesting for Pragmatic People
Testing for Pragmatic People
 

More from Ruben Tan

Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromq
Ruben Tan
 
40 square's git workflow
40 square's git workflow40 square's git workflow
40 square's git workflow
Ruben Tan
 

More from Ruben Tan (11)

Basic distributed systems principles
Basic distributed systems principlesBasic distributed systems principles
Basic distributed systems principles
 
Demystifying blockchains
Demystifying blockchainsDemystifying blockchains
Demystifying blockchains
 
Banking on blockchains
Banking on blockchainsBanking on blockchains
Banking on blockchains
 
Consensus in distributed computing
Consensus in distributed computingConsensus in distributed computing
Consensus in distributed computing
 
Leveraging zeromq for node.js
Leveraging zeromq for node.jsLeveraging zeromq for node.js
Leveraging zeromq for node.js
 
Defensive programming in Javascript and Node.js
Defensive programming in Javascript and Node.jsDefensive programming in Javascript and Node.js
Defensive programming in Javascript and Node.js
 
Client-side storage
Client-side storageClient-side storage
Client-side storage
 
Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromq
 
How we git - commit policy and code review
How we git - commit policy and code reviewHow we git - commit policy and code review
How we git - commit policy and code review
 
NodeHack #2 - MVP
NodeHack #2 - MVPNodeHack #2 - MVP
NodeHack #2 - MVP
 
40 square's git workflow
40 square's git workflow40 square's git workflow
40 square's git workflow
 

Recently uploaded

Recently uploaded (20)

[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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
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...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Unit testing for 40 square software

  • 2. Unit Testing, Node.JS style What is unit testing “What to test”, not “how to test” Vows, NodeMock, and CommonSense™
  • 3. Vows Use vows properly Run the test suite runner BEFORE pull requests Check confessions server AFTER pull request integration
  • 4. Test Suite Runner $ bin/fo test $ bin/fo test -v (verbose) $ bin/fo --help (kills nyancats) Test it out...
  • 5. Writing a test case Config Object Goal: Load contents from a config file Parse contents and return as a JSON string Should handle errors gracefully See wiki/testing-example1-basic
  • 6. Writing a test case What’s the problem here? 1 - Depends on files 2 - Hard to setup test environment 3 - It’s NOT unit testing
  • 7. Mocks Control Environment Run It Anywhere™ Mock objects to the rescue! See wiki/testing-example2-mock
  • 8. NodeMock https://github.com/arunoda/nodemock Mocking solution made easy No need to rewrite code for every single test context See wiki/testing-example3-nodemock
  • 9. NodeMock Mocking objects in test cases are messy How to re-use mock objects When to re-use mock objects When NOT to re-use mock objects
  • 10. Fixtures Control environment “Clean Slate” to run tests on No external dependencies Injects dependencies Used in manufacturing’s testing procedures See wiki/testing-example4-fixtures
  • 11. Dependency Injection WTF is DI? Inject dependency vs Encapsulated dependency Constructor injection, Getter/setter injection, Service Locator, Factory & DI Containers Dependency Injection = Easier to test Use FO(‘...’) to auto-construct components
  • 12. Code Samples Config File: http://pastebin.com/N65mt2eh Simple Test: http://pastebin.com/Df7ysdEH JSON File: http://pastebin.com/Qbt52Cba With Mock: http://pastebin.com/G0812kxz NodeMock: http://pastebin.com/swGR379h Fixture: http://pastebin.com/f4daFf5Z
  • 13. -Q&A-

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n