8

I am developing my first project using test driven development. I am using Zend Framework and PHPUnit.

Currently my project is at 100% code coverage but I am not sure I understand in what order I am supposed to write my code.

Am I supposed to write my test FIRST with what my objects are expected to do, or write my objects and then test them?

I've been working on completing a controller/model and then writing a test for it but I am not sure if this is what TDD is about.

Any advice?

For example, I wrote my Auth plugin and my Auth controller and tested that they work properly in my browser; then I sat down to write the tests for them which proved that there were some logical errors in the code that did work in the browser.

Mike Partridge
6,7171 gold badge28 silver badges42 bronze badges
asked Sep 19, 2012 at 8:07

1 Answer 1

20

Test first. TDD is also IOW called "test-first development".

What you have been working at is not TDD.

TDD is 1-2-3:

  1. Write a failing test.
  2. Make it pass (committing any sins and shortcuts during the way).
  3. Remove duplicities (logical, not just physical).

In shorter words this is also advertised as "make it fail / make it work / make it right".

More in Kent Beck's awesome book Test-Driven Development By Example.

gnat
20.5k29 gold badges117 silver badges308 bronze badges
answered Sep 19, 2012 at 8:24
14
  • 11
    +1 These three steps are also known as "Red-Green-Refactor". Commented Sep 19, 2012 at 8:53
  • 2
    Make it pass means: Write only so much code that It pass, not more! That's quite important. Commented Sep 19, 2012 at 9:30
  • 2
    It used to be called Test-First Development, but that is misleading: it's not about writing your tests first, it's about letting your tests drive the design and development. Writing your tests first is simply a prerequisite for that – how can your tests drive you if they don't exist yet? But you can write your tests first and still not let them drive you, and then you are not doing TDD. Commented Sep 19, 2012 at 10:08
  • 3
    @deltree erm... I disagree with every statement you made! Commented Sep 19, 2012 at 13:43
  • 2
    except "code coverage != TDD" :p Commented Sep 19, 2012 at 14:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.