SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Effective TDD
Less is More
Ben Lau
2013
Conversation with a
programmer using TDD
Story (1)
“TDD is really excellent”
“It helps me to fix a lot of bug
and speed up the development”
The programmer said
Then the programmer ask
Can I ask QA to
write more test
code for me?
TDD is Test Driven
Development
But it is NOT asking you to
write a lot of test code
How deep are your unit tests?
“I get paid for code that works, not for tests, so my philosophy is
to test as little as possible to reach a given level of confidence (I
suspect this level of confidence is high compared to industry
standards, but that could just be hubris). If I don't typically make
a kind of mistake (like setting the wrong variables in a
constructor), I don't test for it. I do tend to make sense of test
errors, so I'm extra careful when I have logic with complicated
conditionals. When coding on a tea...” [snipped]
by Kent Beck
The creator of TDD
A post in stackoverflow
Source: http://goo.gl/WRmWa
Why we don't write test case
“It is too early! Do it when the project finished!”
“It is working. We don't need test program”
“Let's QA do it!”
“Testing is not real work.”
About 261,000,000 results (0.55 seconds)
At the end , you won't write any testing code
Source : http://goo.gl/cNTmo
In my opinion...
We don't write test code
just because...
It is not funny
Writing test code is boring and useless?
“I have confidence with my code,
why need to write extra code to
prove it work and the result is
just to print “OK”?”
“It is just wasting of time. ”
However , it is not rare
Automated testing code is necessary but people hate it
Source: http://goo.gl/4QrBD
Make it fun by make it fail
Enjoy writing testing code – Tip 1
Story (2) – Pair programming
Mission
Refactor the program with test code
due to requirement changes
The conversation
Ben : “So this piece of code is wrong?”
Programmer: “NO! It is correct.” (before
requirement change)
Ben : “Nevermind. I will make it wrong no matter
is it working now. (smile)”
So make it fun by make it fail
1) Don't touch the main program
2) Modify the test code according to the
requirement changes
3) Run it
Expected result - Red Light
Compile error / throw undefined
exception
● If it is green (surprise!):
– Your test code is wrong?
● May be you have misunderstood the
requirement
– The code base has supported the
feature already?
● Finished!
– Someone disabled the test code?
● You find a potential problem
Your mission - turn it green
● The short term goal – Fix it.
● It is an achievement
● The definition of “Done” is
clear = Pass the test case.
● Highly focused on what you
are doing
Enjoy writing testing code – Tip 2
Design your program
by write test code first
Just write it?
● Many project / module start without design
documentation and lets programmer “just
write it”.
● Many programmers enjoy working in this
way (before the hell of integration and
testing)
● Sometimes may discover architectural
failure but it is already too late to fix
Design failure?
● Even you have design documentation
– It may not be deep enough
– It may be wrong
● In fact ,the specification / design document and
implementation can be different.
● Design failure is usually discovered in implementation
● Coding is the most effective way to discover design
failure , but the cost of change is very high
Design by writing test code
● Write test code before to implement the main program
● Just like writing pseudo code but executable
● Help you to clarify your intention
– How others use your module?
– Will it be confusing? Too ugly? Too complicated?
– Inconsistent with other module?
– Is it testable?
– Discover design failure by stand in the point of user without real
implementation.
Practice make perfect
var a = [3];
console.log(a.length); //
1
consloe.log(a[0]); //3
var a = new Array(3);
console.log(a.length); //
3
consloe.log(typeof
a[0]); //undefined
● A typical example
code in text book
● Expected output is
written in comment
● Verify the result by
human eye
Turn into test code is easy
test("array" , function() {
var a = [3];
ok(a.length == 1);
ok(a[0] == 3);
var a = new Array(3);
ok(a.length == 3);
ok(typeof a[0] ==
undefined);
});
● Describe your
expectation
(Specification / Design
plan)
● May discover potential
problem in this phase
http://goo.gl/9wiKD
All code examples are embedded
within a test framework
Less is more
Mission
● Implement an user account creation RESTful API but
no document and your boss ask you to do it now.
● It is coded before the real handling code
● Design during coding...
– The name of test case = mission statement
– Try to demonstrate a success and fail case...
– Th URL and method is “POST /account”
– The successful code should be 201 instead of 200
– Fail code : 400
exports.createAccount = function(test) {
// Mission: Implement an account creation call using node.js
var app = express();
app.use(express.bodyParser());
app.post("/account",account.create);
request(app) //"supertest" may simulate HTTP call to express
.post("/account")
.expect(400) // Argument mssing, it should return error.
.end(function(err,res) {
if (err) {test.ok(false,err)}
}); // A simple test case
request(app)
.post("/account")
.set( 'Content-Type', 'application/json' )
.send({ username : "tester" ,
email : "xxx@xxx.com" })
.expect(201) // Expected result
.end(function(err,res) {
if (err) {test.ok(false,err)}
test.done();
});
}
Tests Missed / Hidden Requirements
● Duplicated username and email ?
● Email format checking?
● Fields like birthday, age , gender?
● Verify database record?
● Verify the output content?
● The coverage of test is not 100%
● Create account is a simple task , 100% coverage of test
code may be 10 times more then the real implementation
You don't need 100% coverage in TDD
● 100% coverage of test is simply not possible in
many condition
● The more the coverage, the more the time
consumed. (Usually in exponential rate)
● But a simple success and fail condition is very
easy!
● TDD turn writing test into a design activity (funny)
but not asking to write a lot of test code (very
boring)
Write test code only if needed
● Example
– Design driven
● For new API of module
– Bug driven
● Do it when you got a bug report
● Prevent it happen again
● Enhance the coverage of test
– Complicated code and conditional
Conclusion
The core activity of TDD
● Write test code first
● Write failing test case
Write test code first
● It is not just to verify the work (boring)
● Force developer to clarify their intention
– What are you going to do?
– Verify the design - is it too tight or unfocused?
● You may discover design failure during coding
● If you have coded the feature , you may not want to change it
– Design code in testable way. More easy to debug and
extend
– Helping the team to understand the features
Write failing test case
● Set a short term goal : Make it pass (funny~)
● Limit the scope
● Concentrate your work
The benefit
● You have automated test codes!
● Your code is testable
● Testable code is more easy to debug and
change.
● “Programmers using pure TDD on new
("greenfield") projects reported they only rarely
felt the need to invoke a debugger”. Quoted
from Wikipedia
Thank you
Twitter: @benlaud

Weitere ähnliche Inhalte

Was ist angesagt?

Chrome extensions
Chrome extensions Chrome extensions
Chrome extensions Ahmad Tahhan
 
Introduction to chrome extension development
Introduction to chrome extension developmentIntroduction to chrome extension development
Introduction to chrome extension developmentKAI CHU CHUNG
 
Let’s Build a Chrome Extension
Let’s Build a Chrome ExtensionLet’s Build a Chrome Extension
Let’s Build a Chrome ExtensionPrajaktaLombar
 
Google chrome extension
Google chrome extensionGoogle chrome extension
Google chrome extensionJohnny Kingdom
 
Orange is the new blue: How to port Chrome Extension to Firefox Extension
Orange is the new blue: How to port Chrome Extension to Firefox ExtensionOrange is the new blue: How to port Chrome Extension to Firefox Extension
Orange is the new blue: How to port Chrome Extension to Firefox Extensionchaykaborya
 
Web Components: Web back to future.
Web Components: Web back to future.Web Components: Web back to future.
Web Components: Web back to future.GlobalLogic Ukraine
 
BP101: A Modernized Workflow w/ Domino/XPages
BP101: A Modernized Workflow w/ Domino/XPagesBP101: A Modernized Workflow w/ Domino/XPages
BP101: A Modernized Workflow w/ Domino/XPagesedm00se
 
JavaScript Introduction
JavaScript IntroductionJavaScript Introduction
JavaScript IntroductionJainul Musani
 
Cloud browser testing with Gradle and Geb
Cloud browser testing with Gradle and GebCloud browser testing with Gradle and Geb
Cloud browser testing with Gradle and GebDavid Carr
 
The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17
The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17
The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17GreeceJS
 
JavaScript Missing Manual, Ch. 1
JavaScript Missing Manual, Ch. 1JavaScript Missing Manual, Ch. 1
JavaScript Missing Manual, Ch. 1Gene Babon
 
Functional testing your Grails app with GEB
Functional testing your Grails app with GEBFunctional testing your Grails app with GEB
Functional testing your Grails app with GEBGR8Conf
 
Taming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebTaming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebC4Media
 
Boost your testing: End-to-End with Docker and Geb
 Boost your testing: End-to-End with Docker and Geb Boost your testing: End-to-End with Docker and Geb
Boost your testing: End-to-End with Docker and GebMarkus Schlichting
 
JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPTGo4Guru
 

Was ist angesagt? (20)

Chrome extensions
Chrome extensionsChrome extensions
Chrome extensions
 
Chrome extensions
Chrome extensions Chrome extensions
Chrome extensions
 
Develop Chrome Extension
Develop Chrome ExtensionDevelop Chrome Extension
Develop Chrome Extension
 
Introduction to chrome extension development
Introduction to chrome extension developmentIntroduction to chrome extension development
Introduction to chrome extension development
 
Let’s Build a Chrome Extension
Let’s Build a Chrome ExtensionLet’s Build a Chrome Extension
Let’s Build a Chrome Extension
 
Google chrome extension
Google chrome extensionGoogle chrome extension
Google chrome extension
 
Orange is the new blue: How to port Chrome Extension to Firefox Extension
Orange is the new blue: How to port Chrome Extension to Firefox ExtensionOrange is the new blue: How to port Chrome Extension to Firefox Extension
Orange is the new blue: How to port Chrome Extension to Firefox Extension
 
Web Components: Web back to future.
Web Components: Web back to future.Web Components: Web back to future.
Web Components: Web back to future.
 
BP101: A Modernized Workflow w/ Domino/XPages
BP101: A Modernized Workflow w/ Domino/XPagesBP101: A Modernized Workflow w/ Domino/XPages
BP101: A Modernized Workflow w/ Domino/XPages
 
JavaScript Introduction
JavaScript IntroductionJavaScript Introduction
JavaScript Introduction
 
Cloud browser testing with Gradle and Geb
Cloud browser testing with Gradle and GebCloud browser testing with Gradle and Geb
Cloud browser testing with Gradle and Geb
 
The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17
The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17
The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17
 
Introduction to polymer project
Introduction to polymer projectIntroduction to polymer project
Introduction to polymer project
 
JavaScript Missing Manual, Ch. 1
JavaScript Missing Manual, Ch. 1JavaScript Missing Manual, Ch. 1
JavaScript Missing Manual, Ch. 1
 
Java script
Java scriptJava script
Java script
 
Functional testing your Grails app with GEB
Functional testing your Grails app with GEBFunctional testing your Grails app with GEB
Functional testing your Grails app with GEB
 
JavaScript - Part-1
JavaScript - Part-1JavaScript - Part-1
JavaScript - Part-1
 
Taming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebTaming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and Geb
 
Boost your testing: End-to-End with Docker and Geb
 Boost your testing: End-to-End with Docker and Geb Boost your testing: End-to-End with Docker and Geb
Boost your testing: End-to-End with Docker and Geb
 
JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPT
 

Ähnlich wie Effective TDD - Less is more

Test Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEMTest Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEMSagar Sane
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven DevelopmentPablo Villar
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
Software Testing Basic Concepts
Software Testing Basic ConceptsSoftware Testing Basic Concepts
Software Testing Basic Conceptswesovi
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your codePascal Larocque
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1Blue Elephant Consulting
 
Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5rtpaem
 
Topic production code
Topic production codeTopic production code
Topic production codeKavi Kumar
 
Azure DevOps Realtime Work Item Sync: the good, the bad, the ugly!
Azure DevOps Realtime Work Item Sync: the good, the bad, the ugly!Azure DevOps Realtime Work Item Sync: the good, the bad, the ugly!
Azure DevOps Realtime Work Item Sync: the good, the bad, the ugly!Lorenzo Barbieri
 
Agile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin NakovAgile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin NakovSvetlin Nakov
 
Sync Workitems between multiple Team Projects #vssatpn
Sync Workitems between multiple Team Projects #vssatpnSync Workitems between multiple Team Projects #vssatpn
Sync Workitems between multiple Team Projects #vssatpnLorenzo Barbieri
 
Test Driven Development and Automation
Test Driven Development and AutomationTest Driven Development and Automation
Test Driven Development and AutomationMahesh Salaria
 

Ähnlich wie Effective TDD - Less is more (20)

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
That worked before
That worked beforeThat worked before
That worked before
 
Test Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEMTest Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEM
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Software Testing Basic Concepts
Software Testing Basic ConceptsSoftware Testing Basic Concepts
Software Testing Basic Concepts
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your code
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1
 
UPC Plone Testing Talk
UPC Plone Testing TalkUPC Plone Testing Talk
UPC Plone Testing Talk
 
TDD
TDDTDD
TDD
 
Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5
 
Usable Software Design
Usable Software DesignUsable Software Design
Usable Software Design
 
Topic production code
Topic production codeTopic production code
Topic production code
 
Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
 
Azure DevOps Realtime Work Item Sync: the good, the bad, the ugly!
Azure DevOps Realtime Work Item Sync: the good, the bad, the ugly!Azure DevOps Realtime Work Item Sync: the good, the bad, the ugly!
Azure DevOps Realtime Work Item Sync: the good, the bad, the ugly!
 
Agile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin NakovAgile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin Nakov
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Sync Workitems between multiple Team Projects #vssatpn
Sync Workitems between multiple Team Projects #vssatpnSync Workitems between multiple Team Projects #vssatpn
Sync Workitems between multiple Team Projects #vssatpn
 
Test Driven Development and Automation
Test Driven Development and AutomationTest Driven Development and Automation
Test Driven Development and Automation
 
Tdd
TddTdd
Tdd
 

Kürzlich hochgeladen

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 

Kürzlich hochgeladen (20)

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 

Effective TDD - Less is more

  • 1. Effective TDD Less is More Ben Lau 2013
  • 2. Conversation with a programmer using TDD Story (1)
  • 3. “TDD is really excellent” “It helps me to fix a lot of bug and speed up the development” The programmer said
  • 4. Then the programmer ask Can I ask QA to write more test code for me?
  • 5. TDD is Test Driven Development But it is NOT asking you to write a lot of test code
  • 6. How deep are your unit tests? “I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence (I suspect this level of confidence is high compared to industry standards, but that could just be hubris). If I don't typically make a kind of mistake (like setting the wrong variables in a constructor), I don't test for it. I do tend to make sense of test errors, so I'm extra careful when I have logic with complicated conditionals. When coding on a tea...” [snipped] by Kent Beck The creator of TDD A post in stackoverflow Source: http://goo.gl/WRmWa
  • 7. Why we don't write test case “It is too early! Do it when the project finished!” “It is working. We don't need test program” “Let's QA do it!” “Testing is not real work.” About 261,000,000 results (0.55 seconds)
  • 8. At the end , you won't write any testing code Source : http://goo.gl/cNTmo
  • 9. In my opinion... We don't write test code just because... It is not funny
  • 10. Writing test code is boring and useless? “I have confidence with my code, why need to write extra code to prove it work and the result is just to print “OK”?” “It is just wasting of time. ”
  • 11. However , it is not rare Automated testing code is necessary but people hate it Source: http://goo.gl/4QrBD
  • 12. Make it fun by make it fail Enjoy writing testing code – Tip 1
  • 13. Story (2) – Pair programming Mission Refactor the program with test code due to requirement changes
  • 14. The conversation Ben : “So this piece of code is wrong?” Programmer: “NO! It is correct.” (before requirement change) Ben : “Nevermind. I will make it wrong no matter is it working now. (smile)”
  • 15. So make it fun by make it fail 1) Don't touch the main program 2) Modify the test code according to the requirement changes 3) Run it
  • 16. Expected result - Red Light Compile error / throw undefined exception ● If it is green (surprise!): – Your test code is wrong? ● May be you have misunderstood the requirement – The code base has supported the feature already? ● Finished! – Someone disabled the test code? ● You find a potential problem
  • 17. Your mission - turn it green ● The short term goal – Fix it. ● It is an achievement ● The definition of “Done” is clear = Pass the test case. ● Highly focused on what you are doing
  • 18. Enjoy writing testing code – Tip 2 Design your program by write test code first
  • 19. Just write it? ● Many project / module start without design documentation and lets programmer “just write it”. ● Many programmers enjoy working in this way (before the hell of integration and testing) ● Sometimes may discover architectural failure but it is already too late to fix
  • 20. Design failure? ● Even you have design documentation – It may not be deep enough – It may be wrong ● In fact ,the specification / design document and implementation can be different. ● Design failure is usually discovered in implementation ● Coding is the most effective way to discover design failure , but the cost of change is very high
  • 21. Design by writing test code ● Write test code before to implement the main program ● Just like writing pseudo code but executable ● Help you to clarify your intention – How others use your module? – Will it be confusing? Too ugly? Too complicated? – Inconsistent with other module? – Is it testable? – Discover design failure by stand in the point of user without real implementation.
  • 22. Practice make perfect var a = [3]; console.log(a.length); // 1 consloe.log(a[0]); //3 var a = new Array(3); console.log(a.length); // 3 consloe.log(typeof a[0]); //undefined ● A typical example code in text book ● Expected output is written in comment ● Verify the result by human eye
  • 23. Turn into test code is easy test("array" , function() { var a = [3]; ok(a.length == 1); ok(a[0] == 3); var a = new Array(3); ok(a.length == 3); ok(typeof a[0] == undefined); }); ● Describe your expectation (Specification / Design plan) ● May discover potential problem in this phase http://goo.gl/9wiKD All code examples are embedded within a test framework
  • 25. Mission ● Implement an user account creation RESTful API but no document and your boss ask you to do it now. ● It is coded before the real handling code ● Design during coding... – The name of test case = mission statement – Try to demonstrate a success and fail case... – Th URL and method is “POST /account” – The successful code should be 201 instead of 200 – Fail code : 400
  • 26. exports.createAccount = function(test) { // Mission: Implement an account creation call using node.js var app = express(); app.use(express.bodyParser()); app.post("/account",account.create); request(app) //"supertest" may simulate HTTP call to express .post("/account") .expect(400) // Argument mssing, it should return error. .end(function(err,res) { if (err) {test.ok(false,err)} }); // A simple test case request(app) .post("/account") .set( 'Content-Type', 'application/json' ) .send({ username : "tester" , email : "xxx@xxx.com" }) .expect(201) // Expected result .end(function(err,res) { if (err) {test.ok(false,err)} test.done(); }); }
  • 27. Tests Missed / Hidden Requirements ● Duplicated username and email ? ● Email format checking? ● Fields like birthday, age , gender? ● Verify database record? ● Verify the output content? ● The coverage of test is not 100% ● Create account is a simple task , 100% coverage of test code may be 10 times more then the real implementation
  • 28. You don't need 100% coverage in TDD ● 100% coverage of test is simply not possible in many condition ● The more the coverage, the more the time consumed. (Usually in exponential rate) ● But a simple success and fail condition is very easy! ● TDD turn writing test into a design activity (funny) but not asking to write a lot of test code (very boring)
  • 29. Write test code only if needed ● Example – Design driven ● For new API of module – Bug driven ● Do it when you got a bug report ● Prevent it happen again ● Enhance the coverage of test – Complicated code and conditional
  • 31. The core activity of TDD ● Write test code first ● Write failing test case
  • 32. Write test code first ● It is not just to verify the work (boring) ● Force developer to clarify their intention – What are you going to do? – Verify the design - is it too tight or unfocused? ● You may discover design failure during coding ● If you have coded the feature , you may not want to change it – Design code in testable way. More easy to debug and extend – Helping the team to understand the features
  • 33. Write failing test case ● Set a short term goal : Make it pass (funny~) ● Limit the scope ● Concentrate your work
  • 34. The benefit ● You have automated test codes! ● Your code is testable ● Testable code is more easy to debug and change. ● “Programmers using pure TDD on new ("greenfield") projects reported they only rarely felt the need to invoke a debugger”. Quoted from Wikipedia