jolz wrote:
>You should write tests for each new line (or two) of the code. Never
write
new code without a failing test.
Allways?
If you can't think of the test, how will you think of the line of code?
The test doesn't need to perfectly prove the line will work. In the
degenerate case, the test simply checks the line exists. Another line could
fool the test. Don't do that, and add different tests with different attack
angles, to triangulate the code you need.
How do I test concurrency?
Developer tests are only import for rapid development. Not for (shocked
gasp) proving a program works. If you simply must go concurrent, write a
good structure for your semaphores, enforce this _structure_ with tests, and
then soak-test your final application, just to check for CPU bugs and such.
How do I test that after adding new button my gui looks ugly?
If that's important (it usually _isn't_), you make reviewing the GUI very
easy:
http://www.zeroplayer.com/cgi-bin/wiki?TestFlea
How do I test proper excepion handling after disk failure?
Sometimes you let things like this slide. Yes, you occassionally write a
line of code without a test. The difference is Ayou could have written the
test, and Byour code follows community agreements about robustness.
For example, an in-house tool application should simply die and tell the
user to report to the help-desk. A commercial application, by contrast,
shouldn't make those assumptions.
A test on disk failure should use a Mock Object to mock out the file system.
That will return an error on command, so a test can trivially ensure the
error gets handled correctly.
How do I test efficiency of used algorithm?
You don't need to do that to test-first each line of the algorithm. You need
to know what the algorithm is, so you can implement it under test. If you
don't know the algorithm yet, test-first might help discover it, but you
still need other design techniques.
If the algorithm is important, then it should have tests that catch when any
detail of its internal operations change. So only implement important things
via test-first.
There is a lot of code that making an unit test for is useless.
Uh, then why write the code?
If you assume a unit test is expensive, then you will never get over that
curve and make them cheap.
If, instead, you start with unit tests, they accelerate development. For
each new line of code, the unit test is easier to write than debugging that
line would be.
Don't tell my bosses this, but I have programs that I almost never manually
drive. So, for me, there's a lot of code that manual testing is useless!
Unit test are really nice especially when code changes - if test passes
after the change that's a hint that maybe nothing was broken. But they
are also dangerous.
So get them reviewed as often as you review the code. Also, tests make an
excellent way to document the code, and you should help your business
analysts review this code.
Many times I'v heard that if test passes than
everything works.
Nobody should believe that, even when you have 100% statement coverage, and
it appears true!
Actually often it means that there are too little
tests. Or maybe every single function works, but together they don't
work (especially true in multithread environment).
Don't multithread. Do write tests that re-use lots of objects in
combination. (Don't abuse Mock Objects, for example.) Your design should be
so loosely coupled that you can create any object under test without any
other object.
Or maybe new
functionality that was added introduces several hundred of new states
in application that wans't considered when existing test was written.
Yep. But the odds you _know_ you did that are very high.
Also probanly everybody who write tests spent a lot of time debuging
working code and finding that the mistake was in the test.
Uh, "debugging" ?
Under test-first, if the tests fail unexpectedly, you hit Undo until they
pass. That's where most debugging goes.
If you try the feature again, and the tests fail again, you Undo and then
start again, at a smaller scale, with a _smaller_ change. If this passes, do
it again. You might find the error in the tests like this, because it will
relate to the line of code you are trying to change.
This is like debugging, but with one major difference. Each and every time
you get a Green Bar, you could integrate and ship your code.
So a test-rich environment is like having a big button on your editor called
"Kill Bug". Invest in the power of that button.
Yes, test are important, but I tkink that they often overrated. I'v
often heard that TDD's goal is to make it green. Beware of red. Make it
as simple as possible. But I don't recall hearing the word "design".
If you are reading the blogs and such, they often preach to the choir. They
know you know it's about "design".
Google for "emergent design" to learn the full power of the system.
--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!