I am trying to get into the habit of writing a unit test before the actual class. What are some pointers the stack overflow community can give me along with any useful resources.
Thank You
I am trying to get into the habit of writing a unit test before the actual class. What are some pointers the stack overflow community can give me along with any useful resources.
Thank You
Here is a pretty good place to start. It contains a very good primer on TDD, with a comprehensive list of links to additional resources:
I have but one pointer:
Think about what a method should do and how it should behave, not how you're going to code it.
That will allow you to plan the Unit Tests without having any code written (or even planned out, for that matter).
The idea behind writing the test first is that you know what the code is going to do before you write it. So, you make a list of what you want it to do, and how you want each feature to be used. Then, you write a test for each feature (feel free to write more than one, or how ever many it takes to test the functionality of the object.)
Now, you have your framework for your class. Your first goal should be to make the test compile by adding all the methods used in the class. Then make the test pass by implementing the methods correctly.
Boom - Test Driven Development goes on your resume! :-)
I generally do my TDD (with PHPUnit) by authoring a set of business rules as part of the PHPDocs for my class or script. I don't write the code, just the business rules (e.g. Foo will throw an exception if..., etc.). After I have that in place I start writing test cases that address each business rule. I've found writing the business rules first helps me establish some boundaries and the test cases shape the actual code.