<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Being Cellfish : Test</title><link>http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx</link><description>Tags: Test</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Test readability</title><link>http://blogs.msdn.com/cellfish/archive/2009/06/11/test-readability.aspx</link><pubDate>Fri, 12 Jun 2009 07:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9729708</guid><dc:creator>cellfish</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9729708.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9729708</wfw:commentRss><description>&lt;P&gt;I read this excellent post on &lt;A href="http://www.jphamilton.net/post/Striving-for-Test-Readability.aspx" target=_blank mce_href="http://www.jphamilton.net/post/Striving-for-Test-Readability.aspx"&gt;test readability&lt;/A&gt;&amp;nbsp;a few days ago.&amp;nbsp;I like the way he shows how he refactors a test for readability&amp;nbsp;but personally I would have written the example test a little bit differently. First of all I would have added another extension method: &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;PRE&gt;&lt;DIV class=csharpcode&gt;
&lt;SPAN class=lnum&gt;   1:  &lt;/SPAN&gt;    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; PushOkButtonMultipleTimes(&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt; ILoginView view, &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; times)   
&lt;SPAN class=lnum&gt;   2:  &lt;/SPAN&gt;    {   
&lt;SPAN class=lnum&gt;   3:  &lt;/SPAN&gt;        &lt;SPAN class=kwrd&gt;for&lt;/SPAN&gt; (&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; i=0; i&amp;lt;times; i++)
&lt;SPAN class=lnum&gt;   4:  &lt;/SPAN&gt;            view.PushOkButton();   
&lt;SPAN class=lnum&gt;   5:  &lt;/SPAN&gt;    }
&lt;/DIV&gt;&lt;/PRE&gt;&lt;BR&gt;With that extension method my version of the test would have looked like this: &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;PRE&gt;&lt;DIV class=csharpcode&gt;
&lt;SPAN class=lnum&gt;   1:  &lt;/SPAN&gt;    [Test]
&lt;SPAN class=lnum&gt;   2:  &lt;/SPAN&gt;    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; only_three_login_attempts_are_allowed()
&lt;SPAN class=lnum&gt;   3:  &lt;/SPAN&gt;    {
&lt;SPAN class=lnum&gt;   4:  &lt;/SPAN&gt;        presenter = given_a_login_presenter_with_bad_credentials();
&lt;SPAN class=lnum&gt;   5:  &lt;/SPAN&gt;&amp;nbsp;
&lt;SPAN class=lnum&gt;   6:  &lt;/SPAN&gt;        view.PushOkButtonMultipleTimes(3);
&lt;SPAN class=lnum&gt;   7:  &lt;/SPAN&gt;&amp;nbsp;
&lt;SPAN class=lnum&gt;   8:  &lt;/SPAN&gt;        view.ShouldBeClosed();
&lt;SPAN class=lnum&gt;   9:  &lt;/SPAN&gt;    }
&lt;SPAN class=lnum&gt;  10:  &lt;/SPAN&gt;    
&lt;SPAN class=lnum&gt;  11:  &lt;/SPAN&gt;    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; ILoginView given_a_login_view_with_bad_credentials()
&lt;SPAN class=lnum&gt;  12:  &lt;/SPAN&gt;    {
&lt;SPAN class=lnum&gt;  13:  &lt;/SPAN&gt;        ILoginView view = CreateLoginView(&lt;SPAN class=str&gt;"BadUser"&lt;/SPAN&gt;, &lt;SPAN class=str&gt;"BadPassword"&lt;/SPAN&gt;);
&lt;SPAN class=lnum&gt;  14:  &lt;/SPAN&gt;        ILoginService loginService = CreateLoginService(&lt;SPAN class=kwrd&gt;false&lt;/SPAN&gt;);
&lt;SPAN class=lnum&gt;  15:  &lt;/SPAN&gt;&amp;nbsp;
&lt;SPAN class=lnum&gt;  16:  &lt;/SPAN&gt;        LoginPresenter presenter = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; LoginPresenter(loginService);
&lt;SPAN class=lnum&gt;  17:  &lt;/SPAN&gt;        presenter.Initialize(view);
&lt;SPAN class=lnum&gt;  18:  &lt;/SPAN&gt;        &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; view;
&lt;SPAN class=lnum&gt;  19:  &lt;/SPAN&gt;    }
&lt;/DIV&gt;&lt;/PRE&gt;
&lt;P&gt;I personally prefer to move as much as possible of test setup to separate methods that are called explicitly by the test. The use of the extension method to remove the three times duplicated &lt;EM&gt;PushOkButton&lt;/EM&gt; call is not something I'd always do. I was considering having the for-loop in the test method but I think an in place for-loop is less readable than making the calls three times. The extension method wrapping the for-loop however removes the duplication without decreasing the readability of the test.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9729708" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/BDD/default.aspx">BDD</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/TDD/default.aspx">TDD</category></item><item><title>When to write passing tests before fixing a bug</title><link>http://blogs.msdn.com/cellfish/archive/2009/04/21/when-to-write-passing-tests-before-fixing-a-bug.aspx</link><pubDate>Tue, 21 Apr 2009 23:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9559838</guid><dc:creator>cellfish</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9559838.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9559838</wfw:commentRss><description>&lt;P&gt;If you're a BDD/TDD practitioner you're probably used to create a failing test whenever you have a new bug reported and you want to fix it. But sometimes a situation arises where you have to do the opposite; creating a &lt;EM&gt;passing&lt;/EM&gt; test before you fix a bug. &lt;/P&gt;
&lt;P&gt;Let me explain with an example. Let's say that you have code that uses date and time to calculate something. It all works fine. Then you go into daylight savings time and the test fails. Your first reaction might be to change the code to work but since you only have one test you might end up changing the code so that in half a year when you stop using daylight savings time the test fails again. So a better approach to handle this problem is to first create a test that passes before you change the code. Once that is done you have your (old) failing test and you fix the problem. Now you know you have code that works both with and without daylight savings time.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9559838" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/BDD/default.aspx">BDD</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/TDD/default.aspx">TDD</category></item><item><title>CAT - Continuous Acceptance Testing</title><link>http://blogs.msdn.com/cellfish/archive/2009/01/17/cat-continuous-acceptance-testing.aspx</link><pubDate>Sat, 17 Jan 2009 12:17:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9331204</guid><dc:creator>cellfish</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9331204.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9331204</wfw:commentRss><description>&lt;P&gt;A few days ago I wrote about &lt;A class="" href="http://blogs.msdn.com/cellfish/archive/2009/01/05/cut-continuous-unit-testing.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2009/01/05/cut-continuous-unit-testing.aspx"&gt;Continuous Unit Testing&lt;/A&gt; (CUT) and why the need for CUT was &lt;A class="" href="http://blogs.msdn.com/cellfish/archive/2009/01/07/why-slow-unit-tests-are-a-bad-smell.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2009/01/07/why-slow-unit-tests-are-a-bad-smell.aspx"&gt;a sign of other problems&lt;/A&gt;. The last days I have been thinking about CUT a little bit more and I still find CUT questionable but there is another thing - CAT (Continuous Acceptance Testing) that I find very useful.&lt;/P&gt;
&lt;P&gt;Acceptance tests are typically slower than unit tests since they involve several components that are not faked in any way and that may need extensive setup work. So acceptance tests are often much slower than unit tests. The unit tests created as part of the BDD (or TDD) cycle are probably enough to catch most problems but in some cases things will be caught in automated acceptance tests. And since that is the case you have much to gain from running those acceptance tests as often as possible. So why not run them AL the time (continuously)&amp;nbsp;in the background?&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9331204" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/BDD/default.aspx">BDD</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/TDD/default.aspx">TDD</category></item><item><title>Why slow unit tests are a bad smell:</title><link>http://blogs.msdn.com/cellfish/archive/2009/01/07/why-slow-unit-tests-are-a-bad-smell.aspx</link><pubDate>Wed, 07 Jan 2009 12:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9273551</guid><dc:creator>cellfish</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9273551.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9273551</wfw:commentRss><description>&lt;P&gt;&lt;A class="" title="CUT - Continuous Unit Testing" href="http://blogs.msdn.com/cellfish/archive/2009/01/05/cut-continuous-unit-testing.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2009/01/05/cut-continuous-unit-testing.aspx"&gt;Earlier I promised to elaborate&lt;/A&gt; on why slow unit tests are a sign of problems (or a smell if you like). So here it goes.&lt;/P&gt;
&lt;P&gt;The first thing I would like to look at is when the complete test suite takes too long to run to be part of a tight TDD-loop. If it is just the number of tests that makes the difference and each single test is super fast you will have a very large number of tests. Probably several thousands of tests. This can only mean two things. The least bad thing is that you have a very large application that really need all these tests. But you could probably split things up into different modules and test each module by it self thus reducing the number of tests that need to be run when you're working on a single module. Any dependencies between the modules can be faked in your day to day work. A much worse problem is that you're over-testing your code. That means testing the same thing in several similar (but not identical) ways. While over-testing it self should not be a problem it impacts TDD bad in two ways. First of all writing more tests takes more time so you get the feel that TDD is much slower than "developing the old way". It also makes the feedback slower since tests takes longer to run and that means to loose focus.&lt;/P&gt;
&lt;P&gt;The second reason for slow tests is when a single test actually takes to long to execute just by itself. This is a sign of trouble since it probably means you're having badly written tests or even worse; badly designed production code. The reason for the test to be slow is typically because it takes some time to set up the circumstances for the test. The reason for this is typically a bad design since it is difficult or impossible to fake/stub/mock dependencies away in your test code. If you're having trouble faking dependencies you probably have a too tightly coupled design which most people I know tend to think is a bad design.&lt;/P&gt;
&lt;P&gt;So whenever you feel the complete test suite takes too long to run, don't start looking at what is wrong with the tests - start looking at what is wrong with your production code...&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9273551" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/BDD/default.aspx">BDD</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/TDD/default.aspx">TDD</category></item><item><title>CUT - Continuous Unit Testing</title><link>http://blogs.msdn.com/cellfish/archive/2009/01/05/cut-continuous-unit-testing.aspx</link><pubDate>Mon, 05 Jan 2009 12:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9273521</guid><dc:creator>cellfish</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9273521.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9273521</wfw:commentRss><description>&lt;P&gt;During the Christmas holidays I read about something I would like to call CUT (Continuous Unit Testing).&amp;nbsp;CUT is a technique where you run your unit tests in the background all the time rather than after each time you compile (I'm assuming TDD/BDD is being used all the time). There are a number of reasons I'm skeptical toward this approach.&lt;/P&gt;
&lt;P&gt;My first concern is why the tests are run in the background and not part of the regular build process. It could be because the build environment does not have the ability to run custom actions after each build. Hm... Let me rephrase that: &lt;EM&gt;The developer does not know how to add custom build steps to the build process&lt;/EM&gt;. And that is not the only problem. Every time I write a new test I have to wait for the background process to finish, start a new test run and wait for that result. This means the average turn around time each time I write a test is not only the time it takes to compile and make a single test run, it is the compile time plus 1.5 times the test time.&lt;/P&gt;
&lt;P&gt;Another argument might be that the use of a background process to run tests makes it possible to continue writing code while the tests run. But then you're not really following TDD practices, &lt;EM&gt;are you&lt;/EM&gt;? You're just mixing in your test &amp;amp; code writing continuously running the tests.&lt;/P&gt;
&lt;P&gt;But there is one relevant reason for running unit tests in the background. If it takes longer than you can wait to run all tests you will probably start running only a small subset of tests for the code you're actually working on and then run all tests just prior to check-in (or have the build server run all the tests for you). This will however give you feedback that you've changed something you didn't perceive quite late. Under these circumstances I think it makes perfectly sense to run all unit tests in the background and only have a small subset running each time you compile for that quick feedback in your TDD-loop.&lt;/P&gt;
&lt;P&gt;IMHO, having the ability to run unit tests fast is important if you are to be successful with TDD/BDD. And sometimes you end up with a number of tests that are not fast enough (or you have so many tests that they all together takes too long). Under these circumstances the background test runner makes sense but not otherwise. Also, having unit tests that are slow is typically a sign of problems but I think I have to elaborate on that later.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9273521" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/BDD/default.aspx">BDD</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/TDD/default.aspx">TDD</category></item><item><title>Yet another reason for loving xUnit.net</title><link>http://blogs.msdn.com/cellfish/archive/2008/11/26/yet-another-reason-for-loving-xunit-net.aspx</link><pubDate>Wed, 26 Nov 2008 13:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9138631</guid><dc:creator>cellfish</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9138631.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9138631</wfw:commentRss><description>&lt;P&gt;I recently noticed a thing about xUnit.net that makes me love that framework even more than &lt;A class="" title="My new favorite .Net unit test framework" href="http://blogs.msdn.com/cellfish/archive/2008/09/24/my-new-favorite-net-unit-test-framework.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2008/09/24/my-new-favorite-net-unit-test-framework.aspx"&gt;before&lt;/A&gt;. As you probably already know, an important aspect of (unit) tests is that they can be executed in any order. xUnit.net helps you remember this since it &lt;EM&gt;always&lt;/EM&gt; runs your tests in random order. So simple, so brilliant...&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9138631" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Tools/default.aspx">Tools</category></item><item><title>Dangers of using Visual Studio 2008 Team System Code Coverage Tool for Native C++</title><link>http://blogs.msdn.com/cellfish/archive/2008/11/18/dangers-of-using-visual-studio-2008-team-system-code-coverage-tool-for-native-c.aspx</link><pubDate>Tue, 18 Nov 2008 16:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9080834</guid><dc:creator>cellfish</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9080834.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9080834</wfw:commentRss><description>&lt;P&gt;So now you know how to get coverage reports for native C++ using Visual Studio 2008 Team System (if not - &lt;A class="" title="Native C++ Code Coverage reports using Visual Studio 2008 Team System" href="http://blogs.msdn.com/cellfish/archive/2008/11/16/native-c-code-coverage-reports-using-visual-studio-2008-team-system.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2008/11/16/native-c-code-coverage-reports-using-visual-studio-2008-team-system.aspx"&gt;read this&lt;/A&gt;). There are a few things you need to know before you get excited. First of all the only metrics you get are line and block coverage. A block is basically a statement and each line typically consists of one or more blocks. Unless you have 100% coverage I think these metrics both are pretty useless when measuring quality. For example consider a function consisting of ten lines of code. There is an IF-statement checking for an error and it throws an exception if the error occurs. If the error never occurs during the test-run you still get 90% line coverage since the other nine lines are executed. I think this is pretty common in production code. Most of the code is for the common state and fewer lines are used to handle errors. So you get pretty high line coverage even if you do not test any of error cases.&lt;/P&gt;
&lt;P&gt;Block coverage is even worse. For example consider the following line:&lt;/P&gt;&lt;PRE&gt;SimpleClass* o = &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; SimpleClass();&lt;/PRE&gt;
&lt;P&gt;That line produces two blocks of which only one is covered. And there is no reasonable way test the uncovered block since it probably has to do with when the process runs out of memory.&lt;/P&gt;
&lt;P&gt;Identifying functions that are not called at all is often considered an important part of the code coverage report. Here we have another problem with the visual studio tool. Functions never referenced by the code will be excluded from the report completely (I suspect this is the case since the linker will remove all unreferenced symbols as part of the optimization at link time). This means the following class will report 100% coverage it it is instantiated and only &lt;FONT face=courier&gt;GetA&lt;/FONT&gt; is called.&lt;/P&gt;&lt;PRE&gt;&lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; SimpleClass
{
&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;:
    SimpleClass(&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; a, &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; b)
        : m_a(0) , m_b(b)
    { }

    &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; GetA() { &lt;FONT color=#0000ff&gt;return&lt;/FONT&gt; m_a; }
    &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; GetB() { &lt;FONT color=#0000ff&gt;return&lt;/FONT&gt; m_b; }

&lt;FONT color=#0000ff&gt;private&lt;/FONT&gt;:
    &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; m_a;
    &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; m_b;
};&lt;/PRE&gt;
&lt;P&gt;So with all these potential problems there is another tool I'd recommend you consider. It's called &lt;A class="" href="http://www.bullseye.com/" target=_blank mce_href="http://www.bullseye.com/"&gt;BullsEye&lt;/A&gt;. It is a more "puristic" tool so there is no way to get block or line coverage, basically since those metrics are bad. In stead you can get Decision and Condition/Decision coverage. Basically Decision coverage checks that each conditional evaluates to both true and false and Condition/Decision coverage is when each part of a boolean expression is evaluated to both true and false. Consider the following line:&lt;/P&gt;&lt;PRE&gt;&lt;FONT color=#0000ff&gt;if&lt;/FONT&gt;(a || b)&lt;/PRE&gt;
&lt;P&gt;There are two different decisions (either "a || b" is true or false) but four different conditions (both "a" and "b" must evaluate to true and false). BullsEye also adds instrumentation at compile time so the &lt;FONT face=courier&gt;GetB&lt;/FONT&gt; method in the example above will not be lost but be part of the report as an uncovered function even if not referenced anywhere in the code. In the initial example (ten lines with 90% line coverage) we would get 50% decision coverage which is a much better indicator of quality.&lt;/P&gt;
&lt;P&gt;And on using code coverage as a quality metric...I must insist you read &lt;A class="" href="http://blogs.msdn.com/cellfish/archive/2008/06/16/code-coverage.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2008/06/16/code-coverage.aspx"&gt;one of my previous posts&lt;/A&gt; if you haven't done that already...&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9080834" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Tools/default.aspx">Tools</category></item><item><title>Native C++ Code Coverage reports using Visual Studio 2008 Team System</title><link>http://blogs.msdn.com/cellfish/archive/2008/11/16/native-c-code-coverage-reports-using-visual-studio-2008-team-system.aspx</link><pubDate>Sun, 16 Nov 2008 13:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9080305</guid><dc:creator>cellfish</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/9080305.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=9080305</wfw:commentRss><description>&lt;P&gt;The code coverage tool in Visual Studio 2008 Team System is quite easy to use from within the IDE unless you want code coverage for your native C++ code. In order to generate a code coverage report for native C++ you have to use the command line tools. This is how you do it:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;First of all your project must be compiled using the &lt;FONT face=courier&gt;/PROFILE&lt;/FONT&gt; link option. If you bring up your project properties it can be found here:&lt;BR&gt;&lt;FONT face=courier&gt;Configuration Properties -&amp;gt; Linker -&amp;gt; Advanced -&amp;gt; Profile&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;The profiler tools can then be found in the following directory:&lt;BR&gt;&lt;FONT face=courier&gt;C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Performance Tools&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;You need to add some instrumentation code to your EXE or DLL file and that is done with this command:&lt;BR&gt;&lt;FONT face=courier&gt;vsinstr.exe &amp;lt;YOUR_EXE_OR_DLL&amp;gt; /COVERAGE&lt;/FONT&gt;&lt;BR&gt;This will copy the original file to an ".orig"-file and create a new file with the original name that contains instrumentation code needed to gather coverage data.&lt;/LI&gt;
&lt;LI&gt;Now start the listener with this command:&lt;BR&gt;&lt;FONT face=courier&gt;VSPerfMon.exe /COVERAGE /OUTPUT:&amp;lt;REPORT_FILE_NAME&amp;gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;Now run your EXE or some test suite that uses the file you want to calculate coverage for.&lt;/LI&gt;
&lt;LI&gt;The listener started in step four (4) will not stop by it self once your test suite is finished so you have to stop in manually using this command (from a second command prompt):&lt;BR&gt;&lt;FONT face=courier&gt;VSPerfCmd.exe /SHUTDOWN&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;When the listener has stopped you just drag-n-drop the created ".coverage"-file into Visual Studio and you can view the results.&lt;/LI&gt;&lt;/OL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9080305" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Tools/default.aspx">Tools</category></item><item><title>Exit Mocks. Enter Stubs!</title><link>http://blogs.msdn.com/cellfish/archive/2008/10/14/exit-mocks-enter-stubs.aspx</link><pubDate>Tue, 14 Oct 2008 12:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8999222</guid><dc:creator>cellfish</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/8999222.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=8999222</wfw:commentRss><description>&lt;P&gt;Since &lt;A class="" title="I don't like Mock objects" href="http://blogs.msdn.com/cellfish/archive/2008/05/24/i-don-t-like-mock-objects.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2008/05/24/i-don-t-like-mock-objects.aspx"&gt;I'm not a fan of mocks&lt;/A&gt; I guess my prayers have been heard.&amp;nbsp;Microsoft Research &lt;A class="" title="Pex gets a Stubs framework (I said stubs, not mocks)" href="http://blog.dotnetwiki.org/PexGetsAStubsFrameworkISaidStubsNotMocks.aspx" target=_blank mce_href="http://blog.dotnetwiki.org/PexGetsAStubsFrameworkISaidStubsNotMocks.aspx"&gt;will soon release a stub framework&lt;/A&gt;. That's right. Stubs and not mocks! It is part of &lt;A class="" title="Code Coverage" href="http://blogs.msdn.com/cellfish/archive/2008/06/16/code-coverage.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2008/06/16/code-coverage.aspx"&gt;Pex that I have mentioned before&lt;/A&gt;. And you don't need to use Pex if you don't want. You can just use the stub framework by it self.&lt;/P&gt;
&lt;P&gt;The new stub framework can currently be used to stub interfaces. It creates the stubs by generating code. At first this scared me since I feared it would not regenerate the stubs when the interface changed (which might happen quite common when creating the interface using BDD). But it works just fine and detects changes in the assembly and regenerates the stubs. Very convenient.&lt;/P&gt;
&lt;P&gt;Another convenient thing is that it supplies default implementations for methods if I want. The default is to throw an &lt;FONT face=Courier&gt;StubNotImplementedException&lt;/FONT&gt; but I can change that and return default values (null, 0, empty string etc) if that is what I want. And the change can be made globally for all stubs created after the change or for a single stub object.&lt;/P&gt;
&lt;P&gt;So how does this work? Consider the following interface:&lt;/P&gt;&lt;PRE&gt;&lt;FONT color=#0000ff&gt;public interface&lt;/FONT&gt; &lt;FONT color=#2b91af&gt;IMyInterface&lt;/FONT&gt;
{
    &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; DoStuff();
}&lt;/PRE&gt;
&lt;P&gt;I just tell the stub framework to generate stubs for the assembly containing the interface and it will generate a new namespace with the stubs. If &lt;FONT face=Courier&gt;IMyInterface&lt;/FONT&gt; is part of &lt;FONT face=Courier&gt;MyNamespace&lt;/FONT&gt; the stub framework will create a new namespace &lt;FONT face=Courier&gt;MyNamespace.Stubs&lt;/FONT&gt; with a class &lt;FONT face=Courier&gt;SMyInterface&lt;/FONT&gt;. Now we want to make a test. The test below doesn't really make since since it tests the stub instead of something using the stub, but I want to show the code that makes the stub work without the surrounding stuff so bare with me...&lt;/P&gt;&lt;PRE&gt;&lt;FONT color=#0000ff&gt;void&lt;/FONT&gt; TestStub()
{
    MyNamespace.Stubs.&lt;FONT color=#2b91af&gt;SMyInterface&lt;/FONT&gt; o = &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; MyNamespace.Stubs.&lt;FONT color=#2b91af&gt;SMyInterface&lt;/FONT&gt;();
    MyNamespace.&lt;FONT color=#2b91af&gt;IMyInterface&lt;/FONT&gt; i = o;
    Assert.Equal(42, i.DoStuff());
}&lt;/PRE&gt;
&lt;P&gt;Now the test will fail since the stub will throw an exception since the stub is not implemented. Let's use the default implementation:&lt;/P&gt;&lt;PRE&gt;&lt;FONT color=#0000ff&gt;void&lt;/FONT&gt; TestStub()
{
    MyNamespace.Stubs.&lt;FONT color=#2b91af&gt;SMyInterface&lt;/FONT&gt; o = &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; MyNamespace.Stubs.&lt;FONT color=#2b91af&gt;SMyInterface&lt;/FONT&gt;();
    o.DefaultStub = Microsoft.Stubs.&lt;FONT color=#2b91af&gt;DefaultStub&lt;/FONT&gt;&amp;lt;MyNamespace.Stubs.&lt;FONT color=#2b91af&gt;SMyInterface&lt;/FONT&gt;&amp;gt;.DefaultValue;
    MyNamespace.&lt;FONT color=#2b91af&gt;IMyInterface&lt;/FONT&gt; i = o;
    Assert.Equal(42, i.DoStuff());
}&lt;/PRE&gt;
&lt;P&gt;Still a failure since the stub returns zero for &lt;FONT face=Courier&gt;DoStuff()&lt;/FONT&gt;. Let's stub that method.&lt;/P&gt;&lt;PRE&gt;&lt;FONT color=#0000ff&gt;void&lt;/FONT&gt; TestStub()
{
    MyNamespace.Stubs.&lt;FONT color=#2b91af&gt;SMyInterface&lt;/FONT&gt; o = &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; MyNamespace.Stubs.&lt;FONT color=#2b91af&gt;SMyInterface&lt;/FONT&gt;();
    o.DefaultStub = Microsoft.Stubs.&lt;FONT color=#2b91af&gt;DefaultStub&lt;/FONT&gt;&amp;lt;MyNamespace.Stubs.&lt;FONT color=#2b91af&gt;SMyInterface&lt;/FONT&gt;&amp;gt;.DefaultValue;
    o.DoStuff = (stub) =&amp;gt; { &lt;FONT color=#0000ff&gt;return&lt;/FONT&gt; 42; };
    MyNamespace.&lt;FONT color=#2b91af&gt;IMyInterface&lt;/FONT&gt; i = o;
    Assert.Equal(42, i.DoStuff());
}&lt;/PRE&gt;
&lt;P&gt;Now the test passes! I think this new framework is an excellent addition to the developer's toolbox. And I think it is a better starting framework than all the mock frameworks&amp;nbsp;out there. In the few cases that a mock framework is really needed (over a stub framework) you can easily extend your stub to verify behavior in the same way as a mock does. Currently the stub framework have a few limitations such as only being able stub interfaces. But I'll guess we'll have to wait and see what will be part of the release once it is available to the public&amp;nbsp;(&lt;EM&gt;when writing this I've been using an internal release available to Microsoft staff only&lt;/EM&gt;).&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8999222" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/.Net/default.aspx">.Net</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Tools/default.aspx">Tools</category></item><item><title>My new favorite .Net unit test framework</title><link>http://blogs.msdn.com/cellfish/archive/2008/09/24/my-new-favorite-net-unit-test-framework.aspx</link><pubDate>Wed, 24 Sep 2008 11:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8962542</guid><dc:creator>cellfish</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/8962542.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=8962542</wfw:commentRss><description>&lt;P&gt;I recently had a chance to really sink my teeth into &lt;A class="" title=xUnit.net href="http://www.codeplex.com/xunit/" target=_blank mce_href="http://www.codeplex.com/xunit/"&gt;xUnit.net&lt;/A&gt;. Since I've been stuck with &lt;A class="" title=CPPUnit href="http://sourceforge.net/projects/cppunit/" target=_blank mce_href="http://sourceforge.net/projects/cppunit/"&gt;CPPUnit&lt;/A&gt; for quite some time and working with xUnit.net was a relief. Compared to NUnit I find xUnit much easier and convenient to work with (and compared with CPPUnit it's in a class of it own).&lt;/P&gt;
&lt;P&gt;I feel I must give you four good reasons (no particular order)&amp;nbsp;for why I prefer xUnit.net:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;xcopy install makes it incredible easy to use it anywhere it is needed and in any form. Even different versions.&lt;/LI&gt;
&lt;LI&gt;No need to mark up fixtures and test suits with attributes. A test method can basically be put anywhere.&lt;/LI&gt;
&lt;LI&gt;No weird assertion names that feels like just a tick in the feature list. A small set of useful asserts.&lt;/LI&gt;
&lt;LI&gt;Testing for exceptions is an assertion and not a test method attribute.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;So &lt;A class="" title="Yet another unit test framework for .Net released" href="http://blogs.msdn.com/cellfish/archive/2008/04/24/yet-another-unit-test-framework-for-net-released.aspx" target=_blank mce_href="http://blogs.msdn.com/cellfish/archive/2008/04/24/yet-another-unit-test-framework-for-net-released.aspx"&gt;earlier I suggested&lt;/A&gt; you to take a look at xUnit. Now I'm &lt;EM&gt;telling&lt;/EM&gt; you.&amp;nbsp;You won't be disappointed!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8962542" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/BDD/default.aspx">BDD</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/.Net/default.aspx">.Net</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/TDD/default.aspx">TDD</category></item><item><title>Nothing is "Too simple to test"</title><link>http://blogs.msdn.com/cellfish/archive/2008/06/23/nothing-is-too-simple-to-test.aspx</link><pubDate>Mon, 23 Jun 2008 09:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8590370</guid><dc:creator>cellfish</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/8590370.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=8590370</wfw:commentRss><description>A common practice taught in many TDD classes is the principle of &lt;SPAN style="FONT-STYLE: italic"&gt;too easy to test&lt;/SPAN&gt;. The principle basically says that you should not test really simple things since then you will spend time writing tests that not really add value. 
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;This is a fair recommendation and actually makes a lot of sense. Especially if you're implementing TDD in a team where people are a little bit skeptical. But there are a few dangers with allowing the use of this rule.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle"&gt;&lt;SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri"&gt;Exactly what is considered too easy to test may be different if you ask different developers. Those who are reluctant to embrace TDD tend to stretch this line quite far.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle"&gt;&lt;SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri"&gt;If something that once was really simple and then didn't need a test later on is changed to something more complex there is a risk that the developer will not add a new test since there was no tests before.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Personally I think that if something is considered to be so simple it does not need a test, it must be really simple to test. And if it so simple to test why shouldn't you use it. The relative cost might be high but the absolute cost for adding a real simple test for some real simple functionality is worth it in my book since it removes the decision from the developer. "&lt;EM&gt;No new functionality unless you have failing tests&lt;/EM&gt;" is much simpler to follow and remember than "no new functionality unless you have failing tests or you consider the functionality to be really simple". Also consider the &lt;A class="" title="Code Coverage" href="http://blogs.msdn.com/cellfish/archive/2008/06/16/code-coverage.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2008/06/16/code-coverage.aspx"&gt;code coverage&lt;/A&gt; issues.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8590370" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/TDD/default.aspx">TDD</category></item><item><title>Code Coverage</title><link>http://blogs.msdn.com/cellfish/archive/2008/06/16/code-coverage.aspx</link><pubDate>Mon, 16 Jun 2008 09:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8590362</guid><dc:creator>cellfish</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/8590362.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=8590362</wfw:commentRss><description>&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Measuring code coverage is often perceived as a good measure of test quality. It is not. Good tests finds problems when changes are made to the code. But if you just want to have large code coverage you can easily make a number of tests calling all your methods but not checking the results. The only thing high code coverage values really tell you is that the code is at least not crashing with the given input.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;If you however are using BDD/TDD, code coverage values might be of interest. For example if you do not have 100% function coverage (i.e. not 100% of the functions are called) then you aren't really using BDD/TDD are you? Because how could you write a method that is not called by anyone? Well actually you might have created methods that are never called by your tests. Many TDD practitioners use a rule "to simple to test" which applies to real simple, property like methods. I don't really like that philosophy, but more on that in a later post.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;So now you think with 100% function coverage you will also have 100% line coverage with BDD/TDD, right? Well, yes and no. Typically you don't since you will add error handling with logging methods that will never occur since the errors never occur in your tests. It might be that you open a file and if that fails you log an error and exit your application. This never happens in the tests since you always manage to open the file in your tests. So how did those lines get in there if never called? Well one other rule often used by TDD practitioners is that you "should not do stupid things". If a system call may fail, you check the result regardless of tests or not. With dependency injection you'll probably get close to 100% but there is no point in bending back-wards in order to achieve high code coverage.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Does this mean there is no point in measuring code coverage? I think it is great to measure code coverage if you use the result correctly. You should not add more tests just to increase coverage since test added just to increase coverage tend to just exercise code and not really testing something interesting. But low code coverage when using BDD/TDD is definitely a warning signal that something is wrong. The team is not using the methodology correctly. So what is considered OK coverage levels? From personal experience I think anything below the levels listed in the table below should be considered bad since type without injectiontype withoutinjection with with you should have no problem at all achieving the given values.&lt;BR&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" align=center&gt;
&lt;TABLE class="" border=1&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class=""&gt;Coverage type&lt;/TH&gt;
&lt;TH class=""&gt;without dependency injection&lt;/TH&gt;
&lt;TH class=""&gt;with dependency injection&lt;/TH&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;Function&lt;/TD&gt;
&lt;TD class="" align=right&gt;90%&lt;/TD&gt;
&lt;TD class="" align=right&gt;99%&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;Line&lt;/TD&gt;
&lt;TD class="" align=right&gt;75%&lt;/TD&gt;
&lt;TD class="" align=right&gt;95%&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/P&gt;
&lt;P&gt;But sometimes there is someone (usually a manager) that thinks coverage should be above some level. So even though you know those tests will not really be useful you have to add more tests for coverage. What do you do? Either you can try to ignore the coverage fact and just try to add more tests, testing interesting things. Or you could try using &lt;A class="" title=PEX href="http://research.microsoft.com/pex/" target=_blank mce_href="http://research.microsoft.com/pex/"&gt;Pex&lt;/A&gt;. Pex is a tool from Microsoft Research that is an automated exploratory testing tool. It can be used to make a small test suite with high code coverage and with only a few simple examples I get the impression it is quite good at finding border cases in your code. This will not however replace your traditional tests/specifications written as part of your TDD/BDD process. But it can help you test some cases you did not think of and that way increase code coverage even more without any extra effort from you. At least it is better than adding coverage tests by hand.&lt;BR&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8590362" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/TDD/default.aspx">TDD</category></item><item><title>I don't like Mock objects</title><link>http://blogs.msdn.com/cellfish/archive/2008/05/24/i-don-t-like-mock-objects.aspx</link><pubDate>Sat, 24 May 2008 23:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8548940</guid><dc:creator>cellfish</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/8548940.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=8548940</wfw:commentRss><description>&lt;P&gt;When looking at &lt;A title="Behaviour Driven Development" href="http://en.wikipedia.org/wiki/Behavior_driven_development" target=_blank mce_href="http://en.wikipedia.org/wiki/Behavior_driven_development"&gt;BDD&lt;/A&gt; and &lt;A title="Test Driven Development" href="http://en.wikipedia.org/wiki/Test_driven_development" target=_blank mce_href="http://en.wikipedia.org/wiki/Test_driven_development"&gt;TDD&lt;/A&gt; examples it is very common to see the use of &lt;A title="Mocks are not stubs" href="http://blogs.msdn.com/cellfish/archive/2008/04/22/mocks-are-not-stubs.aspx" mce_href="http://blogs.msdn.com/cellfish/archive/2008/04/22/mocks-are-not-stubs.aspx"&gt;mock objects&lt;/A&gt; in the code. This however strikes me as a little bit strange since I think you should use stubs and not mocks when working with BDD/TDD. A mock is basically a stub with the added knowledge that a mock object knows how it is expected to be used. In my opinion this fact disturbs the focus when&amp;nbsp; applying BDD/TDD. Additionally I think maintainability is decreased since the tests will now have a dependency on implementation internals and you risk having to change your test not because the API has changed but how things are done internally.&lt;/P&gt;
&lt;P&gt;Let me explain what I mean with an example.&amp;nbsp;Consider a data object used to handle transactions and the ability to read and modify the amount money on a given bank account. Now assume this data object is used as a parameter to a &lt;EM&gt;transfer&lt;/EM&gt; method that is used to transfer funds between two accounts. One way to implement the transfer method could be:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Start a transaction.&lt;/LI&gt;
&lt;LI&gt;Get amount from the to-account.&lt;/LI&gt;
&lt;LI&gt;Get amount from the from-account.&lt;/LI&gt;
&lt;LI&gt;Set new account amounts for both involved accounts.&lt;/LI&gt;
&lt;LI&gt;Check that the from-account does not have a negative balance.&lt;/LI&gt;
&lt;LI&gt;End the transaction.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;From a maintainability point of view I think mocking the data object will be risky since the mock will verify that the correct number of calls are made in the correct order. It wouldn't surprise me if we some day saw a new &lt;EM&gt;transfer&lt;/EM&gt; implementation looking something like this:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Start a transaction.&lt;/LI&gt;
&lt;LI&gt;Get amount from the from-account.&lt;/LI&gt;
&lt;LI&gt;Check that the amount is enough.&lt;/LI&gt;
&lt;LI&gt;Get amount from the to-account.&lt;/LI&gt;
&lt;LI&gt;Set new account amounts for both involved accounts.&lt;/LI&gt;
&lt;LI&gt;End the transaction.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;The example may seem a little far-fetched&amp;nbsp;but still I think it points toward a maintainability risk involved when using a mock. But OK, let's assume that maintainability will not be a problem. We're still shifting focus from what is important I think. As soon as a mock is involved when I write my test/scenario I do not only have to think about the behavior I expect. I must also think about how my mock will be used since that may or may not affect my test code. Using stubs instead of mocks will reduce that focus shift since stubs are just doing what they're told without regard of context.&lt;/P&gt;
&lt;P&gt;Sure one might argue that using stubs will also involve having to think about how the stub is used. That is true but without the risk of having order or number of calls to the stub mess up your test/scenario setup. And the less time you spend on thinking about setup and the more you spend on defining behavior, the better I think. And thinking about behavior of internals and/or dependent objects should just be kept to an absolute minimum if you ask me.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8548940" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/BDD/default.aspx">BDD</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/TDD/default.aspx">TDD</category></item><item><title>Yet another unit test framework for .Net released</title><link>http://blogs.msdn.com/cellfish/archive/2008/04/24/yet-another-unit-test-framework-for-net-released.aspx</link><pubDate>Thu, 24 Apr 2008 09:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8420916</guid><dc:creator>cellfish</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/8420916.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=8420916</wfw:commentRss><description>&lt;P&gt;Version 1.0 of &lt;A class="" title="xUnit.net @ codeplex" href="http://www.codeplex.com/xunit" target=_blank mce_href="http://www.codeplex.com/xunit"&gt;xUnit.net&lt;/A&gt; has been &lt;A class="" href="http://bradwilson.typepad.com/blog/2008/04/xunitnet-10-rel.html" target=_blank mce_href="http://bradwilson.typepad.com/blog/2008/04/xunitnet-10-rel.html"&gt;released&lt;/A&gt;. So do we need another unit test framework? And I think the answer is &lt;EM&gt;yes&lt;/EM&gt;. There are a number of things that I've found a little bit weird in &lt;A class="" title=NUnit href="http://nunit.org/" target=_blank mce_href="http://nunit.org/"&gt;NUnit&lt;/A&gt; and with xUnit.net they have been removed. I definitely think that you should take a closer look at &lt;A class="" href="http://www.codeplex.com/xunit/Wiki/View.aspx?title=WhyDidWeBuildXunit&amp;amp;referringTitle=Home" target=_blank mce_href="http://www.codeplex.com/xunit/Wiki/View.aspx?title=WhyDidWeBuildXunit&amp;amp;referringTitle=Home"&gt;why xUnit.net was written&lt;/A&gt; and the &lt;A class="" href="http://www.codeplex.com/xunit/Wiki/View.aspx?title=Comparisons&amp;amp;referringTitle=Home" target=_blank mce_href="http://www.codeplex.com/xunit/Wiki/View.aspx?title=Comparisons&amp;amp;referringTitle=Home"&gt;comparison with other&lt;/A&gt; unit test frameworks. xUnit.net definitely feels like a new fresh wind in the TDD/BDD toolbox.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8420916" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/BDD/default.aspx">BDD</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/.Net/default.aspx">.Net</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/TDD/default.aspx">TDD</category></item><item><title>Mocks are not Stubs</title><link>http://blogs.msdn.com/cellfish/archive/2008/04/22/mocks-are-not-stubs.aspx</link><pubDate>Tue, 22 Apr 2008 09:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8416129</guid><dc:creator>cellfish</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/cellfish/comments/8416129.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cellfish/commentrss.aspx?PostID=8416129</wfw:commentRss><description>&lt;P&gt;As many before me I was recently looking at different mocking frameworks in order to find one that suited my needs, and was written in C++. And there are not many alternatives out there if you're using C++. I've found &lt;A class="" title=MockPP href="http://mockpp.sourceforge.net/" target=_blank mce_href="http://mockpp.sourceforge.net/"&gt;one open source&lt;/A&gt; and two internal (Microsoft staff can access what can be described as an internal &lt;A class="" title=CodePlex href="http://codeplex.com/" target=_blank mce_href="http://codeplex.com/"&gt;codeplex&lt;/A&gt;) ones. Pretty soon it turned out that none of these frameworks did what I wanted. Actually they suggested using no framework at all for the situations I was looking to "solve" with a framework.&lt;/P&gt;
&lt;P&gt;At this point I rediscovered an &lt;A class="" title="Mocks Aren't Stubs" href="http://martinfowler.com/articles/mocksArentStubs.html" target=_blank mce_href="http://martinfowler.com/articles/mocksArentStubs.html"&gt;old article by Martin Fowler&lt;/A&gt;&amp;nbsp;and it became crystal clear that the reason I didn't find what I wanted in the mocking frameworks was because I really wanted a stub framework (or rather a stub helper). Because what I wanted was a simple way to reuse the algorithm "&lt;EM&gt;return 17 the first 7 times called and then throw an exception after that, unless called with argument 4711 in which case you should always return 42&lt;/EM&gt;". A pretty simple method to write in your stub if you just subclass and override the object you want to mock/stub but a little bit cumbersome in the long run if you do this a lot.&lt;/P&gt;
&lt;P&gt;During a discussion on a company internal mailing list for TDD it became clear that people generally use the term &lt;EM&gt;mock object&lt;/EM&gt; for almost every kind of non-production object used during testing. And when another large group do make the distinction between mocks, stubs, fakes and dummies you quickly run into trouble since you are no longer discussing the same thing. So here is yet another recap on what the different kinds of objects are so I can contribute to the cause, making world a better place to be. At least when talking about mocks &amp;amp; friends.&lt;/P&gt;
&lt;P&gt;
&lt;TABLE class="" border=1&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class=""&gt;Object&lt;/TH&gt;
&lt;TH class=""&gt;Description&lt;/TH&gt;
&lt;TH class=""&gt;Comment&lt;/TH&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;Dummy&lt;/TD&gt;
&lt;TD class=""&gt;Passed around and should never be used.&lt;/TD&gt;
&lt;TD class=""&gt;Should fail the tests if ever used.&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;Fake&lt;/TD&gt;
&lt;TD class=""&gt;Working implementation but takes short-cuts making them unsuitable for production.&lt;/TD&gt;
&lt;TD class=""&gt;For example an in memory database.&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;Stub&lt;/TD&gt;
&lt;TD class=""&gt;Quickly responds with a small number of known values.&lt;/TD&gt;
&lt;TD class=""&gt;Used during testing to mimic a certain behavior.&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;Mock&lt;/TD&gt;
&lt;TD class=""&gt;I like to think of mocks as stubs with the knowledge of usage added.&lt;/TD&gt;
&lt;TD class=""&gt;Used to verify that the user of the (mock)object makes the correct number of&amp;nbsp;calls&amp;nbsp;in the correct order.&lt;BR&gt;Many mock frameworks are designed to make verification of number of calls and/or order optional. But if you do not verify order or number of calls you're basically using a stub (but it is generated from a mock framework). &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8416129" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cellfish/archive/tags/Test/default.aspx">Test</category><category domain="http://blogs.msdn.com/cellfish/archive/tags/C_2B002B00_/default.aspx">C++</category></item></channel></rss>