views:

118

answers:

7

Before i can understand unit testing, i have to see real world examples. Every book, blog, article, or answer i've seen gives hypothetical examples that don't apply to the/my real world.

i really don't want to flood StackOverflow with hundreds of questions all titled "How do i unit-test this?" There must be another place i can go to ask for real solutions.

Where can i go to get practical answers to unit-testing questions?


Note: i would give an example question, but then people would get grumpy when i asked the 200 follow-up questions.

+8  A: 

Despite your concern about asking the questions here, I have to say that my answer would be "here".

lance
+1 Agreed......
Skilldrick
+6 says here on SO. Very well then.
Ian Boyd
A: 

If you use dependency injection it is very straight-forward to test your code. If, on the other hand, you write tightly coupled code it can be very hard to write unit tests.

And, by all means, ask your questions here. I doubt that you have 200 radically different questions.

klausbyskov
One reason for unit-testing is the improvement on design and reusability of your code, as tested code has sometimes to be slightly rewritten to be testable and that is most often an improvement.
Mnementh
+3  A: 

If you want to see real unit tests in real projects, I recommend checking out some open source projects.

If you have practical questions, post them here - I don't think anyone would get grumpy about 200 clear and useful questions (seeing your reputation, you obviously know and follow the SO conventions :-)

Péter Török
A: 

What do you want the unit in question to do? For each item you mention in the answer, develop a test that shows the unit meets that individual specification. E.G., "the unit should allow authorized users to look up part numbers in the database and get dimensions." From that we develop tests:

  • Accept authorized users
  • Reject unauthorized users (several variants bad authentication, bad username, time elapsed, etc)
  • Get part numbers
  • Validate part numbes
  • Access DB readonly
  • Get info from DB
  • Return info to users (format? unit conversions?)
  • etc.

Hope that helps you figure out how to develop your tests.

mpez0
+2  A: 

In my opinion, you should feel free to post questions on SO. But... also get the bible:

alt text

Pascal Thivent
@Pascal Thivent The best. POJO in Action also gives you a good insight
Arthur Ronald F D Garcia
A: 

Probably its worth reading a book about testing and test driven development first, it will provide design patterns and best practises that will allow you to solve most of the real life problems (TDD is not really difficult, trust me :). My recommendation: http://www.growing-object-oriented-software.com/

If this doesn't help you can ask here :)

Grzenio
A: 

I recommend that you read Working Effectively with Legacy Code if you plan to write unit tests for your legacy code.

alt text

Bruno Rothgiesser