Two hours talk about testing, its history, its meaning and propose, a small guide of good practices, and what it is supposed to be a good professional and what is expected from us the professionals.
2. @drpicox
Qui soc?
• David Ródenas Picó
• Enginyer en Informàtica (pla del 91)
• Doctor en Informàtica
• Vicedegà del Col·legi d’Enginyers Informàtics
• Freelance Arquitecte Software i Formador
• Programador des dels 8 anys (el '86)
• Passat: professor universitari, empresari, becari, …
2
7. @drpicox 7
http://wiki.c2.com/?TenYearsOfTestDrivenDevelopment
1994
1999
2002
Beginning of new Era
xUnit
Extreme Programming
Framework Integrated Test
Test Driven Development
Ward Cunningham writes in one day a test runner
Kent Beck writes first version of SUnit (Testing Framework)
“Extreme Programming Explained: Embrace Change” - Kent Beck
Ward Cunningham writes first tool for test running
“Test Driven Development By Example” - Kent Beck
1989
15. @drpicox
Coding cost
15
Debug
+-
• Even if everything works well, we
have to verify that it is true
• Navigate to affected point
• Make sure that everything works
as expected, each time
• If it fails, stop and start looking for
problems…
21. @drpicox
Why testing?
• Automatic testing saves time
• Considers all features with all semantics
• Everything is debugged
• Does not throw away debug cases
• New features does not break old one
• Safe refactors to accommodate new changes
• More bald implementations
• Developers(professionals) feel safer.
21
29. @drpicox
Types of testing
29
Logic
Wiring
Visual > 10s
< 1s
~1ms
each takes
few
many
lots
end-to-end
functional
unit
known as
whole system
subsystems
just classes
acceptance testing
bdd
tdd
#tests
31. @drpicox
Unit Testing
• It test the most dangerous kind of bugs
• It is easy to write
• It is fast to execute
• It is from engineers to engineers
• It focus into features details
31
32. @drpicox
Unit Testing
• It test the most dangerous kind of bugs → Solves them
• It is easy to write → Write tens in few minutes
• It is fast to execute → Development cycle of seconds
• It is from engineers to engineers → It is the doc
• It focus into features details → Solve fast ambiguity
• IT IS THE MOST POWERFUL TYPE OF TESTING
32
34. @drpicox
Unit Test Everything!
• Test coverage?
34
¿Are you sure that you want
to write a line of code that cannot be
justified with a test?
¿Do you want to
write useless code?
¿Do you want to
maintain more code
than necessary?
¿Do you want to
relay in your ability
to manual testing
all cases?
36. @drpicox
Unit Test Everything?
36
Serious Banking
http://embed.plnkr.co/veOMnl
🃏
Sorry, not everything, we have frame problem.
(we can imagine infinite stupid things that can go wrong)
38. @drpicox
TDD Rules
1. You are not allowed to write any production code unless
it is to make a failing unit test pass.
2. You are not allowed to write any more of a unit test
than is sufficient to fail; and compilation failures are
failures.
3. You are not allowed to write any more production code
than is sufficient to pass the one failing unit test.
38
http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd
48. @drpicox
Hello World
48
describe(‘hello world’, () => {
it(‘it should say hi there!’, () => {
let helloWorld = new HelloWorld();
helloWorld.sayHello();
expect(???).toBe(‘hi there!’);
});
});
http://embed.plnkr.co/JxAOX7
54. @drpicox
Dependency Injection
function testCreditCardCharge() {
Database db = new Database();
OfflineQueue q = new OfflineQueue(db);
CreditCardProcessor ccp =
new CreditCardProcessor(q);
CreditCard c = new CreditCard(
"1234 5678 9012 3456", 5, 2008);
c.charge(ccp, 100);
}
54
55. @drpicox
Dependency Injection
function testCreditCardCharge() {
Database db = new Database();
OfflineQueue q = new OfflineQueue(db);
CreditCardProcessor ccp =
new CreditCardProcessor(q);
CreditCard c = new CreditCard(
"1234 5678 9012 3456", 5, 2008);
c.charge(ccp, 100);
}
55
56. @drpicox
Dependency Injection
function testCreditCardCharge() {
Database db = new Database();
OfflineQueue q = new OfflineQueue(db);
CreditCardProcessor ccp =
new CreditCardProcessor(q);
CreditCard c = new CreditCard(
"1234 5678 9012 3456", 5, 2008);
c.charge(ccp, 100);
}
56
Looks better, right? But you still loosing 100$.
57. @drpicox
Dependency Injection
function testCreditCardCharge() {
CreditCardProcessorMock ccp =
new CreditCardProcessorMock();
CreditCard c = new CreditCard(
"1234 5678 9012 3456", 5, 2008);
c.charge(ccp, 100);
assertTrue(ccp
.charged("1234 5678 9012 3456", 100));
}
57
Looks better!
58. @drpicox
Dependency Injection
• Is inversion of control for resolving dependencies.
• Your dependencies will explicitly be:
• given by constructor
• setted after construction
58
class UserController {
constructor(userService) {
this.userService = userService;
}
}
59. @drpicox
Dependency Injection
59
Two piles
Pile of Objects
• Business logic
• This is why you write code
Pile of New Keywords
• Factories, Providers, …
• Build object graphs
• This is how you get the
code you write to work
together
new
new
new
new
new
new
new
new
new
new
new
60. @drpicox
Dependency Injection
60
describe(‘hello world’, () => {
it(‘it should say hi there!’, () => {
let console = jasmine.createSpyObj(‘console’, [‘log’]);
let helloWorld = new HelloWorld(console);
helloWorld.sayHello();
expect(console.log).toHaveBeenCalledWith(‘hi there!’);
});
});
http://embed.plnkr.co/vZyo7u
61. @drpicox
Law of Demeter
• You only ask for objects which you directly need (operate
on)
• a.getX().getY()… is dead giveaway
• serviceLocator.getService() is breaking the Law of
Demeter
• Dependency Injection of the specific object you need.
Hollywood Principle.
61
62. @drpicox
How university can contribute?
• Write tests First!
• Create testing syllabus
• Get deeper in Cohesion
• Transmit: Professionalism require testing
62
70. Història
• 1945: O(1) ordinadors
• 1960: O(100) ordinadors, O(1.000) programadors
• 1965: O(10.000) ordinadors, O(100.000) programadors
• 1970: O(100.000) ordinadors, O(1M) programadors
Des de 1970 el número de programadors
es dobla cada 5 anys.
76. @drpicox
This is real
• Toyota software has killed 89 people
• They have been fined with $1.200.000.000
• They had to review 8.000.000 cars in US
76
79. @drpicox
Sumary
• Testing saves development time
• Focus in Unit Test
(acceptance testing is also amazing)
• Tests to show that your code is what you need
• Make dependencies explicit
79