Transcript
Page 1: Is this how you hate unit testing?

www.odd-e.com

I hate unit testing!!!

Friday, 9 December, 11

Page 2: Is this how you hate unit testing?

Who am I?

• Name: Steven Mak• Agile Coach at Odd-e• Lives in Hong Kong• Agile, TDD Coaching• I love coding - Java, C/C++,

PHP, Perl, and some weird ones

• I speak English, Cantonese, and Mandarin

2

Friday, 9 December, 11

Page 3: Is this how you hate unit testing?

本故事純屬虛構如有雷同實屬巧合

3

Friday, 9 December, 11

Page 4: Is this how you hate unit testing?

I HATE UNIT TESTING!

4

•Doesn’t catch many bugs•Still need to do integration tests•Waste time

Friday, 9 December, 11

Page 5: Is this how you hate unit testing?

Need to test at integration level anyway?

5

Is this what you mean by integration testing?

Friday, 9 December, 11

Page 6: Is this how you hate unit testing?

How many tests do you need to cover through system level tests?

6

10states

10states

10states

5 interactions 5 interactions

5 interactions

Adapted from: http://www.renaissancesoftware.net/files/sglon/LondonScrumGathering-v1r0.key.pdf

Tests

• It would take 1000 (or more) tests to this simple system!• System Level Tests just cannot be thorough

Friday, 9 December, 11

Page 7: Is this how you hate unit testing?

The down side• Tons of them to write• False sense of security• Integration tests are slow• Hard to diagnose• Brittle

- one change would trigger many test failures

7

Friday, 9 December, 11

Page 8: Is this how you hate unit testing?

Test Automation Pyramid

8

You are going to need some integration tests, but not in the way you do like unit tests.

Friday, 9 December, 11

Page 9: Is this how you hate unit testing?

Write good focused unit tests

• Don’t test the platform.• When writing a single object to test with other

collaborating objects, use interfaces for those other points. Interfaces are not the actual collaborating object.

• Leverage the interfaces so you don’t need to actually test the other objects.

• Test the single object to speak to itself, i.e. State Tests

• Create your focused Collaboration and Contract Tests.

9

http://b-speaking.blogspot.com/search/label/integration%20tests

Friday, 9 December, 11

Page 10: Is this how you hate unit testing?

Collaboration and Contract Tests

• Collaboration tests:- Do I ask the collaborators the right questions?- Can I handle the collaborators’ responses?

• Contract tests:- Can the interface accept the question being asked of it?- Can the interface supply the responses expected?

10

Friday, 9 December, 11

Page 11: Is this how you hate unit testing?

Example:

11

@Testpublic void with_arguments(){! MyCollaborator c = mock(MyCollaborator.class);! when(c.compareTo("Test")).thenReturn(1);! assertEquals(1,a.doSomethingElse("Test"));}

We are assuming that compareTo() would return 1 if we pass in “Test”

@Testpublic void compareToShouldReturnOne(){! ! MyCollaborator c = new MyCollaborator();! ! assertEquals(1,c.compareTo("Test"));}

We write a test for the real MyCollaborator.compareTo()

Contract tests are usually slow but tend to be stable. Often running just once a day is plenty

Friday, 9 December, 11

Page 12: Is this how you hate unit testing?

Refactoring?

12

Code smells!

Code stinks!

It’s no fun writing unit test if you don’t spend time refactoring

Friday, 9 December, 11

Page 13: Is this how you hate unit testing?

Why? How?

13

amount of bad

code

panic

amount of code

smells

time spend on

bug fixing

motivation

developers

quick hacks

opportunity for

# of bugs

indicates

O

refactoring

O

Friday, 9 December, 11

Page 14: Is this how you hate unit testing?

Refactoring visualized

14

Original program:

Making changes:

More changes:

Without refactoring:

Friday, 9 December, 11

Page 15: Is this how you hate unit testing?

Refactoring visualized

14

Original program:

Making changes:

More changes:

Without refactoring:

Friday, 9 December, 11

Page 16: Is this how you hate unit testing?

Refactoring visualized

14

Original program:

Making changes:

More changes:

Without refactoring:

Cost of change increases rapidly!

Friday, 9 December, 11

Page 17: Is this how you hate unit testing?

Refactoring visualized

14

Original program:

Making changes:

More changes:

Without refactoring:

Cost of change increases rapidly!

With refactoring:

Friday, 9 December, 11

Page 18: Is this how you hate unit testing?

Refactoring visualized

14

Original program:

Making changes:

More changes:

Without refactoring:

Cost of change increases rapidly!

With refactoring:

Small change

Friday, 9 December, 11

Page 19: Is this how you hate unit testing?

Refactoring visualized

14

Original program:

Making changes:

More changes:

Without refactoring:

Cost of change increases rapidly!

With refactoring:

Small change

Friday, 9 December, 11

Page 20: Is this how you hate unit testing?

Refactoring visualized

14

Original program:

Making changes:

More changes:

Without refactoring:

Cost of change increases rapidly!

With refactoring:

Small change

Refactor

Friday, 9 December, 11

Page 21: Is this how you hate unit testing?

Refactoring visualized

14

Original program:

Making changes:

More changes:

Without refactoring:

Cost of change increases rapidly!

With refactoring:

Small change

Refactor

Friday, 9 December, 11

Page 22: Is this how you hate unit testing?

Refactoring visualized

14

Original program:

Making changes:

More changes:

Without refactoring:

Cost of change increases rapidly!

With refactoring:

Small change

Refactor

Etc

Cost of change not increases

Friday, 9 December, 11

Page 23: Is this how you hate unit testing?

Refactoring based on unit testing

15

Friday, 9 December, 11

Page 24: Is this how you hate unit testing?

What to refactor?

16

Friday, 9 December, 11

Page 25: Is this how you hate unit testing?

Beware of blocking

17

• Individual Code Ownership?• Branching for long time?• Deadline pressure?

- Refactoring makes your code base easier to work on

Friday, 9 December, 11

Page 26: Is this how you hate unit testing?

Time consuming?

18

Too busy fire fighting, but no time to write tests?

Friday, 9 December, 11

Page 27: Is this how you hate unit testing?

Speculate Code Test Debug

Sustainability

19

Traditional development

Time vs

Friday, 9 December, 11

Page 28: Is this how you hate unit testing?

Speculate Code Test Debug

Sustainability

19

Traditional development

Time developers do not notice nor plan for

Time vs

Friday, 9 December, 11

Page 29: Is this how you hate unit testing?

Speculate Code Test Debug

Sustainability

19

Traditional development

Time developers do not notice nor plan for

Time vs

Test-driven development

Speculate Test and Code Debug

Friday, 9 December, 11

Page 30: Is this how you hate unit testing?

Speculate Code Test Debug

Sustainability

19

Traditional development

Time vs

Test-driven development

Speculate Test and Code Debug

Feels slower

Friday, 9 December, 11

Page 31: Is this how you hate unit testing?

20

We are tired of delivering craps

Do you have confidence with your work before you deliver it?

Friday, 9 December, 11

Page 32: Is this how you hate unit testing?

Edsger Dijkstra“Those who want really reliable software will discover that they must find means of avoiding the majority of bugs to start with, and as a result, the programming process will become cheaper. If you want more effective programmers, you will discover that they should not waste their time debugging, they should not introduce the bugs to start with.”

21

Friday, 9 December, 11

Page 33: Is this how you hate unit testing?

TDD Cycle

22

Test

Implement

Design

• Short• Rhythmic• Incremental • Design-focused• Disciplined

Friday, 9 December, 11

Page 34: Is this how you hate unit testing?

Oh, Our code was there already

23

1. Identify change point2. Find test points3. Break dependencies4. Write tests5. Make changes and refactor

It is always harder to write unit tests after we wrote the code

Friday, 9 December, 11

Page 35: Is this how you hate unit testing?

Our code is too simple, so we don’t need unit testing

24

Our code is too complicated, so writing unit tests is too difficult

We don’t have problems at unit level

Friday, 9 December, 11

Page 36: Is this how you hate unit testing?

Tests you write after the fact are Defense or Justification

25

• Test later = Test never• After-the-fact tests are written by someone

who is already vested in the code and already knows how the problem was solved

• There’s just no way those tests can be anywhere near as incisive as tests written first

• If you write the unit test after the fact, you are not going to catch many bugs.

Friday, 9 December, 11

Page 37: Is this how you hate unit testing?

Unit test is not just about testing

26

Look at the design through unit tests

Friday, 9 December, 11

Page 38: Is this how you hate unit testing?

Modularity == Testability

27

Focus on tests first ensures modularity and other good design principles

Friday, 9 December, 11

Page 39: Is this how you hate unit testing?

28

Design from the perspective of userather than implementation

Friday, 9 December, 11

Page 40: Is this how you hate unit testing?

• Debug-later-programming don’t consider testability in the first place• TDD guarantees testability, while you still need to know good design

principles• Unit tests give you a safety net to try different design

29

It is always harder to write unit tests after we wrote the code

Friday, 9 December, 11

Page 41: Is this how you hate unit testing?

When you find it hard to write unit tests...

• Instantiating class instance that is hard to setup?- What about using Factories?- Have you think about Dependency Injection?

• Long methods?- Does it have more than one responsibilities?

• Bare classes?- Why not provide abstract base classes to support better mocking?- Why clients need to know everything from this base class?

• Interface Segregation Principle

30

Friday, 9 December, 11

Page 42: Is this how you hate unit testing?

• Confidence and trust in the code

- Treading the happy path

- “If it doesn’t have to work, I can get it done a lot faster!”

- “Cost of bug fixing is lower if it is found earlier”

• Encourages good design

- Clean it up later

- Experimenting different design

• Integration Testing

- Making assumptions explicit

31

TDD, The Professional Option

Friday, 9 December, 11

Page 43: Is this how you hate unit testing?

Resources

32

Friday, 9 December, 11

Page 44: Is this how you hate unit testing?

Agile Tour ShenZhen• Tencent Building, Shenzhen (騰訊大廈)

- 20 Nov 2011- Tencent Building, Kejizhongyi Avenue, Hi-techPark,Nanshan

District,Shenzhen.- 深圳市南山区科技园科技中一路腾讯大厦,深南大道北侧

• http://www.agiletour.cn• Contact: [email protected]

33

Friday, 9 December, 11

Page 45: Is this how you hate unit testing?

I want to organise a group in Hong Kong!

34

http://tinyurl.com/HKALSDN

Friday, 9 December, 11

Page 46: Is this how you hate unit testing?

Further readings

35

• Integration Tests are a Scam:- http://www.jbrains.ca/series/integrated-tests-are-a-scam

• Integration Contract Tests:- http://martinfowler.com/bliki/IntegrationContractTest.html

• Opportunistic Refactoring:- http://martinfowler.com/bliki/OpportunisticRefactoring.html

• Demand Technical Excellence:- http://www.renaissancesoftware.net/files/sglon/

LondonScrumGathering-v1r0.key.pdf• Clean Coder, by Robert Martin

Friday, 9 December, 11

Page 47: Is this how you hate unit testing?

Books - Technical Practices

36

Friday, 9 December, 11

Page 48: Is this how you hate unit testing?

Thank you :-)

37

Steven MakEmail: [email protected]

Friday, 9 December, 11


Top Related