<p>OK, I know there have already been questions about <a href="https://stackoverflow.com/questions/4303/why-should-i-practice-test-driven-development-and-how-should-i-start">getting started with TDD</a>.. However, I guess I kind of know the general concensus is to <em>just do it</em> , However, I seem to have the following problems getting my head into the game:</p>

<ul>
<li>When working with collections, do will still test for obvious add/remove/inserts successful, even when based on Generics etc where we kind of "know" its going to work?</li>
<li>Some tests seem to take forever to implement.. Such as when working with string output, is there a "better" way to go about this sort of thing? (e.g. test the object model before parsing, break parsing down into small ops and test there) In my mind you should always test the "end result" but that can vary wildly and be tedious to set up.</li>
<li>I don't have a testing framework to use (work wont pay for one) so I can "practice" more. Are there any good ones that are free for commercial use? (at the moment I am using good 'ol <em>Debug.Assert</em> :)</li>
<li>Probably the biggest.. Sometimes I don't know what to expect <em>NOT</em> to happen.. I mean, you get your green light but I am always concerned that I may be missing a test.. Do you dig deeper to try and break the code, or leave it be and wait for it all fall over later (which will cost more)..</li>
</ul>

<p>So basically what I am looking for here is not a " <em>just do it</em> " but more " <em>I did this, had problems with this, solved them by this</em> ".. The <strong>personal</strong> experience :)</p>

[{'answer_id': 24967, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 3, 'selected': False, 'text': '<p>From my own experience:</p>\n\n<ol>\n<li><p>Only test your own code, not the underlying framework\'s code. So if you\'re using a generic list then there\'s no need to test Add, Remove etc.</p></li>\n<li><p>There is no 2. Look over there! Monkeys!!!</p></li>\n<li><p><a href="http://www.nunit.org" rel="nofollow noreferrer">NUnit</a> is the way to go.</p></li>\n<li><p>You definitely can\'t test every outcome. I test for what I expect to happen, and then test a few edge cases where I expect to get exceptions or invalid responses. If a bug comes up down the track because of something you forgot to test, the first thing you should do (before trying to fix the bug) is write a test to prove that the bug exists.</p></li>\n</ol>\n'}, {'answer_id': 24969, 'author': 'Erik Öjebo', 'author_id': 276, 'author_profile': 'https://Stackoverflow.com/users/276', 'pm_score': 0, 'selected': False, 'text': "<p>I am no expert at TDD, by any means, but here is my view:</p>\n\n<ul>\n<li>If it is completely trivial (getters/setters etc) do not test it, unless you don't have confidence in the code for some reason.</li>\n<li>If it is a quite simple, but non-trivial method, test it. The test is probably easy to write anyway.</li>\n<li>When it comes to what to expect not to happen, I would say that if a certain potential problem is the responsibility of the class you are testing, you need to test that it handles it correctly. If it is not the current class' responsibility, don't test it.</li>\n</ul>\n\n<p>The xUnit testing frameworks are often free to use, so if you are a .Net guy, check out NUnit, and if Java is your thing check out JUnit.</p>\n"}, {'answer_id': 24978, 'author': 'Dima Malenko', 'author_id': 2586, 'author_profile': 'https://Stackoverflow.com/users/2586', 'pm_score': 2, 'selected': False, 'text': '<p>My take on this is following:</p>\n\n<ul>\n<li>+1 for not testing framework code, but you may still need to test classes derived from framework classes.</li>\n<li>If some class/method is cumbersome to test it may be strong indication that something is wrong with desing. I try to follow "1 class - 1 responsibility, 1 method - 1 action" principle. That way you will be able to test complex methods much easier by doing that in smaller portions.</li>\n<li>+1 for xUnit. For Java you may also consider <a href="http://testng.org" rel="nofollow noreferrer">TestNG</a>.</li>\n<li>TDD is not single event it is a process. So do not try to envision everything from the beginning, but make sure that every bug found in code is actually covered by test once discovered.</li>\n</ul>\n'}, {'answer_id': 24981, 'author': 'Eric Scrivner', 'author_id': 2594, 'author_profile': 'https://Stackoverflow.com/users/2594', 'pm_score': 0, 'selected': False, 'text': '<p>The above advice is good, and if you want a list of free frameworks you have to look no farther than the <a href="http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks" rel="nofollow noreferrer" title="xUnit Frameworks List">xUnit Frameworks List</a> on Wikipedia.  Hope this helps :)</p>\n'}, {'answer_id': 25013, 'author': 'On Freund', 'author_id': 2150, 'author_profile': 'https://Stackoverflow.com/users/2150', 'pm_score': 2, 'selected': False, 'text': '<p>I think the most important thing with (and actually one of the great outcomes of, in a somewhat recursive manner) TDD is successful management of dependencies. You have to make sure that modules are tested in isolation with no elaborate setup needed. For example, if you\'re testing a component that eventually sends an email, make the email sender a dependency so that you can mock it in your tests.\nThis leads to a second point - mocks are your friends. Get familiarized with mocking frameworks and the style of tests they promote (behavioral, as opposed to the classic state based), and the design choices they encourage (The <a href="http://www.pragprog.com/articles/tell-dont-ask" rel="nofollow noreferrer">"Tell, don\'t ask"</a> principle).</p>\n'}, {'answer_id': 25015, 'author': 'Chris B-C', 'author_id': 1517, 'author_profile': 'https://Stackoverflow.com/users/1517', 'pm_score': 0, 'selected': False, 'text': '<p>In my opinion (your mileage may vary):</p>\n\n<p>1- If you didn\'t write it don\'t test it. If you wrote it and you don\'t have a test for it it doesn\'t exist. </p>\n\n<p>3- As everyone\'s said, xUnit\'s free and great.</p>\n\n<p>2 &amp; 4- Deciding exactly what to test is one of those things you can debate about with yourself forever. I try to draw this line using the principles of design by contract. Check out \'Object Oriented Software Construction" or "The Pragmatic Programmer" for details on it.</p>\n'}, {'answer_id': 25025, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 2, 'selected': False, 'text': '<p>I found that the principles illustrated in the <a href="http://blog.briandicroce.com/2008/03/14/three-index-cards-to-easily-remember-the-essence-of-test-driven-development/" rel="nofollow noreferrer">Three Index Cards to Easily Remember the Essence of TDD</a> is a good guide.</p>\n\n<p>Anyway, to answer your questions</p>\n\n<ol>\n<li>You don\'t have to test something you "know" is going to work, unless you wrote it. You didn\'t write generics, Microsoft did ;)</li>\n<li>If you need to do so much for your test, maybe your object/method is doing too much as well.</li>\n<li>Download <a href="http://testdriven.net" rel="nofollow noreferrer">TestDriven.NET</a> to immediately start unit testing on your Visual Studio, (except if it\'s an Express edition)</li>\n<li>Just test the <em>correct thing that will happen</em>. You don\'t <em>need</em> to test everything that can go wrong: you have to wait for your tests to fail for that.</li>\n</ol>\n\n<p>Seriously, just do it, dude. :)</p>\n'}, {'answer_id': 25203, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 7, 'selected': True, 'text': '<p>First, it is alright and normal to feel frustrated when you first start trying to use TDD in your coding style.  Just don\'t get discouraged and quit, you will need to give it some time.  It is a major paradigm shift in how we think about solving a problem in code.  I like to think of it like when we switched from procedural to object oriented programming.</p>\n\n<p>Secondly, I feel that test driven development is first and foremost a design activity that is used to flesh out the design of a component by creating a test that first describes the API it is going to expose and how you are going to consume it\'s functionality. The test will help shape and mold the System Under Test until you have been able to encapsulate enough functionality to satisfy whatever tasks you happen to be working on.</p>\n\n<p>Taking the above paragraph in mind, let\'s look at your questions:</p>\n\n<ol>\n<li>If I am using a collection in my system under test, then I will setup an expectation to make sure that the code was called to insert the item and then assert the count of the collection.  I don\'t necessarily test the Add method on my internal list.  I just make sure it was called when the method that adds the item is called.  I do this by adding a mocking framework into the mix, with my testing framework.</li>\n<li>Testing strings as output can be tedious.  You cannot account for every outcome.  You can only test what you expect based on the functionality of the system under test.  You should always break your tests down to the smallest element that it is testing.  Which means you will have a lot of tests, but tests that are small and fast and only test what they should, nothing else.</li>\n<li>There are a lot of open source testing frameworks to choose from.  I am not going to argue which is best.  Just find one you like and start using it.\n\n<ul>\n<li><a href="http://www.mbunit.com/" rel="nofollow noreferrer">MbUnit</a></li>\n<li><a href="http://www.nunit.org/index.php" rel="nofollow noreferrer">nUnit</a></li>\n<li><a href="http://www.codeplex.com/xunit" rel="nofollow noreferrer">xUnit</a></li>\n</ul></li>\n<li>All you can do is setup your tests to account for what you want to happen.  If a scenario comes up that introduces a bug in your functionality, at least you have a test around the functionality to add that scenario into the test and then change your functionality until the test passes.  One way to find where we may have missed a test is to use <a href="http://www.ncover.com/" rel="nofollow noreferrer">code coverage</a>.</li>\n</ol>\n\n<p>I introduced you to the mocking term in the answer for question one.  When you introduce mocking into your arsenal for TDD, it dramatically makes testing easier to abstract away the parts that are not part of the system under test.  Here are some resources on the mocking frameworks out there are:</p>\n\n<ul>\n<li><a href="http://code.google.com/p/moq/" rel="nofollow noreferrer">Moq</a>: Open Source</li>\n<li><a href="http://www.ayende.com/projects/rhino-mocks.aspx" rel="nofollow noreferrer">RhinoMocks</a>: Open Source</li>\n<li><a href="http://www.typemock.com/" rel="nofollow noreferrer">TypeMock</a>: Commercial Product</li>\n<li><a href="http://nsubstitute.github.com/" rel="nofollow noreferrer">NSubstitute</a>: Open Source</li>\n</ul>\n\n<p>One way to help in using TDD, besides reading about the process, is to watch people do it.  I recommend in watching the screen casts by JP Boodhoo on <a href="http://www.dnrtv.com/archives.aspx" rel="nofollow noreferrer">DNRTV</a>.  Check these out:</p>\n\n<ul>\n<li><a href="http://www.dnrtv.com/default.aspx?showNum=10" rel="nofollow noreferrer">Jean Paul Boodhoo on Test Driven Development Part 1</a></li>\n<li><a href="http://www.dnrtv.com/default.aspx?showNum=11" rel="nofollow noreferrer">Jean Paul Boodhoo on Test Driven Development Part 2</a></li>\n<li><a href="http://www.dnrtv.com/default.aspx?showNum=63" rel="nofollow noreferrer">Jean Paul Boodhoo on Demystifying Design Patterns Part 1</a></li>\n<li><a href="http://www.dnrtv.com/default.aspx?showNum=65" rel="nofollow noreferrer">Jean Paul Boodhoo on Demystifying Design Patterns Part 2</a></li>\n<li><a href="http://www.dnrtv.com/default.aspx?showNum=68" rel="nofollow noreferrer">Jean Paul Boodhoo on Demystifying Design Patterns Part 3</a></li>\n<li><a href="http://www.dnrtv.com/default.aspx?showNum=71" rel="nofollow noreferrer">Jean Paul Boodhoo on Demystifying Design Patterns Part 4</a></li>\n<li><a href="http://www.dnrtv.com/default.aspx?showNum=92" rel="nofollow noreferrer">Jean Paul Boodhoo on Demystifying Design Patterns Part 5</a></li>\n</ul>\n\n<p>OK, these will help you see how the terms I introduced are used.  It will also introduce another tool called <a href="http://www.jetbrains.com/resharper/" rel="nofollow noreferrer">Resharper</a> and how it can facilitate the TDD process.  I couldn\'t recommend this tool enough when doing TDD.  Seems like you are learning the process and you are just finding some of the problems that have already been solved with using other tools.</p>\n\n<p>I think I would be doing an injustice to the community, if I didn\'t update this by adding Kent Beck\'s new series on <a href="http://www.pragprog.com/screencasts/v-kbtdd/test-driven-development" rel="nofollow noreferrer">Test Driven Development on Pragmatic Programmer</a>.</p>\n'}, {'answer_id': 25270, 'author': 'Chris Porter', 'author_id': 1725, 'author_profile': 'https://Stackoverflow.com/users/1725', 'pm_score': 0, 'selected': False, 'text': '<p>Keep tests short, "atomic". Test the smallest assumption in each test. Make each TestMethod independent, for integration tests I even create a new database for each method. If you need to build some data for each test use an "Init" method. Use mocks to isolate the class your testing from it\'s dependencies. </p>\n\n<p>I always think "what\'s the minimum amount of code I need to write to prove this works for all cases ?"</p>\n'}, {'answer_id': 82041, 'author': 'Hamish Smith', 'author_id': 15572, 'author_profile': 'https://Stackoverflow.com/users/15572', 'pm_score': 0, 'selected': False, 'text': "<p>Over the last year I have become more and more convinced of the benefits of TDD.\nThe things that I have learned along the way: \n1) dependency injection is your friend. I'm not talking about inversion of control containers and frameworks to assemble plugin architectures, just passing dependencies into the constructor of the object under test. This pays back huge dividends in the testability of your code.\n2) I set out with the passion / zealotry of the convert and grabbed a mocking framework and set about using mocks for everything I could. This led to brittle tests that required lots of painful set up and would fall over as soon as I started any refactoring. Use the correct kind of test double. Fakes where you just need to honour an interface, stubs to feed data back to the object under test, mock only where you care about interaction.\n3) Test should be small. Aim for one assertion or interaction being tested in each test. I try to do this and mostly I'm there. This is about robustness of test code and also about the amount of complexity in a test when you need to revisit it later.</p>\n\n<p>The biggest problem I have had with TDD has been working with a specification from a standards body and a third party implementation of that standard that was the de-facto standard. I coded lots of really nice unit tests to the letter of the specification only to find that the implementation on the other side of the fence saw the standard as more of an advisory document. They played quite loose with it. The only way to fix this was to test with the implementation as well as the unit tests and refactor the tests and code as necessary. The real problem was the belief on my part that as long as I had code and unit tests all was good. Not so. You need to be building actual outputs and performing functional testing at the same time as you are unit testing. Small pieces of benefit all the way through the process - into users or stakeholders hands.</p>\n"}, {'answer_id': 113695, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 0, 'selected': False, 'text': '<p>Just as an addition to this, I thought I would say I have put a <a href="http://cantgrokwontgrok.blogspot.com/2008/09/tdd-getting-started-with-test-driven.html" rel="nofollow noreferrer">blog post</a> up on my thoughts on getting started with testing (following this discussion and my own research), since it may be useful to people viewing this thread.</p>\n\n<p>"<a href="http://cantgrokwontgrok.blogspot.com/2008/09/tdd-getting-started-with-test-driven.html" rel="nofollow noreferrer">TDD – Getting Started with Test-Driven Development</a>" - I have got some great feedback so far and would really appreciate any more that you guys have to offer.</p>\n\n<p>I hope this helps! :)</p>\n'}]
['https://Stackoverflow.com/questions/24965', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/832/']