<blockquote>
<p>Unit testing is, roughly speaking, testing bits of your code in isolation with test code. The immediate advantages that come to mind are:</p>
<ul>
<li>Running the tests becomes automate-able and repeatable</li>
<li>You can test at a much more granular level than point-and-click testing via a GUI</li>
</ul>
<p><a href="https://stackoverflow.com/questions/1383/what-is-unit-testing">Rytmis</a></p>
</blockquote>
<p>My question is, what are the current &quot;best practices&quot; in terms of tools as well as when and where to use unit testing as part of your daily coding?</p>
<p>Lets try to be somewhat language agnostic and cover all the bases.</p>

[{'answer_id': 16880, 'author': 'Steve', 'author_id': 2019, 'author_profile': 'https://Stackoverflow.com/users/2019', 'pm_score': 1, 'selected': False, 'text': "<p>The xUnit family are the mainstay of unit testing. They are integrated into the likes of Netbeans, Eclipse and many other IDEs. They offer a simple, structured solution to unit testing.</p>\n\n<p>One thing I always try and do when writing a test is to minimise external code usage. By that I mean: I try to minimise the setup and teardown code for the test as much as possible and try to avoid using other modules/code blocks as much as possible. Well-written modular code shouldn't require too much external code in it's setup and teardown.</p>\n"}, {'answer_id': 16881, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 2, 'selected': False, 'text': '<p>The so-called <a href="http://en.wikipedia.org/wiki/XUnit" rel="nofollow noreferrer">xUnit</a> framework is widely used. It was originally developed for Smalltalk as SUnit, evolved into JUnit for Java, and now has many other implementations such as NUnit for .Net. It\'s almost a de facto standard - if you say you\'re using unit tests, a majority of other developers will assume you mean xUnit or similar.</p>\n'}, {'answer_id': 16893, 'author': 'Craig', 'author_id': 1611, 'author_profile': 'https://Stackoverflow.com/users/1611', 'pm_score': 2, 'selected': False, 'text': '<p>A great resource for \'best practices\' is the <a href="http://googletesting.blogspot.com/" rel="nofollow noreferrer">Google Testing Blog</a>, for example a recent post on <a href="http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-decided-to.html" rel="nofollow noreferrer">Writing Testable Code</a> is a fantastic resource. Specifically their \'Testing on the Toilet\' series weekly posts are great for posting around your cube, or toilet, so you can always be thinking about testing. </p>\n'}, {'answer_id': 16894, 'author': 'Johnno Nolan', 'author_id': 1116, 'author_profile': 'https://Stackoverflow.com/users/1116', 'pm_score': 6, 'selected': True, 'text': '<p>Ok here\'s some best practices from some one who doesn\'t unit test as much as he should...cough.</p>\n\n<ol>\n<li>Make sure your tests test <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=35578" rel="noreferrer" title="one">one</a>\nthing and one thing only.</li>\n<li>Write unit tests as you go. Preferably <a href="http://en.wikipedia.org/wiki/Test-driven_development" rel="noreferrer" title="before">before</a> you write the code you are testing against.</li>\n<li>Do not unit test the GUI. </li>\n<li><a href="http://en.wikipedia.org/wiki/Separation_of_concerns" rel="noreferrer" title="Separate your concerns">Separate your concerns</a>. </li>\n<li>Minimise the dependencies of your tests.</li>\n<li>Mock behviour with <a href="http://en.wikipedia.org/wiki/Mock_object" rel="noreferrer" title="mocks">mocks</a>.</li>\n</ol>\n'}, {'answer_id': 16919, 'author': 'Tilendor', 'author_id': 1470, 'author_profile': 'https://Stackoverflow.com/users/1470', 'pm_score': 0, 'selected': False, 'text': "<p>NUnit is a good tool for any of the .NET languages.</p>\n\n<p>Unit tests can be used in a number of ways: </p>\n\n<ol>\n<li>Test Logic</li>\n<li>Increase separation of code units.  If you can't fully test a function or section of code, then the parts that make it up are too interdependant.</li>\n<li>Drive development, some people write tests <em>before</em> they write the code to be tested.  This forces you to think about what you want the code to <em>do</em>, and then gives you a definite guideline on when you have acheived that.</li>\n</ol>\n"}, {'answer_id': 17101, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 4, 'selected': False, 'text': '<p>You might want to look at <a href="http://blog.objectmentor.com/articles/2008/03/06/tdd-on-three-index-cards" rel="noreferrer">TDD on Three Index Cards</a> and <a href="http://blog.briandicroce.com/2008/03/14/three-index-cards-to-easily-remember-the-essence-of-test-driven-development/" rel="noreferrer">Three Index Cards to Easily Remember the Essence of Test-Driven Development</a>:</p>\n\n<p>Card #1. Uncle Bob’s Three Laws</p>\n\n<ul>\n<li>Write no production code except to pass a failing test.</li>\n<li>Write only enough of a test to demonstrate a failure.</li>\n<li>Write only enough production code to pass the test.</li>\n</ul>\n\n<p>Card #2: FIRST Principles</p>\n\n<ul>\n<li>Fast: Mind-numbingly fast, as in hundreds or thousands per second.</li>\n<li>Isolated: The test isolates a fault clearly.</li>\n<li>Repeatable: I can run it repeatedly and it will pass or fail the same way each time.</li>\n<li>Self-verifying: The Test is unambiguously pass-fail.</li>\n<li>Timely: Produced in lockstep with tiny code changes.</li>\n</ul>\n\n<p>Card #3: Core of TDD</p>\n\n<ul>\n<li>Red: test fails</li>\n<li>Green: test passes</li>\n<li>Refactor: clean code and tests</li>\n</ul>\n'}, {'answer_id': 31282, 'author': 'Thomas Eyde', 'author_id': 3282, 'author_profile': 'https://Stackoverflow.com/users/3282', 'pm_score': 0, 'selected': False, 'text': "<p>Don't forget refactoring support. ReSharper on .NET provides automatic refactoring and quick fixes for missing code. That means if you write a call to something that does not exist, ReSharper will ask if you want to create the missing piece.</p>\n"}]
['https://Stackoverflow.com/questions/16860', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/149/']