This talk cares about the fundamentals of testing, a little bit history of how the professional community developed what we currently know as testing, but also about why I should care about testing? why is it important to do a test? What is important to test? What is not important to test? How to do testing?
There some examples in plnker just to see each step, and many surprises.
This talk also compares what people learned in the Computer Sciences and Engineering degrees and what people does in testing. It gives some tips to catch up with current state of art and gives some points to start changing syllabus to make better engineers.
This talk is good for beginners, teachers, bosses, but also for seasoned techies that just want to light up some of the ideas that they might have been hatching.
Spoiler alert: testing will save you development time and make you a good professional.
6. @drpicox 6
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
14. @drpicox
Coding cost
14
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…
20. @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.
20
28. @drpicox
Types of testing
28
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
30. @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
30
31. @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
31
33. @drpicox
Unit Test Everything!
• Test coverage?
33
¿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?
35. @drpicox
Unit Test Everything?
35
Serious Banking
http://embed.plnkr.co/veOMnl
🃏
Sorry, not everything, we have frame problem.
(we can imagine infinite stupid things that can go wrong)
37. @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.
37
http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd
46. @drpicox
Hello World
46
describe(‘hello world’, () => {
it(‘it should say hi there!’, () => {
let helloWorld = new HelloWorld();
helloWorld.sayHello();
expect(???).toBe(‘hi there!’);
});
});
http://embed.plnkr.co/JxAOX7
52. @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);
}
52
Looks better, right? But you still loosing 100$.
53. @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));
}
53
Looks better!
54. @drpicox
Dependency Injection
• Is inversion of control for resolving dependencies.
• Your dependencies will explicitly be:
• given by constructor
• setted after construction
54
class UserController {
constructor(userService) {
this.userService = userService;
}
}
55. @drpicox
Dependency Injection
55
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
56. @drpicox
Dependency Injection
56
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
57. @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.
57
59. @drpicox
How university can contribute?
• Write tests First!
• Create testing syllabus
• Get deeper in Cohesion
• Transmit: Professionalism require testing
59
61. @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
61