• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what does Test Driven Development Means?

 
sushil bharwani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been hearing this "Test Driven Development" from quite a time, People say that we write junit test cases earlier than we write the actual code, I am not sure how does that happens, when we dont have code what do we write test cases for ? also i want to know how to properly use junit in conjction with Agile and XP Programming


Thanks in Advance
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://c2.com/cgi/wiki?TestDrivenDevelopment has a fair description of TDD, accompanied with some discussion.

Originally posted by sushil bharwani:
when we dont have code what do we write test cases for?


Before you can write code, you need to know what that code should do (otherwise, how would you know what code to write?).

A test case is simply that knowledge expressed in executable code.

Say, for example, that you want to write a factorial function. One thing you know the code should do is that it should return 1 when it is passed 1 as a parameter. So you code that in a test:

assertEquals(1, factorial(1)); // boiler plate code ommitted

Now you have your first test and can start writing production code to make it pass.

Does that help?
 
sushil bharwani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you say , but one doubt you mentioned of : assertEquals(1,factorial(1)); If function factorial does not exists how will my code compile : or do you say that i write all the junit test cases with prototype methods; can you describe me when any requirments comes up for a project ; how should one proceed in for Agile Development Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something that doesn't compile counts as a failing test, so yes that's a valid starting point. One trick is to keep the cycle very small. Write one test that fails, write enough code to make it pass, repeat.

Robert Martin's Bowling Game Kata is a neat example of test first coding ... even though the solution is not particularly OO.

Note that there is no notion of writing a whole lot of tests up front. You should have some idea where you're going, but just code one at a time. One time you might write a test and not immediately code the solution is quitting time. It's nice to come in the next morning and have a failing test to remind you where you were.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic