SlideShare a Scribd company logo
1 of 44
Download to read offline
07 Testing - Techniques
Enginyeria del Software 3
1
@drpicox — 2020
Summary
• Levels of Testing

• FIRST properties

• AAA structure

• Code Coverage

• Regression Tests

• Test doubles
2
Summary
• Levels of Testing
• FIRST properties

• AAA structure

• Code Coverage

• Regression Tests

• Test doubles
3
Levels of Testing
• Acceptance Tests

• Programmers Tests
4
Acceptance Tests
• Readable by Stakeholders

• Natural language

• Aligned to Requirements

• High level

• Integrate many levels in the test

• Slow
5
Programmers Tests
• Fast to run

• Quick to code

• Fine grained

• Close to Requirements (Business rules)
6
Levels and TDD
7
Summary
• Levels of Testing

• FIRST properties
• AAA structure

• Code Coverage

• Regression Tests

• Test doubles
8
FIRST properties
• Fast

• Isolates

• Repeatable

• Self-validating

• Timely
9
Fast
• Many hundreds or thousands per second

• Avoid slow logic

• Data bases

• External services

• Intensive computations

• ...
10
Isolates
• Failure reasons become obvious

• Each test runs individually

• No assumed initial state

• Not require other test run first

• Can run alone

• Can run in random order

• new , new , new

• ...
11
Repeatable
• Run repeatedly in any order, any time

• No side effects

• No globals/statics

• No time

• No random

• No databases

• No services

• No split test logic

• ...
12
Self-validating
• No manual evaluation required

• Only two outcomes

• Red/Green

• Fail/Pass
13
Timely
• Written before the code

• If you write test after code:

• You do not follow TDD

• You are not sure that the test works

• You forget test behaviours

• Your tests become useless

• You feel cheeting

• ...
14
Example
const notebook = new Notebook()
test("add a contact", () => {
notebook.add("John", 1234)
expect(notebook.get("John")).toBe(1234)
})
test("update contact", () => {
notebook.update("John", 3456)
expect(notebook.get("John")).toBe(3456)
})
test("remove a contact", () => {
notebook.remove("John")
expect(notebook.get("John")).toBeNull()
})
15
Example
test("add a contact", () => {
const notebook = new Notebook()
notebook.add("John", 1234)
expect(notebook.get("John")).toBe(1234)
})
test("update contact", () => {
const notebook = new Notebook()
notebook.add("John", 1234)
notebook.update("John", 3456)
expect(notebook.get("John")).toBe(3456)
})
test("remove a contact", () => {
const notebook = new Notebook()
notebook.add("John", 1234)
notebook.remove("John")
expect(notebook.get("John")).toBeNull()
})
16
Example
// Date changes each time
new Date();
// Result changes each call
Random.next();
// We are accessing a static/global
Notebook.add("John", 1234);
// Global + It is not self-validated
if (notebook.get("John") === 1234)
System.out.println("Sucess")
17
Summary
• Levels of Testing

• FIRST properties

• AAA structure
• Code Coverage

• Regression Tests

• Test doubles
18
AAA Structure
• Arrange

• Prepare the initial states

• Call news and required sets before act

• Act

• Execute the actions required by the test

• Assert

• Gather results

• Expect the result values
19
AAA Structure
• Order is always kept 

• Steps are not mixed

• Expect always simple results
20
Example
test("manage contacts", () => {
const notebook = new Notebook() // Arrange
notebook.add("John", 1234) // Act
expect(notebook.get("John")).toBe(1234) // Assert
notebook.update("John", 3456) // Act
expect(notebook.get("John")).toBe(3456) // Assert
notebook.delete("John") // Act
expect(notebook.get("John")).toBeNull() // Assert
})
21
Example
test("add contact", () => {
// Arrange
const notebook = new Notebook()
// Act
notebook.add("John", 1234)
// Assert
expect(notebook.get("John")).toBe(1234)
})
test("update contact", () => {
// Arrange
const notebook = new Notebook()
notebook.add("John", 1234)
// Act
notebook.update("John", 3456)
// Assert
expect(notebook.get("John")).toBe(3456)
})
...
22
Summary
• Levels of Testing

• FIRST properties

• AAA structure

• Code Coverage
• Regression Tests

• Test doubles
23
Code Coverage
24
Code Tested
Code Existing
%
Exemple
test("add contact", () => {
const notebook = new Notebook()
notebook.add("John", 1234)
expect(notebook.get("John")).toBe(1234)
})
class Notebook {
add(name, phone) {
if (map.contains(name))
throw new Error(`Cannot add twice ${name}`)
map.put(name, phone)
}
}
25
Code Coverage: 66%
Uncovered line
Exemple
test("add contact", () => {
const notebook = new Notebook()
notebook.add("John", 1234)
expect(notebook.get("John")).toBe(1234)
})
test('cannot add a duplicated contact', () => {
const notebook = new Notebook()
notebook.add("John", 1234)
expect(() => notebook.add('John', 1111)).toThrow()
})
class Notebook {
add(name, phone) {
if (map.contains(name))
throw new Error(`Cannot add twice ${name}`)
map.put(name, phone)
}
}
26
Code Coverage: 100%
Exemple
test("add contact", () => {
const notebook = new Notebook()
notebook.add("John", 1234)
expect(notebook.get("John")).toBe(1234)
})
class Notebook {
add(name, phone) {
map.put(name, phone)
}
}
27
Code Coverage: 100%
Número de tests
• 1 test base

• 1 test per condition

• &&, ||, if, while, for, ...

• 1 test per switch cases + 1 for default 

• Default is always present, even if it is not written
28
Cannot cover
• Main

• Dates

• Randoms

• Network

• ...
29
–Robert C. Martin
“Every well designed system has at least one dirty
component, usually associated with main. Nothing
outside this component depends on anything inside
it. All dependencies point outward. This is where
you put switches, factories, and IOC.”
30
https://twitter.com/unclebobmartin/status/1122138039390806016
Quality
• 100% Code Coverage DO NOT ensure correctness

• With TDD Code Coverage should be 100%

• It is a hint

• it reveals what went wrong

• what we missed

• which cases we implemented but not planned
31
Summary
• Levels of Testing

• FIRST properties

• AAA structure

• Code Coverage

• Regression Tests
• Test doubles
32
Serius Banking
test("deposit cash", () => {
const bank = new Bank()
bank.createAccount("John")
bank.deposit("John", 100)
expect(bank.get("John")).toBe(100)
})
class Bank {
deposit(name, amount) {
accounts.forEach(account => {
account.desposit += amount
})
}
}
33
🃏
Serius Banking
describe("regression tests", () => {
test("deposit cash into one single account", () => {
const bank = new Bank()
bank.createAccount("John")
bank.createAccount("Maria")
bank.deposit("John", 100)
expect(bank.get("Maria")).toBe(0)
expect(bank.get("John")).toBe(100)
})
})
34
🃏
Summary
• Levels of Testing

• FIRST properties

• AAA structure

• Code Coverage

• Regression Tests

• Test doubles
35
Test Doubles
• Replaces data/object with one created by the test

• 5 types

• Dummy

• Stub

• Spy

• Mock

• Fake
36
Dummy
• Null, 0 or a plain value that can replace an argument
37
test("count created accounts", () => {
const bank = new Bank()
bank.createAccount("John", null)
expect(bank.count()).toBe(1)
})
class Bank {
createAccount(name, fiscalData) {
accounts.put(name, fiscalData)
}
}
Stub
• A value need for the test that the code uses
38
test("gives heads", () => {
const random = {
next() {
return 0
},
}
const result = flipCoin(random)
expect(result).toBe("head")
})
function flipCoin(random) {
if (random.next() < 0.5) return "head"
return "tails"
}
Spy
• Method that remembers its calls
39
test("hello world", () => {
const log = []
const outputSpy = message => {
log.push(message)
}
sayHello(outputSpy)
expect(log).toEqual(["hello world"])
})
function sayHello(output) {
output("hello world")
}
Mock
• A spy that validates correctness and knows the object
40
class DiskMock {
log = [];
move(cylinder) {
this.log.push('move', cylinder);
}
write(data) {
this.log.push('write', data)
}
isFormatted() {
return this.log.equals('move', 0, 'write', 0)
}
}
test('formats a disk', () => {
const disk = new DiskMock();
format(disk);
expect(disk.isFormatted()).toBeTrue();
})
Fake
• An functional object that replaces an original one
41
test("deposit cash", () => {
const db = new InMemoryDB()
const bank = new Bank(db)
bank.createAccount("John")
bank.deposit("John", 100)
expect(bank.get("John")).toBe(100)
})
Confidence
• Test Doubles may Reduce Coverage

• Although, some cases are necessary

• Test Doubles Reduce Confidence
42
Summary
• Levels of Testing

• FIRST properties

• AAA structure

• Code Coverage

• Regression Tests

• Test doubles
43
Homework
• http://misko.hevery.com/code-reviewers-guide/
44

More Related Content

What's hot

Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS ProgrammersDavid Rodenas
 
Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214David Rodenas
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"epamspb
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
Spock Testing Framework - The Next Generation
Spock Testing Framework - The Next GenerationSpock Testing Framework - The Next Generation
Spock Testing Framework - The Next GenerationBTI360
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...julien.ponge
 
Software Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW SydneySoftware Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW Sydneyjulien.ponge
 
Smarter Testing with Spock
Smarter Testing with SpockSmarter Testing with Spock
Smarter Testing with SpockDmitry Voloshko
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent codeDror Helper
 
Smarter Testing With Spock
Smarter Testing With SpockSmarter Testing With Spock
Smarter Testing With SpockIT Weekend
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189Mahmoud Samir Fayed
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctlyDror Helper
 

What's hot (20)

Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS Programmers
 
JS and patterns
JS and patternsJS and patterns
JS and patterns
 
Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Spock Testing Framework - The Next Generation
Spock Testing Framework - The Next GenerationSpock Testing Framework - The Next Generation
Spock Testing Framework - The Next Generation
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
 
Java 7 LavaJUG
Java 7 LavaJUGJava 7 LavaJUG
Java 7 LavaJUG
 
Spock framework
Spock frameworkSpock framework
Spock framework
 
Software Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW SydneySoftware Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW Sydney
 
GMock framework
GMock frameworkGMock framework
GMock framework
 
Smarter Testing with Spock
Smarter Testing with SpockSmarter Testing with Spock
Smarter Testing with Spock
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
Smarter Testing With Spock
Smarter Testing With SpockSmarter Testing With Spock
Smarter Testing With Spock
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
TDD Training
TDD TrainingTDD Training
TDD Training
 
Spock Framework
Spock FrameworkSpock Framework
Spock Framework
 
My java file
My java fileMy java file
My java file
 

Similar to ES3-2020-07 Testing techniques

Iterative architecture
Iterative architectureIterative architecture
Iterative architectureJoshuaRizzo4
 
TDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesTDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesDavid Rodenas
 
Unit test candidate solutions
Unit test candidate solutionsUnit test candidate solutions
Unit test candidate solutionsbenewu
 
Why learn new programming languages
Why learn new programming languagesWhy learn new programming languages
Why learn new programming languagesJonas Follesø
 
TDD Trade-Offs @Softwerkskammer Karlsruhe
TDD Trade-Offs @Softwerkskammer KarlsruheTDD Trade-Offs @Softwerkskammer Karlsruhe
TDD Trade-Offs @Softwerkskammer KarlsruheDavid Völkel
 
Talk about Testing at vienna.rb meetup #2 on Apr 12th, 2013
Talk about Testing at vienna.rb meetup #2 on Apr 12th, 2013Talk about Testing at vienna.rb meetup #2 on Apr 12th, 2013
Talk about Testing at vienna.rb meetup #2 on Apr 12th, 2013Anton Bangratz
 
Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8Frédéric Delorme
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test communityKerry Buckley
 
Mockist vs. Classicists TDD
Mockist vs. Classicists TDDMockist vs. Classicists TDD
Mockist vs. Classicists TDDDavid Völkel
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31Mahmoud Samir Fayed
 
Por qué usar Spock en lugar de JUnit / Mockito para tus tests Java - Codemoti...
Por qué usar Spock en lugar de JUnit / Mockito para tus tests Java - Codemoti...Por qué usar Spock en lugar de JUnit / Mockito para tus tests Java - Codemoti...
Por qué usar Spock en lugar de JUnit / Mockito para tus tests Java - Codemoti...Andrés Viedma Peláez
 
Programming exercises
Programming exercisesProgramming exercises
Programming exercisesTerry Yin
 
The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196Mahmoud Samir Fayed
 
Wann soll ich mocken?
Wann soll ich mocken?Wann soll ich mocken?
Wann soll ich mocken?David Völkel
 
The Ring programming language version 1.10 book - Part 22 of 212
The Ring programming language version 1.10 book - Part 22 of 212The Ring programming language version 1.10 book - Part 22 of 212
The Ring programming language version 1.10 book - Part 22 of 212Mahmoud Samir Fayed
 
Legacy Code Kata v3.0
Legacy Code Kata v3.0Legacy Code Kata v3.0
Legacy Code Kata v3.0William Munn
 
Unit Test and TDD
Unit Test and TDDUnit Test and TDD
Unit Test and TDDViet Tran
 
Using xUnit as a Swiss-Aarmy Testing Toolkit
Using xUnit as a Swiss-Aarmy Testing ToolkitUsing xUnit as a Swiss-Aarmy Testing Toolkit
Using xUnit as a Swiss-Aarmy Testing ToolkitChris Oldwood
 

Similar to ES3-2020-07 Testing techniques (20)

Iterative architecture
Iterative architectureIterative architecture
Iterative architecture
 
TDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesTDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD Techniques
 
Unit test candidate solutions
Unit test candidate solutionsUnit test candidate solutions
Unit test candidate solutions
 
Why learn new programming languages
Why learn new programming languagesWhy learn new programming languages
Why learn new programming languages
 
TDD Trade-Offs @Softwerkskammer Karlsruhe
TDD Trade-Offs @Softwerkskammer KarlsruheTDD Trade-Offs @Softwerkskammer Karlsruhe
TDD Trade-Offs @Softwerkskammer Karlsruhe
 
Talk about Testing at vienna.rb meetup #2 on Apr 12th, 2013
Talk about Testing at vienna.rb meetup #2 on Apr 12th, 2013Talk about Testing at vienna.rb meetup #2 on Apr 12th, 2013
Talk about Testing at vienna.rb meetup #2 on Apr 12th, 2013
 
Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test community
 
Advanced Django
Advanced DjangoAdvanced Django
Advanced Django
 
Mockist vs. Classicists TDD
Mockist vs. Classicists TDDMockist vs. Classicists TDD
Mockist vs. Classicists TDD
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31
 
Por qué usar Spock en lugar de JUnit / Mockito para tus tests Java - Codemoti...
Por qué usar Spock en lugar de JUnit / Mockito para tus tests Java - Codemoti...Por qué usar Spock en lugar de JUnit / Mockito para tus tests Java - Codemoti...
Por qué usar Spock en lugar de JUnit / Mockito para tus tests Java - Codemoti...
 
Programming exercises
Programming exercisesProgramming exercises
Programming exercises
 
The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196
 
Wann soll ich mocken?
Wann soll ich mocken?Wann soll ich mocken?
Wann soll ich mocken?
 
The Ring programming language version 1.10 book - Part 22 of 212
The Ring programming language version 1.10 book - Part 22 of 212The Ring programming language version 1.10 book - Part 22 of 212
The Ring programming language version 1.10 book - Part 22 of 212
 
Legacy Code Kata v3.0
Legacy Code Kata v3.0Legacy Code Kata v3.0
Legacy Code Kata v3.0
 
Linq
LinqLinq
Linq
 
Unit Test and TDD
Unit Test and TDDUnit Test and TDD
Unit Test and TDD
 
Using xUnit as a Swiss-Aarmy Testing Toolkit
Using xUnit as a Swiss-Aarmy Testing ToolkitUsing xUnit as a Swiss-Aarmy Testing Toolkit
Using xUnit as a Swiss-Aarmy Testing Toolkit
 

More from David Rodenas

TDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDTDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDDavid Rodenas
 
TDD CrashCourse Part1: Testing
TDD CrashCourse Part1: TestingTDD CrashCourse Part1: Testing
TDD CrashCourse Part1: TestingDavid Rodenas
 
Be professional: We Rule the World
Be professional: We Rule the WorldBe professional: We Rule the World
Be professional: We Rule the WorldDavid Rodenas
 
ES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataDavid Rodenas
 
Basic Tutorial of React for Programmers
Basic Tutorial of React for ProgrammersBasic Tutorial of React for Programmers
Basic Tutorial of React for ProgrammersDavid Rodenas
 
From high school to university and work
From high school to university and workFrom high school to university and work
From high school to university and workDavid Rodenas
 
Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1David Rodenas
 
Freelance i Enginyeria
Freelance i EnginyeriaFreelance i Enginyeria
Freelance i EnginyeriaDavid Rodenas
 
Angular 1.X Community and API Decissions
Angular 1.X Community and API DecissionsAngular 1.X Community and API Decissions
Angular 1.X Community and API DecissionsDavid Rodenas
 
Mvc - Model: the great forgotten
Mvc - Model: the great forgottenMvc - Model: the great forgotten
Mvc - Model: the great forgottenDavid Rodenas
 
(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
 
Testing: ¿what, how, why?
Testing: ¿what, how, why?Testing: ¿what, how, why?
Testing: ¿what, how, why?David Rodenas
 

More from David Rodenas (16)

TDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDTDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDD
 
TDD CrashCourse Part1: Testing
TDD CrashCourse Part1: TestingTDD CrashCourse Part1: Testing
TDD CrashCourse Part1: Testing
 
Be professional: We Rule the World
Be professional: We Rule the WorldBe professional: We Rule the World
Be professional: We Rule the World
 
ES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game Kata
 
ES3-2020-05 Testing
ES3-2020-05 TestingES3-2020-05 Testing
ES3-2020-05 Testing
 
Vespres
VespresVespres
Vespres
 
Faster web pages
Faster web pagesFaster web pages
Faster web pages
 
Basic Tutorial of React for Programmers
Basic Tutorial of React for ProgrammersBasic Tutorial of React for Programmers
Basic Tutorial of React for Programmers
 
From high school to university and work
From high school to university and workFrom high school to university and work
From high school to university and work
 
Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1
 
Freelance i Enginyeria
Freelance i EnginyeriaFreelance i Enginyeria
Freelance i Enginyeria
 
Angular 1.X Community and API Decissions
Angular 1.X Community and API DecissionsAngular 1.X Community and API Decissions
Angular 1.X Community and API Decissions
 
MVS: An angular MVC
MVS: An angular MVCMVS: An angular MVC
MVS: An angular MVC
 
Mvc - Model: the great forgotten
Mvc - Model: the great forgottenMvc - Model: the great forgotten
Mvc - Model: the great forgotten
 
(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
 
Testing: ¿what, how, why?
Testing: ¿what, how, why?Testing: ¿what, how, why?
Testing: ¿what, how, why?
 

Recently uploaded

Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 

Recently uploaded (20)

Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 

ES3-2020-07 Testing techniques

  • 1. 07 Testing - Techniques Enginyeria del Software 3 1 @drpicox — 2020
  • 2. Summary • Levels of Testing • FIRST properties • AAA structure • Code Coverage • Regression Tests • Test doubles 2
  • 3. Summary • Levels of Testing • FIRST properties • AAA structure • Code Coverage • Regression Tests • Test doubles 3
  • 4. Levels of Testing • Acceptance Tests • Programmers Tests 4
  • 5. Acceptance Tests • Readable by Stakeholders • Natural language • Aligned to Requirements • High level • Integrate many levels in the test • Slow 5
  • 6. Programmers Tests • Fast to run • Quick to code • Fine grained • Close to Requirements (Business rules) 6
  • 8. Summary • Levels of Testing • FIRST properties • AAA structure • Code Coverage • Regression Tests • Test doubles 8
  • 9. FIRST properties • Fast • Isolates • Repeatable • Self-validating • Timely 9
  • 10. Fast • Many hundreds or thousands per second • Avoid slow logic • Data bases • External services • Intensive computations • ... 10
  • 11. Isolates • Failure reasons become obvious • Each test runs individually • No assumed initial state • Not require other test run first • Can run alone • Can run in random order • new , new , new • ... 11
  • 12. Repeatable • Run repeatedly in any order, any time • No side effects • No globals/statics • No time • No random • No databases • No services • No split test logic • ... 12
  • 13. Self-validating • No manual evaluation required • Only two outcomes • Red/Green • Fail/Pass 13
  • 14. Timely • Written before the code • If you write test after code: • You do not follow TDD • You are not sure that the test works • You forget test behaviours • Your tests become useless • You feel cheeting • ... 14
  • 15. Example const notebook = new Notebook() test("add a contact", () => { notebook.add("John", 1234) expect(notebook.get("John")).toBe(1234) }) test("update contact", () => { notebook.update("John", 3456) expect(notebook.get("John")).toBe(3456) }) test("remove a contact", () => { notebook.remove("John") expect(notebook.get("John")).toBeNull() }) 15
  • 16. Example test("add a contact", () => { const notebook = new Notebook() notebook.add("John", 1234) expect(notebook.get("John")).toBe(1234) }) test("update contact", () => { const notebook = new Notebook() notebook.add("John", 1234) notebook.update("John", 3456) expect(notebook.get("John")).toBe(3456) }) test("remove a contact", () => { const notebook = new Notebook() notebook.add("John", 1234) notebook.remove("John") expect(notebook.get("John")).toBeNull() }) 16
  • 17. Example // Date changes each time new Date(); // Result changes each call Random.next(); // We are accessing a static/global Notebook.add("John", 1234); // Global + It is not self-validated if (notebook.get("John") === 1234) System.out.println("Sucess") 17
  • 18. Summary • Levels of Testing • FIRST properties • AAA structure • Code Coverage • Regression Tests • Test doubles 18
  • 19. AAA Structure • Arrange • Prepare the initial states • Call news and required sets before act • Act • Execute the actions required by the test • Assert • Gather results • Expect the result values 19
  • 20. AAA Structure • Order is always kept • Steps are not mixed • Expect always simple results 20
  • 21. Example test("manage contacts", () => { const notebook = new Notebook() // Arrange notebook.add("John", 1234) // Act expect(notebook.get("John")).toBe(1234) // Assert notebook.update("John", 3456) // Act expect(notebook.get("John")).toBe(3456) // Assert notebook.delete("John") // Act expect(notebook.get("John")).toBeNull() // Assert }) 21
  • 22. Example test("add contact", () => { // Arrange const notebook = new Notebook() // Act notebook.add("John", 1234) // Assert expect(notebook.get("John")).toBe(1234) }) test("update contact", () => { // Arrange const notebook = new Notebook() notebook.add("John", 1234) // Act notebook.update("John", 3456) // Assert expect(notebook.get("John")).toBe(3456) }) ... 22
  • 23. Summary • Levels of Testing • FIRST properties • AAA structure • Code Coverage • Regression Tests • Test doubles 23
  • 25. Exemple test("add contact", () => { const notebook = new Notebook() notebook.add("John", 1234) expect(notebook.get("John")).toBe(1234) }) class Notebook { add(name, phone) { if (map.contains(name)) throw new Error(`Cannot add twice ${name}`) map.put(name, phone) } } 25 Code Coverage: 66% Uncovered line
  • 26. Exemple test("add contact", () => { const notebook = new Notebook() notebook.add("John", 1234) expect(notebook.get("John")).toBe(1234) }) test('cannot add a duplicated contact', () => { const notebook = new Notebook() notebook.add("John", 1234) expect(() => notebook.add('John', 1111)).toThrow() }) class Notebook { add(name, phone) { if (map.contains(name)) throw new Error(`Cannot add twice ${name}`) map.put(name, phone) } } 26 Code Coverage: 100%
  • 27. Exemple test("add contact", () => { const notebook = new Notebook() notebook.add("John", 1234) expect(notebook.get("John")).toBe(1234) }) class Notebook { add(name, phone) { map.put(name, phone) } } 27 Code Coverage: 100%
  • 28. Número de tests • 1 test base • 1 test per condition • &&, ||, if, while, for, ... • 1 test per switch cases + 1 for default • Default is always present, even if it is not written 28
  • 29. Cannot cover • Main • Dates • Randoms • Network • ... 29
  • 30. –Robert C. Martin “Every well designed system has at least one dirty component, usually associated with main. Nothing outside this component depends on anything inside it. All dependencies point outward. This is where you put switches, factories, and IOC.” 30 https://twitter.com/unclebobmartin/status/1122138039390806016
  • 31. Quality • 100% Code Coverage DO NOT ensure correctness • With TDD Code Coverage should be 100% • It is a hint • it reveals what went wrong • what we missed • which cases we implemented but not planned 31
  • 32. Summary • Levels of Testing • FIRST properties • AAA structure • Code Coverage • Regression Tests • Test doubles 32
  • 33. Serius Banking test("deposit cash", () => { const bank = new Bank() bank.createAccount("John") bank.deposit("John", 100) expect(bank.get("John")).toBe(100) }) class Bank { deposit(name, amount) { accounts.forEach(account => { account.desposit += amount }) } } 33 🃏
  • 34. Serius Banking describe("regression tests", () => { test("deposit cash into one single account", () => { const bank = new Bank() bank.createAccount("John") bank.createAccount("Maria") bank.deposit("John", 100) expect(bank.get("Maria")).toBe(0) expect(bank.get("John")).toBe(100) }) }) 34 🃏
  • 35. Summary • Levels of Testing • FIRST properties • AAA structure • Code Coverage • Regression Tests • Test doubles 35
  • 36. Test Doubles • Replaces data/object with one created by the test • 5 types • Dummy • Stub • Spy • Mock • Fake 36
  • 37. Dummy • Null, 0 or a plain value that can replace an argument 37 test("count created accounts", () => { const bank = new Bank() bank.createAccount("John", null) expect(bank.count()).toBe(1) }) class Bank { createAccount(name, fiscalData) { accounts.put(name, fiscalData) } }
  • 38. Stub • A value need for the test that the code uses 38 test("gives heads", () => { const random = { next() { return 0 }, } const result = flipCoin(random) expect(result).toBe("head") }) function flipCoin(random) { if (random.next() < 0.5) return "head" return "tails" }
  • 39. Spy • Method that remembers its calls 39 test("hello world", () => { const log = [] const outputSpy = message => { log.push(message) } sayHello(outputSpy) expect(log).toEqual(["hello world"]) }) function sayHello(output) { output("hello world") }
  • 40. Mock • A spy that validates correctness and knows the object 40 class DiskMock { log = []; move(cylinder) { this.log.push('move', cylinder); } write(data) { this.log.push('write', data) } isFormatted() { return this.log.equals('move', 0, 'write', 0) } } test('formats a disk', () => { const disk = new DiskMock(); format(disk); expect(disk.isFormatted()).toBeTrue(); })
  • 41. Fake • An functional object that replaces an original one 41 test("deposit cash", () => { const db = new InMemoryDB() const bank = new Bank(db) bank.createAccount("John") bank.deposit("John", 100) expect(bank.get("John")).toBe(100) })
  • 42. Confidence • Test Doubles may Reduce Coverage • Although, some cases are necessary • Test Doubles Reduce Confidence 42
  • 43. Summary • Levels of Testing • FIRST properties • AAA structure • Code Coverage • Regression Tests • Test doubles 43