Beginning Test Driven Development
page 2 of 7
by Brendan Enrick
Average Rating: 
Views (Total / Last 10 Days): 35032/ 73

Creating Tests First

One comment that comes up often when people hear that they are supposed to write a test before they write their code is, "that it is just going to create a compiler error". Well, that is correct, but keep in mind that I said there are a variety of ways to do things. You do not have to create the tests in advance if you don't want to, but I suggest that you do. We have at least a couple of reasons why we create the tests first; it helps ensure we are testing the correct thing and it also gives the chance to come up with the desired interface to use for the code you will be testing.

Ensuring the Test is Accurate

I will tell you, that you will, at some point, write a test method which you expect to fail, but instead it passes. Plenty of reasons exist which can cause this to occur, but at the end of the day it means that your test is not accurately testing your code. All it takes to make a test pass by mistake is; swapping a less than or greater than, forgetting an assert statement, or sometimes things just do not work as you believe they do. Writing the test first allows you to confirm your theory of how everything is working.

Creating the Desired Interface

Sometimes developers will spend a great deal of time trying to figure out what interface they want to use for their code. How do you want to interact and use some piece of code? Well, one of the best ways to figure it out is to come up with a use case and attempt to use it. Tests make great use cases; they demonstrate how to use a particular class or method, and what the result of that usage is. So if we assume that the test uses our class, we are able to come up with the interface we want to use to access the class before we have written it. What does this mean? Well, it means that we now know how to design the interaction, because we've just tried to use it.

If we did not do things this way, we would be designing based on a guess of how we would want to interact with the code we are going to create. Keep this in mind when judging the value of writing tests in advance of code changes. I believe it is useful and helpful, but this is one of the points where you can easily go either way.

View Entire Article

User Comments

No comments posted yet.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-02-25 7:28:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search