Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

.net - What do you test with your unit tests?

TDD is something that seems to be on everybody's lips these days, and I have tried some on my own but I don't think I'm getting the idea. I am getting a grip on how to write a unit test, but I don't understand exactly what my unit tests should test.

  1. If I have an action method that returns a list of data, what should I verify? Only that the view name is correct, or should I verify the data as well?
  2. If I should test the data as well, won't I be writing the same code twice? What is the use of testing the data, if I use the same method to retrieve the data I'm comparing to?
  3. Should I test the methods adding/editing my data too? How do I verify that a record has been added/edited/removed, in a correct way?

I know it's quite a lot of large questions, but I haven't become any wiser from reading articles on the internet, as they all seem to be concerned with how to test, and not with what.

As an example - I have (or, am going to write) a GuestbookController, with methods for viewing, adding, editing and removing posts. What do I need to test? How do I do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unit Testing (UT) != Test Driven Design (TDD)

This confusion seems to be fairly common. UT is all about code coverage. TDD is concerned with features. They are not the same thing [sorry Joel!]

With UT, you write whatever code you want to, then go back and test every single function (even some of the trivial ones).

With TDD, you select the next feature and write the test for that feature first. Write only the test for that feature, and test coverage is irrelevant. You write the test first to force interface decisions to be made up front. Then you write the code to pass the test (bearing in mind the 'simplest thing that can possibly work'). Then you refactor the code based on what you've learned. Then you go on to the next feature (presumably after check-in and re-running all unit tests).

If desired, develop using TDD then go back and complete coverage with UT tools. If you're creating a class library or other API for developers to use, the more test coverage the better ;-)

If you're just writing an app to do five specific things, TDD alone should be sufficient.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...