Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

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.

share|improve this answer
10  
+1 These three steps are also known as "Red-Green-Refactor". –  Steve Fenton Sep 19 '12 at 8:53
1  
Make it pass means: Write only so much code that It pass, not more! That's quite important. –  ollins Sep 19 '12 at 9:30
    
@ollins: Yes. Or, from the bigger PoV, "Never write a line of code without a failing test.", that is, never write any piece of production code which is not necessitated by existence of failing test and which makes this failing test pass. Kent Beck calls this approach "test-infected". –  herby Sep 19 '12 at 9:36
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. –  Jörg W Mittag Sep 19 '12 at 10:08
2  
except "code coverage != TDD" :p –  Froome Sep 19 '12 at 14:04
show 9 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.