【Edd workshop@140725】TDD introduction_Andy Huang

Preview:

DESCRIPTION

【EDD Workshop@140725】 Topic: TDD介紹 (Test-Driven Development,測試驅動開發) Speaker: Andy Huang

Citation preview

TDDAndy

Test-driven Development

● red/green/refactor where red means fail and green means pass.

The Three Laws of TDD.

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.

Example

The subroutine add() takes two arguments and adds them together. The result is returned.

Live Coding

● fewer than two arguments?● more than two arguments?● argument isn’t numeric?

TDD Sucks

● I never have enough time to write the tests, once I’ve finished the main functionality.

● Testing isn’t my job, because it’s QA’s job to make sure I do quality work

● Unit tests don’t help me, because my code works perfectly the first time.

● I don’t like TDD, because I enjoy the hours I spend in my debugger

● ...

Benefits

1. Productive (Reduce your debugging time)2. Code Quality3. Over-engineering (YAGNI)4. Better Interface5. Documentation6. Lead to more modularized, flexible, and

extensible code

Best Practices

1. Keep the unit small2. Focused on only the results necessary to

validate its test.3. Treat your test code with the same respect as

your production code.4. Low Coupling allows each unit to be effectively

tested in isolation.5. Mock & Stub

Anti-Pattern

1. Avoid dependencies between test cases.2. Do not test implementation details.3. Avoid slow running tests.

Drawback

1. UI2. Management support is essential3. TDD is dead(?)

Q&A

Thank you

REFS● http://en.wikipedia.org/wiki/Test-driven_development● http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd● http://googletesting.blogspot.tw/2014/04/the-real-test-driven-development.html● http://david.heinemeierhansson.com/2014/tdd-is-dead-long-live-testing.html● https://www.facebook.com/notes/kent-beck/rip-tdd/750840194948847● https://github.com/nelsonic/learn-mocha● http://martinfowler.com/articles/mocksArentStubs.html● http://en.wikipedia.org/wiki/Mock_object● http://googletesting.blogspot.tw/

Recommended