SlideShare ist ein Scribd-Unternehmen logo
1 von 41
§TDD training
1. Overview
2. Fixtures
3. Assertion patterns
4. Test quality
5. Case study
Agenda
§Motivation
The Real Life Software Challenge
Disadvantages Technical Debt
Traditional form of development
Requirements Code Test
Motivation - solving the right problem right
Creating poorly written code
 High defect rates
 Defects make systems unstable, unpredictable, unusable
 Cost of fixing defects caught during testing is typically a
magnitude or two higher than those caught when
introduced into the code base
Motivation - solving the right problem right
 Lack of maintainability
 Code difficult to understand and change
 Break functionality elsewhere in the system
 Duplication
Failing to meet actual client needs!
Test-Driven Development
 Learn how to build the thing right - TDD - internal
quality
 Learn how to build the right thing - Acceptance TDD -
external quality
Builditright - TDD
Buildtheright thing: acceptance TDD
§What is TDD?
TDD Cycle
Recommendations
• Do not skip refactoring
• Get to green fast
• Slow down after a mistake
FIRST
Fast – run (subset of) tests quickly
Isolated – no tests depend on others
Repeatable – run N times, get same result
Self-validating – tests can automatically detect if passed
Timely – written about the same time as code under test
How to select the next test
• Going for the highest value vs picking the low-hanging fruit first
• Treading the happy path first vs starting with handling error situations
• Diving first into details vs starting with the big picture
• Exploring uncertain territory vs sticking with the familiar and comfortable
Implementation strategies
• Faking it - fake a particular functionality to get back to green
• Triangulation - each of the tests constrains the available solution space to one
dimension, thus they narrow down (”triangulate”) the solution
• Obvious implementation
AAA Test Format
@Test
public void sizeOfListReflectsItemsAddedToIt() throws Exception {
List<String> list = new ArrayList<String>(); ARRANGE
list.add("something"); ACT
assertEquals(1, list.size()); ASSERT
}
Exercise 1 – Tax Calculator
Given an amount of taxable income, calculate the net
pay according to the following rules:
• First £11,500 are tax-free
• 20% for £11,500 to £45,000
• 40% for £45,000 to £150,000
• 45% for over £150,000
§
Fixtures
Fixtures
A fixture is a fixed state of a set of objects used as a baseline for running tests.
Useful to:
• Remove duplication
• Allow for more focused tests
Used for state-based tests.
Test doubles
Are objects that stand in for the real thing. They pretend to be something
they’re not, so that the client has no idea, and they typically do it faster than
the real thing.
Dummy A null or dummy object to be used as parameter value when test
doesn’t care
Stub Class with test-specific implementation (hard-coded responses)
Fake A full implementation for test purposes (e.g. in-memory
database)
Mock An interface with expectations set for the test (implementation
generated at runtime by mocking framework) – interaction-
based test
Parameterized creation pattern
Hides the non-important attributes from the setup by extracting the object
creation into a separate creation method, which takes the variable attributes as
arguments and populates the non-important ones with constant or random
values. Our test will populate only the important attributes.
Object mother pattern
Is a sophisticated object factory that provides access to a complete object graph
of valid domain objects, including instances in different states.
- Makes it easy for new developers to write tests
- Powerful when integrated with personas
§
Assertion patterns
Resulting-State Assertion
Exercise some functionality on the fixture objects and afterward assert that the
internal state of the fixture objects match our expectations.
@Test
public void sizeOfListReflectsItemsAddedToIt() throws Exception {
List<String> list = new ArrayList<String>();
list.add("something");
assertEquals(1, list.size()); // state verification
}
Guard Assertion
Make explicit the assumptions made about the fixture right before invoking the
functionality we want to test.
@Test
public void listIsNoLongerEmptyAfterAddingAnItemToIt() throws Exception {
List<String> list = new ArrayList<String>();
assertTrue(list.isEmpty()); // guard assertion
list.add("something");
assertFalse(list.isEmpty()); // state verification
}
Delta Assertion
Test that the difference(delta) between the initial and after states is what we
expect.
private ArrayList<String> list;
@Test
public void sizeOfListReflectsItemsAddedToIt() throws Exception {
int sizeBefore = list.size(); // record the "before" state
list.add("something");
assertEquals(sizeBefore + 1, list.size()); // delta verification
}
Custom Assertion
When the amount of code verifying our expectations vastly exceeds the amount
of code required to invoke the code under test, extract it from the test in order
to encapsulate complex verification logic.
@Test
public void timeslotsAreOnWeekdays() throws Exception {
MeetingCalendar calendar = new MeetingCalendar();
Date time = calendar.nextAvailableStartingTime();
assertIsDuringOfficeHoursOnWeekday(time);}
private void assertIsDuringOfficeHoursOnWeekday(Date time) { … }
Interaction Assertion
Verify that our code
interacts with its
collaborator objects
as we expect it to.
Exercise 2 – Word Wrapper
Develop a word-wrapping algorithm, which is given a string and
a row-length, it returns a list of word-wrapped-rows.
Examples of behaviour:
• If the row-length is 60, and the input string is 30, the result is
just the input string
• If the row-length is 3, and the input string is "abc def" the
result is "abc", "def”
• If the row-length is 3, and the input string is "abcdef" the
result is "abc", "def”
• If the row-length is 3, and the input string is "abcdef abc" the
result is "abc", "def", "abc”
• With row length 3 and "a b c d e f" the result is "a b", "c d",
"e f"
§
Are my tests good quality?
Mutation testing
Principle: change (mutate) certain statements in the source code and check if the
test cases are able to find the errors
- Mutant generation is time-consuming and difficult, so it was automated
- If the tests detect the mutant, then it’s ‘killed’
- Surviving mutants show not tested cases
mutation score = number of mutants killed / total number of mutants
Mutation testing types
Value Mutations: values of primary parameters are modified (e.g. change constants)
Decision Mutations: change decisions/conditions to check for design errors (e.g.
arithmetic, logical or relational operators)
Statement Mutations: changes done to the statements by deleting or duplicating the
line (e.g. copy/paste)
§
How effective is TDD?
Case Study
Participants
 1 development team from IBM
 3 development teams from Microsoft
IBM Team
 Products: device drivers
 Legacy product
 New product using a new platform
 Comparison between 7th release of legacy product and 1st
release of new product
 The two products exposed the same interfaces for the same set
of devices
Microsoft Teams
 Products: Windows, MSN, Visual Studio
 Windows: Networking team - networking common library written as
a reusable set of modules for different projects
 MSN: Web services application
 Developer Division: part of the Visual Studio (VS) software
development system
Metrics IBM
(legacy+new)
Ms Windows Ms MSN Ms Vista
Defect density not
using TDD
w x y z
Defect density using
TDD
0.6w 0.38x 0.24 y 0.19z
Increase in time to
code
15-20% 25-35% 15% 25-20%
Study Results
Exercise 3 – Berlin Clock
- the top yellow lamp blinks to
show seconds - on for even
seconds and off for odd seconds
- the next two rows represent
hours
- the upper row has 5 hour blocks
and is made up of 4 red lamps
- the lower row has 1 hour blocks
and is also made up of 4 red
lamps
- the final two rows represent the
minutes
- the upper row has 5 minute
blocks, and is made up of 11
lamps, every 3rd lamp is red, the
rest are yellow
- the bottom row has 1 minute
blocks, and is made up of 4
yellow lamps
TDD Advantages
TDD
Encourages
good design
Prevents over-
engineering
because of flawed
assumptions
Documents how
the code is
intended to work
Gradually
enhances a
working product
Enables effective
learning through early
feedback from client
Less time spent
fixing defects
Produces
testable code
Reduces bugs
with up to 90%
References
References
• https://people.engr.ncsu.edu/gjin2/Classes/5
91/Spring2017/case-tdd-b.pdf
• http://junit.org/junit5/docs/current/user-
guide/
• http://www.guru99.com/mutation-
testing.html
§
Thanks!

Weitere ähnliche Inhalte

Was ist angesagt?

(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and backDavid Rodenas
 
TDD CrashCourse Part4: Improving Testing
TDD CrashCourse Part4: Improving TestingTDD CrashCourse Part4: Improving Testing
TDD CrashCourse Part4: Improving TestingDavid Rodenas
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarROHIT JAISWAR
 
Cracking OCA and OCP Java 8 Exams
Cracking OCA and OCP Java 8 ExamsCracking OCA and OCP Java 8 Exams
Cracking OCA and OCP Java 8 ExamsGanesh Samarthyam
 
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMEREVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMERAndrey Karpov
 
TDD CrashCourse Part5: Testing Techniques
TDD CrashCourse Part5: Testing TechniquesTDD CrashCourse Part5: Testing Techniques
TDD CrashCourse Part5: Testing TechniquesDavid Rodenas
 
Test in action – week 1
Test in action – week 1Test in action – week 1
Test in action – week 1Yi-Huan Chan
 
ReactJS for Programmers
ReactJS for ProgrammersReactJS for Programmers
ReactJS for ProgrammersDavid Rodenas
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smellsPaul Nguyen
 
Unit testing with Spock Framework
Unit testing with Spock FrameworkUnit testing with Spock Framework
Unit testing with Spock FrameworkEugene Dvorkin
 
Example First / A Sane Test-Driven Approach to Programming
Example First / A Sane Test-Driven Approach to ProgrammingExample First / A Sane Test-Driven Approach to Programming
Example First / A Sane Test-Driven Approach to ProgrammingJonathan Acker
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkHumberto Marchezi
 
Smarter Testing With Spock
Smarter Testing With SpockSmarter Testing With Spock
Smarter Testing With SpockIT Weekend
 
Java best practices
Java best practicesJava best practices
Java best practicesRay Toal
 
ES3-2020-06 Test Driven Development (TDD)
ES3-2020-06 Test Driven Development (TDD)ES3-2020-06 Test Driven Development (TDD)
ES3-2020-06 Test Driven Development (TDD)David Rodenas
 
Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupDror Helper
 

Was ist angesagt? (20)

(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back
 
TDD CrashCourse Part4: Improving Testing
TDD CrashCourse Part4: Improving TestingTDD CrashCourse Part4: Improving Testing
TDD CrashCourse Part4: Improving Testing
 
Java Lab Manual
Java Lab ManualJava Lab Manual
Java Lab Manual
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
Cracking OCA and OCP Java 8 Exams
Cracking OCA and OCP Java 8 ExamsCracking OCA and OCP Java 8 Exams
Cracking OCA and OCP Java 8 Exams
 
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMEREVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
 
TDD CrashCourse Part5: Testing Techniques
TDD CrashCourse Part5: Testing TechniquesTDD CrashCourse Part5: Testing Techniques
TDD CrashCourse Part5: Testing Techniques
 
Test in action – week 1
Test in action – week 1Test in action – week 1
Test in action – week 1
 
ReactJS for Programmers
ReactJS for ProgrammersReactJS for Programmers
ReactJS for Programmers
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smells
 
Introduction to Java Programming Part 2
Introduction to Java Programming Part 2Introduction to Java Programming Part 2
Introduction to Java Programming Part 2
 
Unit testing with Spock Framework
Unit testing with Spock FrameworkUnit testing with Spock Framework
Unit testing with Spock Framework
 
Spock Framework
Spock FrameworkSpock Framework
Spock Framework
 
Java ppt
Java pptJava ppt
Java ppt
 
Example First / A Sane Test-Driven Approach to Programming
Example First / A Sane Test-Driven Approach to ProgrammingExample First / A Sane Test-Driven Approach to Programming
Example First / A Sane Test-Driven Approach to Programming
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
 
Smarter Testing With Spock
Smarter Testing With SpockSmarter Testing With Spock
Smarter Testing With Spock
 
Java best practices
Java best practicesJava best practices
Java best practices
 
ES3-2020-06 Test Driven Development (TDD)
ES3-2020-06 Test Driven Development (TDD)ES3-2020-06 Test Driven Development (TDD)
ES3-2020-06 Test Driven Development (TDD)
 
Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet Soup
 

Ähnlich wie TDD Training

utPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQLutPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQLSteven Feuerstein
 
Shift-Left Testing: QA in a DevOps World by David Laulusa
Shift-Left Testing: QA in a DevOps World by David LaulusaShift-Left Testing: QA in a DevOps World by David Laulusa
Shift-Left Testing: QA in a DevOps World by David LaulusaQA or the Highway
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnitAktuğ Urun
 
SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)Amr E. Mohamed
 
DSR Testing (Part 1)
DSR Testing (Part 1)DSR Testing (Part 1)
DSR Testing (Part 1)Steve Upton
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentAll Things Open
 
Effective Unit Test Style Guide
Effective Unit Test Style GuideEffective Unit Test Style Guide
Effective Unit Test Style GuideJacky Lai
 
Introduzione allo Unit Testing
Introduzione allo Unit TestingIntroduzione allo Unit Testing
Introduzione allo Unit TestingStefano Ottaviani
 
LecccccccccccccProgrammingLecture-09.pdf
LecccccccccccccProgrammingLecture-09.pdfLecccccccccccccProgrammingLecture-09.pdf
LecccccccccccccProgrammingLecture-09.pdfAmirMohamedNabilSale
 

Ähnlich wie TDD Training (20)

Arrays
ArraysArrays
Arrays
 
utPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQLutPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQL
 
Shift-Left Testing: QA in a DevOps World by David Laulusa
Shift-Left Testing: QA in a DevOps World by David LaulusaShift-Left Testing: QA in a DevOps World by David Laulusa
Shift-Left Testing: QA in a DevOps World by David Laulusa
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
11 whiteboxtesting
11 whiteboxtesting11 whiteboxtesting
11 whiteboxtesting
 
Test Driven
Test DrivenTest Driven
Test Driven
 
SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)
 
DSR Testing (Part 1)
DSR Testing (Part 1)DSR Testing (Part 1)
DSR Testing (Part 1)
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
Unit testing
Unit testingUnit testing
Unit testing
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Testing
TestingTesting
Testing
 
AutoTest.ppt
AutoTest.pptAutoTest.ppt
AutoTest.ppt
 
AutoTest.ppt
AutoTest.pptAutoTest.ppt
AutoTest.ppt
 
AutoTest.ppt
AutoTest.pptAutoTest.ppt
AutoTest.ppt
 
Effective Unit Test Style Guide
Effective Unit Test Style GuideEffective Unit Test Style Guide
Effective Unit Test Style Guide
 
Test Levels & Techniques
Test Levels & TechniquesTest Levels & Techniques
Test Levels & Techniques
 
Introduzione allo Unit Testing
Introduzione allo Unit TestingIntroduzione allo Unit Testing
Introduzione allo Unit Testing
 
LecccccccccccccProgrammingLecture-09.pdf
LecccccccccccccProgrammingLecture-09.pdfLecccccccccccccProgrammingLecture-09.pdf
LecccccccccccccProgrammingLecture-09.pdf
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 

Kürzlich hochgeladen

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
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 DevelopmentsTrustArc
 
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...Miguel Araújo
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
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
 
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?Igalia
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 

Kürzlich hochgeladen (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
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...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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
 
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?
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

TDD Training

  • 2. 1. Overview 2. Fixtures 3. Assertion patterns 4. Test quality 5. Case study Agenda
  • 4. The Real Life Software Challenge Disadvantages Technical Debt Traditional form of development Requirements Code Test
  • 5. Motivation - solving the right problem right Creating poorly written code  High defect rates  Defects make systems unstable, unpredictable, unusable  Cost of fixing defects caught during testing is typically a magnitude or two higher than those caught when introduced into the code base
  • 6. Motivation - solving the right problem right  Lack of maintainability  Code difficult to understand and change  Break functionality elsewhere in the system  Duplication Failing to meet actual client needs!
  • 7. Test-Driven Development  Learn how to build the thing right - TDD - internal quality  Learn how to build the right thing - Acceptance TDD - external quality
  • 8. Builditright - TDD Buildtheright thing: acceptance TDD
  • 11. Recommendations • Do not skip refactoring • Get to green fast • Slow down after a mistake
  • 12. FIRST Fast – run (subset of) tests quickly Isolated – no tests depend on others Repeatable – run N times, get same result Self-validating – tests can automatically detect if passed Timely – written about the same time as code under test
  • 13. How to select the next test • Going for the highest value vs picking the low-hanging fruit first • Treading the happy path first vs starting with handling error situations • Diving first into details vs starting with the big picture • Exploring uncertain territory vs sticking with the familiar and comfortable
  • 14. Implementation strategies • Faking it - fake a particular functionality to get back to green • Triangulation - each of the tests constrains the available solution space to one dimension, thus they narrow down (”triangulate”) the solution • Obvious implementation
  • 15. AAA Test Format @Test public void sizeOfListReflectsItemsAddedToIt() throws Exception { List<String> list = new ArrayList<String>(); ARRANGE list.add("something"); ACT assertEquals(1, list.size()); ASSERT }
  • 16. Exercise 1 – Tax Calculator Given an amount of taxable income, calculate the net pay according to the following rules: • First £11,500 are tax-free • 20% for £11,500 to £45,000 • 40% for £45,000 to £150,000 • 45% for over £150,000
  • 18. Fixtures A fixture is a fixed state of a set of objects used as a baseline for running tests. Useful to: • Remove duplication • Allow for more focused tests Used for state-based tests.
  • 19. Test doubles Are objects that stand in for the real thing. They pretend to be something they’re not, so that the client has no idea, and they typically do it faster than the real thing. Dummy A null or dummy object to be used as parameter value when test doesn’t care Stub Class with test-specific implementation (hard-coded responses) Fake A full implementation for test purposes (e.g. in-memory database) Mock An interface with expectations set for the test (implementation generated at runtime by mocking framework) – interaction- based test
  • 20. Parameterized creation pattern Hides the non-important attributes from the setup by extracting the object creation into a separate creation method, which takes the variable attributes as arguments and populates the non-important ones with constant or random values. Our test will populate only the important attributes.
  • 21. Object mother pattern Is a sophisticated object factory that provides access to a complete object graph of valid domain objects, including instances in different states. - Makes it easy for new developers to write tests - Powerful when integrated with personas
  • 23. Resulting-State Assertion Exercise some functionality on the fixture objects and afterward assert that the internal state of the fixture objects match our expectations. @Test public void sizeOfListReflectsItemsAddedToIt() throws Exception { List<String> list = new ArrayList<String>(); list.add("something"); assertEquals(1, list.size()); // state verification }
  • 24. Guard Assertion Make explicit the assumptions made about the fixture right before invoking the functionality we want to test. @Test public void listIsNoLongerEmptyAfterAddingAnItemToIt() throws Exception { List<String> list = new ArrayList<String>(); assertTrue(list.isEmpty()); // guard assertion list.add("something"); assertFalse(list.isEmpty()); // state verification }
  • 25. Delta Assertion Test that the difference(delta) between the initial and after states is what we expect. private ArrayList<String> list; @Test public void sizeOfListReflectsItemsAddedToIt() throws Exception { int sizeBefore = list.size(); // record the "before" state list.add("something"); assertEquals(sizeBefore + 1, list.size()); // delta verification }
  • 26. Custom Assertion When the amount of code verifying our expectations vastly exceeds the amount of code required to invoke the code under test, extract it from the test in order to encapsulate complex verification logic. @Test public void timeslotsAreOnWeekdays() throws Exception { MeetingCalendar calendar = new MeetingCalendar(); Date time = calendar.nextAvailableStartingTime(); assertIsDuringOfficeHoursOnWeekday(time);} private void assertIsDuringOfficeHoursOnWeekday(Date time) { … }
  • 27. Interaction Assertion Verify that our code interacts with its collaborator objects as we expect it to.
  • 28. Exercise 2 – Word Wrapper Develop a word-wrapping algorithm, which is given a string and a row-length, it returns a list of word-wrapped-rows. Examples of behaviour: • If the row-length is 60, and the input string is 30, the result is just the input string • If the row-length is 3, and the input string is "abc def" the result is "abc", "def” • If the row-length is 3, and the input string is "abcdef" the result is "abc", "def” • If the row-length is 3, and the input string is "abcdef abc" the result is "abc", "def", "abc” • With row length 3 and "a b c d e f" the result is "a b", "c d", "e f"
  • 29. § Are my tests good quality?
  • 30. Mutation testing Principle: change (mutate) certain statements in the source code and check if the test cases are able to find the errors - Mutant generation is time-consuming and difficult, so it was automated - If the tests detect the mutant, then it’s ‘killed’ - Surviving mutants show not tested cases mutation score = number of mutants killed / total number of mutants
  • 31. Mutation testing types Value Mutations: values of primary parameters are modified (e.g. change constants) Decision Mutations: change decisions/conditions to check for design errors (e.g. arithmetic, logical or relational operators) Statement Mutations: changes done to the statements by deleting or duplicating the line (e.g. copy/paste)
  • 33. Case Study Participants  1 development team from IBM  3 development teams from Microsoft
  • 34. IBM Team  Products: device drivers  Legacy product  New product using a new platform  Comparison between 7th release of legacy product and 1st release of new product  The two products exposed the same interfaces for the same set of devices
  • 35. Microsoft Teams  Products: Windows, MSN, Visual Studio  Windows: Networking team - networking common library written as a reusable set of modules for different projects  MSN: Web services application  Developer Division: part of the Visual Studio (VS) software development system
  • 36. Metrics IBM (legacy+new) Ms Windows Ms MSN Ms Vista Defect density not using TDD w x y z Defect density using TDD 0.6w 0.38x 0.24 y 0.19z Increase in time to code 15-20% 25-35% 15% 25-20% Study Results
  • 37. Exercise 3 – Berlin Clock - the top yellow lamp blinks to show seconds - on for even seconds and off for odd seconds - the next two rows represent hours - the upper row has 5 hour blocks and is made up of 4 red lamps - the lower row has 1 hour blocks and is also made up of 4 red lamps - the final two rows represent the minutes - the upper row has 5 minute blocks, and is made up of 11 lamps, every 3rd lamp is red, the rest are yellow - the bottom row has 1 minute blocks, and is made up of 4 yellow lamps
  • 38. TDD Advantages TDD Encourages good design Prevents over- engineering because of flawed assumptions Documents how the code is intended to work Gradually enhances a working product Enables effective learning through early feedback from client Less time spent fixing defects Produces testable code Reduces bugs with up to 90%