We're supposed to create a program using TDD in our CS class but i'm not entirely sure how it works...

You're supposed to create a fail test before you write code...how can you do that?

I can't find any examples online :(

Test Driven Development is a methodology where you write test cases before you write the actual code, then write code to pass the tests and no more. For example, say we were writing a class to describe a square and we want a method that gives us the perimeter of the square. We can immediately think of three test cases that must always be true for our square: Each side MUST be greater than 0 units in length; each side MUST be equal in length; and the perimeter is equal to 4 times the length of one side.

With these test cases in mind, we can write the following code:

class Square
{
   int sideLength;
   public Square(int aSideLength)
   {
      if (aSideLength <= 0)
         throw new ArgumentOutOfRangeException("Side must be greater than 0");
      sideLength = aSideLength;
   }
   public int Perimeter()
   {
      return 4 * sideLength;
   }
}

It should be obvious that the above code passes all three test scenarios.

I'm sorta getting it, thanks.

I'm just wondering if you have to test functions, do you do it within int main?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.