0

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 :(

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Ferny84
0

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.

0

I'm sorta getting it, thanks.

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.