misconceptions of unit testing

34
Misconceptions of Unit Testing by [email protected] for Scrum Gathering Shanghai 2011 twitter: @terryyin Sina 微博:@terry尹哲

Upload: terry-yin

Post on 28-Nov-2014

2.829 views

Category:

Documents


2 download

DESCRIPTION

This is my slides used in Scrum Gathering Shanghai 2011.

TRANSCRIPT

Page 1: Misconceptions Of Unit Testing

Misconceptionsof Unit Testing

by [email protected]

for Scrum Gathering Shanghai 2011

twitter: @terryyinSina 微博:@terry尹哲

Page 2: Misconceptions Of Unit Testing

Question: Which is likely to be the logo for unit testing?

Page 3: Misconceptions Of Unit Testing

Myth 1: Why UT?

Unit Testing Is About Finding Bugs

Page 4: Misconceptions Of Unit Testing

● People are told that UT can help them find a lot of bugs○ This is a common reason why people stop doing UT after a few

months.○ And this is why management stop believe in UT too.

● Build and fix vs. Prevent from having bugs

Page 5: Misconceptions Of Unit Testing

Unit Test

Then, why do we do UT?

Page 6: Misconceptions Of Unit Testing

Tips

● You don't want the team to generate buggy code in the first place.

● You want the code to be protected for implementing your future ideas, and still keep productive in the future.

● Tell your team that they will sleep better when they know that their source code are protected by UT.

Page 7: Misconceptions Of Unit Testing

Myth 2: What to Test?

UT tests the user requirement

Page 8: Misconceptions Of Unit Testing

● Most people (including and especially PO) care more about○ Is my software functioning as per the requirement?

● But in the same time, they could omit○ Does my software have a good internal design?

Page 9: Misconceptions Of Unit Testing

Internal Quality vs. External Quality

Unfortunately, internal quality is very often not directly linked with current external quality.

Page 10: Misconceptions Of Unit Testing

UT is more about internal quality than external quality.

Page 11: Misconceptions Of Unit Testing

● The main purpose of UT is not to cover your system requirement

● The internal quality is as important as external quality

○ Good design (testable design)

Tips

Page 12: Misconceptions Of Unit Testing

UT is duplication

● Well, yes.● As the RAID system in computer system, double entry book

keeping in accounting, extra protections in rock climbing, UT is a necessary redundancy in programming.

Page 13: Misconceptions Of Unit Testing

Myth 3: Where to put the focus?

Let's add UT, but don't touch the code

Page 14: Misconceptions Of Unit Testing

you want your good things in your product, not somewhere else.

Not even in the tests.

Page 15: Misconceptions Of Unit Testing

● Don't force your team to test badly designed code

○ Ask them to make it testable first.● "Complex" test framework and tests do not

always mean good thing.○ You want great product, not great UT○ Nor great document

Tips

Page 16: Misconceptions Of Unit Testing

Myth 4: How to Test?

UT cases are fragile when I do an architectural change.

Comprehensive System (or functional) Regression test can replace UT

Page 17: Misconceptions Of Unit Testing

Big, end to end test

Oh, shoot! I climbed the wrong rock.

Page 18: Misconceptions Of Unit Testing

you can't be more stable than your dependencies

Page 19: Misconceptions Of Unit Testing

So, it's time to break the dependencies.

Page 20: Misconceptions Of Unit Testing

Example

Page 21: Misconceptions Of Unit Testing

Good unit test has very limited scope

Page 22: Misconceptions Of Unit Testing

● Testability is the key● Challenge your team to have SOLID design● Don't always have all dependent tests

Tips

Page 23: Misconceptions Of Unit Testing

Myth 5: Who Test?

By different person/team

Is a separate task

Page 24: Misconceptions Of Unit Testing

● Because they are more likely to break the software?○ That's simply a waste of time

● If they made a wrong design, they can not find it through UT

○ Yes, that is simply true.

Page 25: Misconceptions Of Unit Testing

Test Exampletest_is_able_to_chow([2, 3], 1);test_is_able_to_chow([2, 3], 4);test_is_able_to_chow([1, 3], 2);test_is_not_able_to_chow([1, 4], 2);test_is_able_to_chow([1, 2, 3], 2);test_is_not_able_to_chow([1], 2);

This is not a test problem, it's a design problem.

Page 26: Misconceptions Of Unit Testing

● Just let people who do the design to write the protection of their design

● UT should be a natural part of coding● Nothing can replace domain knowledge or 10-

year experience○ No silver bullet.

Tips

Page 27: Misconceptions Of Unit Testing

Myth 6: When?

Let's add UT later

writing code with unit tests takes much more time

Page 28: Misconceptions Of Unit Testing

Well, talk to him about your idea.

Page 29: Misconceptions Of Unit Testing

● Michael Feathers' definition for legacy code.● If you find yourself in a hole, what is the first thing to do?

● Test later = test never

Page 30: Misconceptions Of Unit Testing

Debug vs. Precise/immediate Failure Report

These cases, they are more meaningful when they pass.

These cases, they can tell us a lot of useful information when they fail.

Page 31: Misconceptions Of Unit Testing

8 shames, 8 honors以动手实践为荣,以只看不练为耻。 

以打印日志为荣,以单步跟踪为耻。 以空白分隔为荣,以制表分隔为耻。 

以单元测试为荣,以手工测试为耻。 

以代码重用为荣,以复制粘贴为耻。 

以多态应用为荣,以分支判断为耻。 

以短小精悍为荣,以冗余拖沓为耻。 以总结思考为荣,以不求甚解为耻。

Honor for get your hands dirty, shame for just watching.Honor for logging, shame for debugging.Honor for indenting with spaces, shame for using tabs.Honor for unit testing, shame for manual testing.

Honor for code reusing, shame for copy-paste.Honor for polymorphism, shame for using ifs.Honor for short and neat, shame for tediousness.Honor for summarizing and thinking, shame for superficial understanding.

Page 32: Misconceptions Of Unit Testing

● 'time to find the bug' and 'time to locate the bug' are the keys to productivity.

● Any other task will look 'more important' to you than adding UT in the future.

● It is suppose to be faster to do the UT while coding.

○ Are you making the problem better or worse?

Tips

Page 33: Misconceptions Of Unit Testing

It's software design, after all.

Page 34: Misconceptions Of Unit Testing

Thanks to

Gerard Meszaros for reviewing my slides during Scrum Gathering Shanghai 2011.