3 ms·
Still, you can write your unit tests as you write your code. All a unit test does is verify that your code behaves the way you want it to. I think a lot of pe
by jamesgolick 19y ago
Still, you can write your unit tests as you write your code. All a unit test does is verify that your code behaves the way you want it to.
I think a lot of people here are confused about what exactly a unit test is. When you write a unit test, you don't say: "I want a bowling application, with such and such a scoring method, and such and such a pin layout".
It's more like - "I am writing a class (or portion of a class) here that's going to perform a certain function in my 'blue sky', exploratory phase project". If you don't know what your code is going to do, how can you really write it. That is to say, exploratory coding isn't just typing random characters. You have some ideas.
So, instead of just writing the first function/method/class/controller action/model, you write a test first, and watch it fail. Then, you implement your method. That way, you know that your method works.
You really don't need to have an overall big picture of your application to write tests. That view is more of a misunderstanding of testing than anything else.
- pius 19y agoExactly.
- davidw 19y agoSo basically, if you decide to toss out your method or class or whatever, you now have twice the code (or whatever the Test/Code ratio is) to throw away. That doesn't strike me as an optimal use of time. On the other hand, once I'm pretty sure of what things should look like, yeah, test cases are invaluable in demonstrating that everything works like it should, that corner cases don't break things, and that any new bugs stay fixed. Especially if other people have to work with the code, and don't know it well, they can wade into it with confidence that they can be confident they won't break anything (or at least are less likely to).