Watch test fails
Why is that so important to watch the test fails right af开发者_JAVA百科ter writing it? How that makes sense?
I know that it will fail, time wasting.
There are a lot of advantages to writing a failing test.
Fundamentally: if you write a passing test, you aren't doing test-driven development, because the development that causes the test to pass happened before the test was written. Similarly for test-driven design, if you prefer that phrase (as I do); the design that led to the development that passes the test happened before you wrote the test. But that is, to some extent, just definitional; the terms and the process of TDD may not be that important to you.
On a more practical level, failing tests lead to to smaller solutions. When you write just enough code to pass a single failing test, you (frequently) add only a bit to what you had before. You know that that new bit is the part of your code that satisfies that test; you know why it is there. And then you write the next failing test, for the next small incremental bit of functionality. You don't overengineer your solution; you build just enough.
Because failing tests lead you to small-step incremental development, your development flow is better. Instead of long periods of heads-down coding, punctuated by frantic episodes of debugging and discarding, you move steadily forward. When you do discard, what you discard is very small; it's easy to start over because you are not throwing away hours or days of work. When you debug, you know the problem is (very probably) in a very small part of new code.
Failing tests inform you about your code, and (more important) your understanding of your code; they tell you either "Yes, your understanding that the code didn't do X is correct, and you have expressed that understanding correctly and concretely," or "No - something's wrong". Brand new passing tests, on the other hand, only tell you "Yes, it seems to be working," but that can sometimes be because the test is wrong.
So that you can make sure you wrote your test correctly. If it succeeds, then you know that you screwed up in writing your test.
Saying "I know that it will fail" is related to "I know that it will succeed". And if the later would really be true your program wouldn't have any bugs ;-) Same thing with failing a test: you only really know once you've tried.
Actually the point is to watch the message of the failing test. How it captures the problem with your code? How expressive is it? You have to adjust your test until the message exactly says what's the problem with your code. It will make your life easier a lot when the test will fail in the future when you brake something and that's the point here.
精彩评论