Tests are needed to do real refactoring
Most of you guys already write tests. Time and time again I see developers trash the concept of testing, yet they write tests, just informally.
I used to do just that. I’d have a tests/ folder. In there’d I’d put scripts that tested my code:
<?php
$user= new User(1);
var_dump( $user->load() );
?>
(then I’d load that in the browser, as I edited code I could reload and see how my changed took effect. I wrote code that gave me feedback).
In the early 60s and 70s programmers did this with computer print outs. Developers working on large systems would write programs that would print literally 100s of pages of output. One day someone said why not make the tests more “granular”.
“tests” in the TDD sense just make it more granular, in terms of assertions. You don’t have to test at a low level for it to be rediculous. If you tried that and had too many tests, you did it wrong. You want your tests to read like a high level language. Your tests themselves should have “high level” utility methods for doing “test stuff”. Your tests should read like English. They are executable specifications.
Take a look at the tests for Swift Mailer, or Zend Framework (more complex)
Don’t think of tests as tests. Think of them as executible specifications. The intention of TDD is NOT to test your application. That is why people came up with BDD to make that more clear.
There’s software testing, and then theres test driven development. I am an advocate of one not the other. Tests are necessary to build software that would otherwise yield high cyclomatic complexity. When you start building frameworks, or programming languages, that have infinite real world use scenarios, it becomes necessary to to TDD, in my opinion.
If you can’t go into your code and just start renaming stuff (example methods) that you think would be better renamed, then you aren’t well off. In a good TDD system its almost impossible to find duplicated code. Think of TDD like assisted programming. There is much less fear involved. After a while of doing real refactoring you can program real fast. Need to move some methods from one object to the other? Just cut and paste. RUn the tests. They fail? Oh we forgot a variable scope, update that, run the tests again. Still fail? Oh forgot to rename something else, run them again, they pass. Commit our code, write new tests… its a rapid cycle.
Try it for 30 days. Don’t write tests that test your setters and getters. Do it with something that has real behavior. (example I use it to develop my www.vehiclefits.com software. I feel like I would not have produced software that was as extensive otherwise).
DOnt think of it as a bug prevention technique. Its a software methodology.