<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>bencoffman.com/blog - Unit Testing</title>
    <link>http://bencoffman.com/blog/</link>
    <description>News about Tech and a few other things.</description>
    <language>en-us</language>
    <copyright>Ben Coffman</copyright>
    <lastBuildDate>Sun, 01 Aug 2010 22:55:34 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.1.8102.813</generator>
    <managingEditor>coffmanben@gmail.com</managingEditor>
    <webMaster>coffmanben@gmail.com</webMaster>
    <item>
      <trackback:ping>http://bencoffman.com/blog/Trackback.aspx?guid=6805a787-e929-4516-8208-e2ea39d237bc</trackback:ping>
      <pingback:server>http://bencoffman.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://bencoffman.com/blog/PermaLink,guid,6805a787-e929-4516-8208-e2ea39d237bc.aspx</pingback:target>
      <dc:creator>Ben Coffman</dc:creator>
      <wfw:comment>http://bencoffman.com/blog/CommentView,guid,6805a787-e929-4516-8208-e2ea39d237bc.aspx</wfw:comment>
      <wfw:commentRss>http://bencoffman.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=6805a787-e929-4516-8208-e2ea39d237bc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <img src="http://bencoffman.com/blog/content/binary/microsoft_.net_logo.png" width="120px" border="0" height="75px" />
        <br />
        <br />
        <br />
In work the other day we started to debate the best way to "mock" a web service. The
two ideas we boiled down to were either to create another web service exactly the
same as the original, except have it consume the data from a text file or to have
a flag in the existing data layer, trigger the code to read data from a text file
locally, thus removing an additional dependency created by making a second web service<br /><br />
What I came up with was this. We needed to decide what we were testing?<br /><br />
Are we testing<br />
1. To make sure the code can deal with the data correctly?<br />
Or<br />
2. Are we testing to see if our code can connect to the web service?<br /><br />
Since we would like to specify our own data, I believe this makes it pretty clear.
We are testing to make sure we can deal with the data correctly.<br /><br />
The first argument stated, that by making a new web service, in which we specify the
data, you are not only testing that the web service can connect but it can consume
it correctly. I believe this is where their thinking was different than mine.<br /><br />
In most web best practices you create a “N-Tier” architecture. Meaning that one of
the tiers is dedicated to connecting to the database and returning a data-dump. The
reason this is done is because most programming architects like to believe they can
sub in one data consumption tier with another, example, getting data from Oracle as
opposed to DB2 or getting data from a web service as opposed to a MSSQL database,
without causing any issues in the logic of the code. Thus, how the data is retrieve
is of little importance, but how it’s processed is where the real brains are and have
high importance. All of this being the point of separating this data layer tier from
the business logic tier. Now understanding this aspect you also have to think in the
mind of a tester. Testers want to remove as many external dependencies on the code
as possible.  Testers don’t care about the connection a web service makes, they
only care about the data it returns. Testers want to control that data and put in
their own to test for every scenario and they want to do it with as few dependencies
as possible. Therefore introducing a new web service that sits on some other server
is not favorable. Now in order to test code you have to maintain another server, another
web service and worry about any network issues between the tester and that web service.
However most would feel best practice would be to inject the data right into the code
nearly removing the data layer. This removes numerous external dependencies and makes
the code easier to maintain as it gets passed from generation to generation of developer
that comes and goes from the company.<br /><br />
The next question is, what would be the best way to inject this data? To make the
example easy I just added some additional code in the data layer where we consume
the web service and made it look at a flag I set in the web.config. If it says true,
it hits the web service, if the flag says false it consumes a local text file, the
path of which is also specified in the web.config. This is a down and dirty way of
doing it. There are more <i>elegant</i> ways such as using “<a href="http://martinfowler.com/articles/injection.html">Constructor
Based Dependency Injection</a>,” and using <a href="http://nmock.org/download.html">nMock</a>.<br /><br />
If you would like more information on the correct way to test a web service<a href="http://blogs.msdn.com/b/davidwaddleton/archive/2006/08/03/687841.aspx"> this
blog post </a> provides some very solid and good examples.<br /><img width="0" height="0" src="http://bencoffman.com/blog/aggbug.ashx?id=6805a787-e929-4516-8208-e2ea39d237bc" /></body>
      <title>How To Test With A Web Service</title>
      <guid isPermaLink="false">http://bencoffman.com/blog/PermaLink,guid,6805a787-e929-4516-8208-e2ea39d237bc.aspx</guid>
      <link>http://bencoffman.com/blog/2010/08/01/HowToTestWithAWebService.aspx</link>
      <pubDate>Sun, 01 Aug 2010 22:55:34 GMT</pubDate>
      <description>&lt;img src="http://bencoffman.com/blog/content/binary/microsoft_.net_logo.png" width="120px" border="0" height="75px"&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
In work the other day we started to debate the best way to "mock" a web service. The
two ideas we boiled down to were either to create another web service exactly the
same as the original, except have it consume the data from a text file or to have
a flag in the existing data layer, trigger the code to read data from a text file
locally, thus removing an additional dependency created by making a second web service&lt;br&gt;
&lt;br&gt;
What I came up with was this. We needed to decide what we were testing?&lt;br&gt;
&lt;br&gt;
Are we testing&lt;br&gt;
1. To make sure the code can deal with the data correctly?&lt;br&gt;
Or&lt;br&gt;
2. Are we testing to see if our code can connect to the web service?&lt;br&gt;
&lt;br&gt;
Since we would like to specify our own data, I believe this makes it pretty clear.
We are testing to make sure we can deal with the data correctly.&lt;br&gt;
&lt;br&gt;
The first argument stated, that by making a new web service, in which we specify the
data, you are not only testing that the web service can connect but it can consume
it correctly. I believe this is where their thinking was different than mine.&lt;br&gt;
&lt;br&gt;
In most web best practices you create a “N-Tier” architecture. Meaning that one of
the tiers is dedicated to connecting to the database and returning a data-dump. The
reason this is done is because most programming architects like to believe they can
sub in one data consumption tier with another, example, getting data from Oracle as
opposed to DB2 or getting data from a web service as opposed to a MSSQL database,
without causing any issues in the logic of the code. Thus, how the data is retrieve
is of little importance, but how it’s processed is where the real brains are and have
high importance. All of this being the point of separating this data layer tier from
the business logic tier. Now understanding this aspect you also have to think in the
mind of a tester. Testers want to remove as many external dependencies on the code
as possible. &amp;nbsp;Testers don’t care about the connection a web service makes, they
only care about the data it returns. Testers want to control that data and put in
their own to test for every scenario and they want to do it with as few dependencies
as possible. Therefore introducing a new web service that sits on some other server
is not favorable. Now in order to test code you have to maintain another server, another
web service and worry about any network issues between the tester and that web service.
However most would feel best practice would be to inject the data right into the code
nearly removing the data layer. This removes numerous external dependencies and makes
the code easier to maintain as it gets passed from generation to generation of developer
that comes and goes from the company.&lt;br&gt;
&lt;br&gt;
The next question is, what would be the best way to inject this data? To make the
example easy I just added some additional code in the data layer where we consume
the web service and made it look at a flag I set in the web.config. If it says true,
it hits the web service, if the flag says false it consumes a local text file, the
path of which is also specified in the web.config. This is a down and dirty way of
doing it. There are more &lt;i&gt;elegant&lt;/i&gt; ways such as using “&lt;a href="http://martinfowler.com/articles/injection.html"&gt;Constructor
Based Dependency Injection&lt;/a&gt;,” and using &lt;a href="http://nmock.org/download.html"&gt;nMock&lt;/a&gt;.&lt;br&gt;
&lt;br&gt;
If you would like more information on the correct way to test a web service&lt;a href="http://blogs.msdn.com/b/davidwaddleton/archive/2006/08/03/687841.aspx"&gt; this
blog post &lt;/a&gt; provides some very solid and good examples.&lt;br&gt;
&lt;img width="0" height="0" src="http://bencoffman.com/blog/aggbug.ashx?id=6805a787-e929-4516-8208-e2ea39d237bc" /&gt;</description>
      <comments>http://bencoffman.com/blog/CommentView,guid,6805a787-e929-4516-8208-e2ea39d237bc.aspx</comments>
      <category>.Net</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://bencoffman.com/blog/Trackback.aspx?guid=495ff9e5-688e-4110-a9ff-3594a6a55edc</trackback:ping>
      <pingback:server>http://bencoffman.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://bencoffman.com/blog/PermaLink,guid,495ff9e5-688e-4110-a9ff-3594a6a55edc.aspx</pingback:target>
      <dc:creator>Ben Coffman</dc:creator>
      <wfw:comment>http://bencoffman.com/blog/CommentView,guid,495ff9e5-688e-4110-a9ff-3594a6a55edc.aspx</wfw:comment>
      <wfw:commentRss>http://bencoffman.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=495ff9e5-688e-4110-a9ff-3594a6a55edc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <img src="http://bencoffman.com/blog/content/binary/PragmaticUnitTesting.jpg" style="width: 320px; height: 320px; float: left;" id="PragmaticUnitTesting" />If <a href="http://www.hanselman.com/blog/">Scott
Hanselman</a> lives and die's by unit tests, why don't we all just fall in line? It
seems the nerd community, has an uncanny ability to adopt silly things quickly. Things
like <a href="http://www.xkcd.com/">xkcd.com</a> or the idea of ninja's. I have yet
to laugh at one xkcd or understand how ninja's are relative in any shape or form,
because of this I felt I needed to read up on unit tests, specifically for C#, to
find out if it's just hype or this is something development shops with a strong QA
team really need to look at. 
<div><br /></div><div>I read two books that covered the topic of unit testing. The first book was <i><a href="http://www.openmymind.net/FoundationsOfProgramming.pdf">Foundations
of Programming</a></i> (recommended by Scottie H. himself and is free) and the second
is <i><a href="http://www.amazon.com/Pragmatic-Unit-Testing-NUnit-Programmers/dp/0974514020">Pragmatic
Unit Testing In C# with NUnit</a></i>. Both of them start off with the same old song
and dance on how you might have up front costs of introducing unit tests to your code,
but the stability these tests provide over the duration of your codes lifetime will
cause such dramatic cost savings for the company, it would be foolish not to explore
the idea. What a compelling sales pitch, a pitch salesmen, for just about any technology,
have used since the beginning of technology in businesses. I'll cut all the used car
salesman tactics out and state the most intriguing argument to unit test. It increases
code stability and it's easy.<br /></div><br /><div>The next question I asked myself: What do I exactly test in the code-base I am
working on? In <i>Pragmatic Unit Testing</i> (PUT) they give us an acronym to use
in order to answer this question. BICEP. 
<br /><br /></div><div><br />
BICEP, breaks down as such.
</div><div><br /></div><div><b>B</b>oundary Tests
</div><div><b>I</b>nversion Tests
</div><div><b>C</b>ross Check Tests
</div><div><b>E</b>rror Tests
</div><div><b>P</b>erformance Tests
</div><div><br /></div><div>These are the 5 major aspects one should test, according to PUT and it provides
unit testers with a place to start. The next major topic in discussing how to test
code is decoupling one piece of code to another. Does your code talk to a middleware?
How do you test if middleware is not done creating your service to consume or if middleware
is down? In steps <a href="http://www.nunit.org/index.php">NUnit Mocks</a>, <a href="http://www.nunit.org/index.php">NMock2</a>,
and <a href="http://www.nunit.org/index.php">DotNetMock</a>. These 3 mock frameworks
provide the developer with the ability to feed your tests predefined values, values
decided by you. Without going into how to use these mock frameworks, I believe that
outside of a few isolated situations they should not be used. The entire purpose of
testing your code is to test that you are getting information back that fits the criteria
you are looking for. If this information, that is beyond your control, changes on
whatever level for whatever reason, you as a developer need to know. Putting in mock
objects hides this.
</div><div><br /></div><div>Since I work for a web shop, the topic of web UI unit tests interested me. PUT
recommend using <a href="http://seleniumhq.org/">Selenium</a>. This makes sense because
it still uses the nunit style of testing keeping all your tests to one testing style.
Selenium seems a bit cumbersome in comparison to <a href="http://watin.sourceforge.net/">WaitN</a> or <a href="https://addons.mozilla.org/en-US/firefox/addon/3863">iMacro</a>,
but I feel keeping all your testing in the same style outweighs the cons of not using
Selenium. The more desperate testing sources you introduce, the more confusing it
is for an outsider to step in and see the whole picture when learning the code, especially
when it's not contained in a single <i>solution</i>.
</div><div><br /></div><div>Finally, I recommend reading both of the books mentioned, but lets be honest
most of us care so little about unit testing we'll be lucky to read all of 1 of these
books let alone all of both. If this is the case I recommend PUT, while the first
few chapters read like the high school teacher striving to gain his students social
acceptance, it's an easy read and you can jump into the book at nearly any chapter
and get the exact information you are looking for on unit testing. This is something
I wish all programming books could achieve. 
<br /></div><img width="0" height="0" src="http://bencoffman.com/blog/aggbug.ashx?id=495ff9e5-688e-4110-a9ff-3594a6a55edc" /></body>
      <title>NUnit, NUnit Mocks, NMock2, DotNetMock, and Selenium : Pragmatic Unit Testing In C# with NUnit</title>
      <guid isPermaLink="false">http://bencoffman.com/blog/PermaLink,guid,495ff9e5-688e-4110-a9ff-3594a6a55edc.aspx</guid>
      <link>http://bencoffman.com/blog/2009/11/11/NUnitNUnitMocksNMock2DotNetMockAndSeleniumPragmaticUnitTestingInCWithNUnit.aspx</link>
      <pubDate>Wed, 11 Nov 2009 21:12:32 GMT</pubDate>
      <description>&lt;img src="http://bencoffman.com/blog/content/binary/PragmaticUnitTesting.jpg" style="width: 320px; height: 320px; float: left;" id="PragmaticUnitTesting"&gt;If &lt;a href="http://www.hanselman.com/blog/"&gt;Scott
Hanselman&lt;/a&gt; lives and die's by unit tests, why don't we all just fall in line? It
seems the nerd community, has an uncanny ability to adopt silly things quickly. Things
like &lt;a href="http://www.xkcd.com/"&gt;xkcd.com&lt;/a&gt; or the idea of ninja's. I have yet
to laugh at one xkcd or understand how ninja's are relative in any shape or form,
because of this I felt I needed to read up on unit tests, specifically for C#, to
find out if it's just hype or this is something development shops with a strong QA
team really need to look at. 
&lt;div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;div&gt;I read two books that covered the topic of unit testing. The first book was &lt;i&gt;&lt;a href="http://www.openmymind.net/FoundationsOfProgramming.pdf"&gt;Foundations
of Programming&lt;/a&gt;&lt;/i&gt; (recommended by Scottie H. himself and is free) and the second
is &lt;i&gt;&lt;a href="http://www.amazon.com/Pragmatic-Unit-Testing-NUnit-Programmers/dp/0974514020"&gt;Pragmatic
Unit Testing In C# with NUnit&lt;/a&gt;&lt;/i&gt;. Both of them start off with the same old song
and dance on how you might have up front costs of introducing unit tests to your code,
but the stability these tests provide over the duration of your codes lifetime will
cause such dramatic cost savings for the company, it would be foolish not to explore
the idea. What a compelling sales pitch, a pitch salesmen, for just about any technology,
have used since the beginning of technology in businesses. I'll cut all the used car
salesman tactics out and state the most intriguing argument to unit test. It increases
code stability and it's easy.&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;div&gt;The next question I asked myself: What do I exactly test in the code-base I am
working on? In &lt;i&gt;Pragmatic Unit Testing&lt;/i&gt; (PUT) they give us an acronym to use
in order to answer this question. BICEP. 
&lt;br&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br&gt;
BICEP, breaks down as such.
&lt;/div&gt;
&lt;div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;div&gt;&lt;b&gt;B&lt;/b&gt;oundary Tests
&lt;/div&gt;
&lt;div&gt;&lt;b&gt;I&lt;/b&gt;nversion Tests
&lt;/div&gt;
&lt;div&gt;&lt;b&gt;C&lt;/b&gt;ross Check Tests
&lt;/div&gt;
&lt;div&gt;&lt;b&gt;E&lt;/b&gt;rror Tests
&lt;/div&gt;
&lt;div&gt;&lt;b&gt;P&lt;/b&gt;erformance Tests
&lt;/div&gt;
&lt;div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;div&gt;These are the 5 major aspects one should test, according to PUT and it provides
unit testers with a place to start. The next major topic in discussing how to test
code is decoupling one piece of code to another. Does your code talk to a middleware?
How do you test if middleware is not done creating your service to consume or if middleware
is down? In steps &lt;a href="http://www.nunit.org/index.php"&gt;NUnit Mocks&lt;/a&gt;, &lt;a href="http://www.nunit.org/index.php"&gt;NMock2&lt;/a&gt;,
and &lt;a href="http://www.nunit.org/index.php"&gt;DotNetMock&lt;/a&gt;. These 3 mock frameworks
provide the developer with the ability to feed your tests predefined values, values
decided by you. Without going into how to use these mock frameworks, I believe that
outside of a few isolated situations they should not be used. The entire purpose of
testing your code is to test that you are getting information back that fits the criteria
you are looking for. If this information, that is beyond your control, changes on
whatever level for whatever reason, you as a developer need to know. Putting in mock
objects hides this.
&lt;/div&gt;
&lt;div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;div&gt;Since I work for a web shop, the topic of web UI unit tests interested me. PUT
recommend using &lt;a href="http://seleniumhq.org/"&gt;Selenium&lt;/a&gt;. This makes sense because
it still uses the nunit style of testing keeping all your tests to one testing style.
Selenium seems a bit cumbersome in comparison to &lt;a href="http://watin.sourceforge.net/"&gt;WaitN&lt;/a&gt; or &lt;a href="https://addons.mozilla.org/en-US/firefox/addon/3863"&gt;iMacro&lt;/a&gt;,
but I feel keeping all your testing in the same style outweighs the cons of not using
Selenium. The more desperate testing sources you introduce, the more confusing it
is for an outsider to step in and see the whole picture when learning the code, especially
when it's not contained in a single &lt;i&gt;solution&lt;/i&gt;.
&lt;/div&gt;
&lt;div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;div&gt;Finally, I recommend reading both of the books mentioned, but lets be honest
most of us care so little about unit testing we'll be lucky to read all of 1 of these
books let alone all of both. If this is the case I recommend PUT, while the first
few chapters read like the high school teacher striving to gain his students social
acceptance, it's an easy read and you can jump into the book at nearly any chapter
and get the exact information you are looking for on unit testing. This is something
I wish all programming books could achieve. 
&lt;br&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://bencoffman.com/blog/aggbug.ashx?id=495ff9e5-688e-4110-a9ff-3594a6a55edc" /&gt;</description>
      <comments>http://bencoffman.com/blog/CommentView,guid,495ff9e5-688e-4110-a9ff-3594a6a55edc.aspx</comments>
      <category>.Net</category>
      <category>readings</category>
      <category>Unit Testing</category>
    </item>
  </channel>
</rss>