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
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
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
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"
}
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