<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rhyous</title>
	<atom:link href="http://www.rhyous.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rhyous.com</link>
	<description>Knight of the Code</description>
	<lastBuildDate>Wed, 16 May 2012 06:00:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Building a FreeBSD kernel for debugging</title>
		<link>http://www.rhyous.com/2012/05/16/building-a-freebsd-kernel-for-debugging/</link>
		<comments>http://www.rhyous.com/2012/05/16/building-a-freebsd-kernel-for-debugging/#comments</comments>
		<pubDate>Wed, 16 May 2012 06:00:01 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[FreeBSD]]></category>

		<guid isPermaLink="false">http://www.rhyous.com/?p=5014</guid>
		<description><![CDATA[You may want to actually debug the kernel or a kernel module, however, debugging is not enabled by default. Prereqs Before getting started it is assumed you have a FreeBSD Install and you have downloaded/installed FreeBSD Source. The steps are identical to the steps contained in the How to build and install a custom kernel on [...]]]></description>
			<content:encoded><![CDATA[<p>You may want to actually debug the kernel or a kernel module, however, debugging is not enabled by default.</p>
<h2>Prereqs</h2>
<p>Before getting started it is assumed you have a <a href="/2009/11/04/how-do-i-install-freebsd/">FreeBSD Install</a> and you have downloaded/installed <a href="/2009/12/25/how-to-download-freebsd-source-using-svn/">FreeBSD Source</a>.</p>
<p>The steps are identical to the steps contained in the <a title="Permanent Link to How to build and install a custom kernel on FreeBSD?" href="http://www.rhyous.com/2012/05/09/how-to-build-and-install-a-custom-kernel-on-freebsd/" rel="bookmark">How to build and install a custom kernel on FreeBSD?</a> article, with the exception of the kernel configuration file.</p>
<h2>Step 1 &#8211; Create a new kernel config</h2>
<ol>
<li>Determine your architecture by running this command:
<pre class="brush: bash; title: ; notranslate">
# uname -a
FreeBSD 9.0-RELEASE FreeBSD 9.0-RELEASE #0: Tue Jan 3 07:46:30 UTC 2012 root@farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC amd64
</pre>
</li>
<li>Look at the last text item in the output string. I have amd64 so that is my architecture: amd64</li>
<li>Change to the directory of the kernel configuration files for your architecture. Remember if you are on a different architecture to replace amd64 in the following command lien with your architecture.
<pre class="brush: bash; title: ; notranslate">
# cd /usr/src/sys/amd64/conf
</pre>
</li>
<li>Copy GENERIC to a new file.
<pre class="brush: bash; title: ; notranslate">
# cp GENERIC KRNLDBG
</pre>
</li>
<li>Edit KRNLDBG with your favorite text editor.</li>
<li>First, change the ident value near the top from GENERIC to KRNLDBG.
<pre class="brush: plain; title: ; notranslate">
ident           KRNLDBG
</pre>
</li>
<li>Add debugging settings to your KRNLDBG configuraiton file.
<pre class="brush: plain; title: ; notranslate">
options           KDB
options           KDB_TRACE
options           DDB
options           GDB
</pre>
</li>
<li>Save and close your new KRNLDBG configuration file.</li>
</ol>
<p>For the remaining steps, follow the <a title="Permanent Link to How to build and install a custom kernel on FreeBSD?" href="http://www.rhyous.com/2012/05/09/how-to-build-and-install-a-custom-kernel-on-freebsd/" rel="bookmark">How to build and install a custom kernel on FreeBSD?</a> article, only replace any references to KRNL1 kernel config file with the KRNLDBG config file created in the previous step.</p>
<p>Once you are done building and installing the kernel, you should have debugging enabled.</p>
<p>I happen to be working with a kernel module that is crashing and when it crashes, it automatically places me in a debugging session with the following prompt.</p>
<pre class="brush: bash; title: ; notranslate">
db&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rhyous.com/2012/05/16/building-a-freebsd-kernel-for-debugging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing VMWare Tools on FreeBSD 9</title>
		<link>http://www.rhyous.com/2012/05/09/installing-vmware-tools-on-freebsd-9-without-xorg/</link>
		<comments>http://www.rhyous.com/2012/05/09/installing-vmware-tools-on-freebsd-9-without-xorg/#comments</comments>
		<pubDate>Wed, 09 May 2012 16:23:21 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[FreeBSD]]></category>
		<category><![CDATA[VMWare]]></category>

		<guid isPermaLink="false">http://www.rhyous.com/?p=4988</guid>
		<description><![CDATA[Virtualizing a FreeBSD server is common place. Knowing how to install VMWare Tools on a FreeBSD server without X11 is going to be extremely important. This article will provide the steps. Lets get started. Step 1 &#8211; Install FreeBSD as VMWare Guest. Instructions for installing FreeBSD 9 are found here: How do I install FreeBSD [...]]]></description>
			<content:encoded><![CDATA[<p>Virtualizing a FreeBSD server is common place. Knowing how to install VMWare Tools on a FreeBSD server without X11 is going to be extremely important. This article will provide the steps.</p>
<p>Lets get started.</p>
<h2>Step 1 &#8211; Install FreeBSD as VMWare Guest.</h2>
<p>Instructions for installing FreeBSD 9 are found here: <a href="http://www.rhyous.com/2012/01/13/how-to-install-freebsd-9/">How do I install FreeBSD 9?</a></p>
<p>It shouldn&#8217;t be much of an effort to follow these steps inside a VMWare guest.</p>
<p>Note: You may consider taking a snapshot here to save your current state.</p>
<h2>Step 2 &#8211; Update FreeBSD and Install ports</h2>
<p>Instructions for updating FreeBSD and installing ports are found here:<br />
<a title="Permanent Link to What are the first commands I run after installing FreeBSD? Or How to patch FreeBSD? Or How to install ports on FreeBSD?" href="http://www.rhyous.com/2009/11/03/what-are-the-first-commands-i-run-after-installing-freebsd/" rel="bookmark">Update FreeBSD and Install ports</a></p>
<p>Note: You may consider taking a snapshot here to save your current state.</p>
<h2>Step 3 &#8211; Install Prerequisites</h2>
<h3>Step 3.1 &#8211; Install Perl</h3>
<p>Installing Perl is easy. Use either of the following commands.</p>
<p>From ports</p>
<pre class="brush: bash; title: ; notranslate">
# cd /usr/ports/lang/perl5.12
# make install
</pre>
<p>From packages</p>
<pre class="brush: bash; title: ; notranslate">
# pgk_add -r perl
</pre>
<h3>Step 3.2 &#8211; Install compat6x-amd64</h3>
<p>The compat6x-amd64 port is also easily installed.</p>
<p>From ports</p>
<pre class="brush: bash; title: ; notranslate">
# cd /usr/ports/misc/compat6x/
# make install
</pre>
<p>From packages</p>
<pre class="brush: bash; title: ; notranslate">
# pkg_add -r compat6x-amd64
</pre>
<h2>Step 4 &#8211; Take a VMWare Snapshot</h2>
<p><strong>Important!</strong> Take a snapshot here! Do not skip this step.</p>
<h2>Step 5 &#8211; Mount the VMWare Tools ISO</h2>
<p>I am using VMWare workstation. Some steps may be slightly different if you are using ESXi or other VMWare solution.</p>
<ol>
<li>In VMWare Workstation, choose VM | Install VMWare Tools.</li>
<li>In FreeBSD as root, create a directory to mount the CD-Rom to.
<pre class="brush: bash; title: ; notranslate">
# mkdir /cdrom
</pre>
</li>
<li>Mount the cd-rom.
<pre class="brush: bash; title: ; notranslate">
# mount -t cd9660 /dev/cd0 /cdrom
</pre>
</li>
</ol>
<p>Note: You may consider taking a snapshot here to save your current state.</p>
<h2>Step 6 &#8211; Extract the vmware-freebsd-tools.tar.gz</h2>
<p>Now that the drive is mounted, it should be easy to get to the vwmare-tools file.</p>
<ol>
<li> Copy the vmware-freebsd-tools.tar.gz file to a local location.
<pre class="brush: bash; title: ; notranslate">
# cp /cdrom/vmware-freebsd-tools.tar.gz /root
</pre>
</li>
<li>Extract the vmware-freebsd-tools.tar.gz file.
<pre class="brush: bash; title: ; notranslate">
# cd /root
# tar -xzf vmware-freebsd-tools.tar.gz
</pre>
</li>
</ol>
<p>VMWare tools should now be extracted.</p>
<h2>Step 7 &#8211; Recompile VMWare Tools Modules</h2>
<p>Before you install VMWare tools on FreeBSD 9, you need the modules to work with FreeBSD 9. VMWare is slow to update the vmware tools for the FreeBSD guest. So you are just going to have to update them yourself.</p>
<p>Note: We are now at the point where we are going to do more and install more than you want to for your nice new clean server, but that is ok, because we have a snapshot and once we get the files compiled you can revert to the clean snapshot. Alternately if you have another FreeBSD system, you can do these steps on that system.</p>
<p>If you install without recompiling as of May 10, 2012, you will get this result.</p>
<pre class="brush: plain; title: ; notranslate">
Starting VMware Tools services in the virtual machine:
   Switching to guest configuration:                                   done
   Guest memory manager:                                              failed
   Blocking file system:                                              failed
   Guest operating system daemon:                                      done
Unable to start services for VMware Tools

Execution aborted.
</pre>
<p>Compiling the modules first should prevent the above failures.</p>
<p>Note 1: You may want to compile a custom kernel while you are at this. If so, check out this article: <a title="Permanent Link to How to build and install a custom kernel on FreeBSD?" href="http://www.rhyous.com/2012/05/09/how-to-build-and-install-a-custom-kernel-on-freebsd/" rel="bookmark">How to build and install a custom kernel on FreeBSD?</a></p>
<h3>Step 7.1 &#8211; Get FreeBSD Source</h3>
<p>Download the FreeBSD Source as described here.</p>
<p><a title="Permanent Link to How to download FreeBSD source using svn?" href="http://www.rhyous.com/2009/12/25/how-to-download-freebsd-source-using-svn/" rel="bookmark">How to download FreeBSD source using svn?</a></p>
<h3>Step 7.2 &#8211; Configure the /etc/make.conf</h3>
<p>Right now there is a move toward compiling with clang over gcc. Because of changes due to this, you need to add the following line to your /etc/make.conf to compile with gcc. I have not tried to compile with clang yet.</p>
<ol>
<li>As root open the /etc/make.conf file with you favorite editor and add the following line:
<pre class="brush: plain; title: ; notranslate">
MK_CLANG_IS_CC=no
</pre>
</li>
<li>Save and close the /etc/make.conf file.</li>
</ol>
<p>Your /etc/make.conf is now configured.</p>
<h3>Step 7.3 &#8211; Compile the vmmemctl module</h3>
<p>Recompile vmmemctl using these steps.</p>
<ol>
<li>Go to the lib/modules/source directory under where you extracted vmware-freebsd-tools.tar.gz.
<pre class="brush: bash; title: ; notranslate">
# cd /root/vmware-tools-distrib/lib/modules/source/
</pre>
</li>
<li>Extract vmmemctl.tar
<pre class="brush: bash; title: ; notranslate">
# tar -xf vmmemctl.tar
</pre>
</li>
<li>Change to the vmmemctl-only directory.
<pre class="brush: bash; title: ; notranslate">
# cd vmmemctl-only
</pre>
</li>
<li>Run make.
<pre class="brush: bash; title: ; notranslate">
# make
</pre>
</li>
<li>Run make install.
<pre class="brush: bash; title: ; notranslate">
# make install
</pre>
</li>
</ol>
<p>You have now built and installed the vmmemctl module.</p>
<h3>Step 7.4 &#8211; Compile the vmblock module</h3>
<ol>
<li>Go to the lib/modules/source directory under where you extracted vmware-freebsd-tools.tar.gz.
<pre class="brush: bash; title: ; notranslate">
# cd /root/vmware-tools-distrib/lib/modules/source/
</pre>
</li>
<li>Extract vmblock.tar
<pre class="brush: bash; title: ; notranslate">
# tar -xf vmblock.tar
</pre>
</li>
<li>Change to the vmblock-only directory.
<pre class="brush: bash; title: ; notranslate">
# cd vmblock-only
</pre>
</li>
<li>Run make.
<pre class="brush: bash; title: ; notranslate">
# make
</pre>
</li>
<li>Run make install.
<pre class="brush: bash; title: ; notranslate">
# make install
</pre>
</li>
</ol>
<p>You have now built and installed the vmblock module.</p>
<h2>Step 8 &#8211; Install VMWare Tools</h2>
<p>You are now ready to install the VMWare Tools.</p>
<p>Note: If you are trying to keep you server clean and pristine, then copy the /root/vmware-tools-distrib directory off the server somewhere. THen revert to the snapshot you took just before Step 4. Copy the directory back to your clean snapshot and continue.</p>
<ol>
<li>Move into the directory created by the extraction.
<pre class="brush: bash; title: ; notranslate">
# cd /root/vmware-tools-distrib/
</pre>
</li>
<li>Run vmware-install.pl
<pre class="brush: bash; title: ; notranslate">
# ./vmware-install.pl
</pre>
</li>
<li>Select all the defaults.</li>
</ol>
<p>Your vmware tools installation should go smoothly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rhyous.com/2012/05/09/installing-vmware-tools-on-freebsd-9-without-xorg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to build and install a custom kernel on FreeBSD?</title>
		<link>http://www.rhyous.com/2012/05/09/how-to-build-and-install-a-custom-kernel-on-freebsd/</link>
		<comments>http://www.rhyous.com/2012/05/09/how-to-build-and-install-a-custom-kernel-on-freebsd/#comments</comments>
		<pubDate>Wed, 09 May 2012 14:08:50 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[FreeBSD]]></category>

		<guid isPermaLink="false">http://www.rhyous.com/?p=4989</guid>
		<description><![CDATA[Lets get started. The main reason I am writing this when so many articles on this already exist is because many articles do not have all the steps in one place. For example, the article in the FreeBSD Handbook doesn&#8217;t have steps for downloading the source using subversion or for making sure you have enough [...]]]></description>
			<content:encoded><![CDATA[<p>Lets get started. The main reason I am writing this when so many articles on this already exist is because many articles do not have all the steps in one place. For example, the article in the FreeBSD Handbook doesn&#8217;t have steps for downloading the source using subversion or for making sure you have enough space on the root partition. See this article here: <a href="http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/kernelconfig-building.html" rel="nofollow">Building and Installing a Custom Kernell</a></p>
<h2>Step 1 &#8211; Install FreeBSD as VMWare Guest.</h2>
<p>Instructions for installing FreeBSD 9 are found here:</p>
<p><a href="http://www.rhyous.com/2012/01/13/how-to-install-freebsd-9/">How do I install FreeBSD 9?</a></p>
<p>You may also want to install FreeBSD ports:</p>
<p><a title="Permanent Link to What are the first commands I run after installing FreeBSD? Or How to patch FreeBSD? Or How to install ports on FreeBSD?" href="http://www.rhyous.com/2009/11/03/what-are-the-first-commands-i-run-after-installing-freebsd/" rel="bookmark">How to install FreeBSD ports</a></p>
<h2>Step 2 &#8211; Download FreeBSD Source</h2>
<p>Instructions for downloading FreeBSD Source can be found here:</p>
<p><a title="Permanent Link to How to download FreeBSD source using svn?" href="http://www.rhyous.com/2009/12/25/how-to-download-freebsd-source-using-svn/" rel="bookmark">How to download FreeBSD source using svn?</a></p>
<h2>Step 3 &#8211; Build the GENERIC Kernel</h2>
<p>Before you create a custom kernel it is always good to know that the default GENERIC kernel is compiling and working. Also, if you are practiced at this and are certain this will work, feel free to skip this step.</p>
<p>Note: I call it the GENERIC kernel because the GENERIC is the file name of the default kernel configuration.</p>
<ol>
<li>Go to the /usr/src directory:
<pre class="brush: bash; title: ; notranslate">
# cd /usr/src
</pre>
</li>
<li>As root, run this command:
<pre class="brush: bash; title: ; notranslate">
# make buildkernel
</pre>
</li>
<li> Wait for the compile to complete</li>
</ol>
<h2>Step 4 &#8211; Create a new kernel config</h2>
<ol>
<li>Determine your architecture by running this command:
<pre class="brush: bash; title: ; notranslate">
# uname -a
FreeBSD 9.0-RELEASE FreeBSD 9.0-RELEASE #0: Tue Jan 3 07:46:30 UTC 2012 root@farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC amd64
</pre>
</li>
<li>Look at the last text item in the output string. I have amd64 so that is my architecture: amd64</li>
<li>Change to the directory of the kernel configuration files for your architecture. Remember if you are on a different architecture to replace amd64 in the following command lien with your architecture.
<pre class="brush: bash; title: ; notranslate">
# cd /usr/src/sys/amd64/conf
</pre>
</li>
<li>Copy GENERIC to a new file.
<pre class="brush: bash; title: ; notranslate">
# cp GENERIC KRNL1
</pre>
</li>
<li>Edit KRNL1 with your favorite text editor.</li>
<li>First, change the ident value near the top from GENERIC to KRNL1.
<pre class="brush: plain; title: ; notranslate">
ident           KRNL1
</pre>
</li>
<li>Make any other changes you would like to make.It is hard to know why you are building a custom kernel and hopefully you know what you need in your custom kernel. This is where you modify the kernel to provide what you need.</li>
<li>Save and close your new KRNL1 configuration file.</li>
</ol>
<h2>Step 5 &#8211; Build the custom kernel</h2>
<p>Now that you have a new configuration file, build a kernel using that configuration file.</p>
<ol>
<li>Compile the kernel.
<pre class="brush: bash; title: ; notranslate">
# cd /usr/src
# make buildkernel KERNCONF=KRNL1
</pre>
</li>
<li>Wait for you kernel to build.</li>
</ol>
<h2>Step 6 &#8211; Verify you have enough space for the new kernel</h2>
<ol>
<li>Make sure you have enough free space to install your kernel.Note: Your output may be quite different than mine.
<pre class="brush: bash; title: ; notranslate"># df -ah
Filesystem    Size    Used   Avail Capacity  Mounted on
/dev/da0p2     74G    4.3G     64G     6%    /
devfs         1.0k    1.0k      0B   100%    /dev
</pre>
</li>
<li>If your root partion, /, has a capacity greater than 55%, you probably OK. Otherwise, your backup or kernel installation may fail.</li>
</ol>
<h2>Step 7 &#8211; Install the custom kernel</h2>
<ol>
<li>Install the Kernel.
<pre class="brush: bash; title: ; notranslate">
# make installkernel KERNCONF=KRNL1
</pre>
</li>
<li>Reboot the system.
<pre class="brush: bash; title: ; notranslate">
# reboot
</pre>
</li>
</ol>
<p>You now have a custom kernel installed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rhyous.com/2012/05/09/how-to-build-and-install-a-custom-kernel-on-freebsd/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unit Testing with Parameter Value Coverage (PVC)</title>
		<link>http://www.rhyous.com/2012/05/08/unit-testing-with-parameter-value-coverage-pvc/</link>
		<comments>http://www.rhyous.com/2012/05/08/unit-testing-with-parameter-value-coverage-pvc/#comments</comments>
		<pubDate>Tue, 08 May 2012 06:12:28 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[Unit Tests]]></category>

		<guid isPermaLink="false">http://www.rhyous.com/?p=4962</guid>
		<description><![CDATA[Parameter Value Coverage (PVC) is the ability to track coverage of a method based on the common possible values for the parameters accepted by the method. Current code coverage tools fail to take into consideration the possibility that a value for a parameter is not handled resulting in a bug. However, there may not be [...]]]></description>
			<content:encoded><![CDATA[<p>Parameter Value Coverage (PVC) is the ability to track coverage of a method based on the common possible values for the parameters accepted by the method.</p>
<p>Current code coverage tools fail to take into consideration the possibility that a value for a parameter is not handled resulting in a bug. However, there may not be any code addressing this value in any way, introducing the possibility of obtaining 100% code coverage or line coverage (LC) without detecting the bug.</p>
<p>If the common values for types, especially primitive types and known types, are documented and methods that use them are required to have a test for each possible parameter value, bugs can be avoided.</p>
<p>The following is a table of parameter value requirements for an int or System.Int32 and string or System.String. A unit test should exist for each of the possible values in order to have 100% PVC.</p>
<table>
<tbody>
<tr>
<td>System.Int32</td>
<td>System.String</td>
</tr>
<tr>
<td valign="top">
<ol>
<li>A positive integer</li>
<li>A negative integer</li>
<li>Zero</li>
<li>int.MaxValue or 2,147,483,647</li>
<li>int.MinValue or -2,147,483,648</li>
</ol>
</td>
<td>
<ol>
<li>A null string</li>
<li>An empty string, String.Empty, or &#8220;&#8221;</li>
<li>One or more spaces &#8221; &#8220;</li>
<li>One or more tabs &#8221; &#8220;</li>
<li>A new line or Environment.NewLine</li>
<li>A valid string.</li>
<li>An invalid or junk string</li>
<li>Unicode characters such as Chinese</li>
</ol>
</td>
</tr>
</tbody>
</table>
<p>Were code coverage tools enhanced to take into account Parameter Value Coverage (PVC), better tests would be written and more bugs would be found.</p>
<p>Such PVC lists could be created for each primitive type and a list of required parameter values to test against for each of primitive type could be standard for each language. You could also create a list of parameter values for you own types though you may find that your own type is actually a collection of primitive types and methods, and if they are tested with PVC, your class is automatically tested with PVC as well.</p>
<h2>Finding bugs beyond 100% Code Coverage with PVC</h2>
<p>100% code coverage or line coverage (LC) can fail to find many bugs. Developers often write a unit test with only the goal to get as close to 100% LC as they can. Unfortunately, they are only covering the written code, but bugs may exist due to code not written.</p>
<p>Here is a quick example. The method below is a very common example of a piece of code that looks so simple, most assume there is no way there is a bug, but after a brief analysis, the bug becomes obvious.</p>
<pre class="brush: csharp; title: ; notranslate">

To get 100% LC, the following test could be used.

1
private void Add_Test()
{
    Adder adder = new Adder();
    var actual = adder.Add(1,2);
    var expected = 3;
    Assert.AreEqual(expected, actual)
}
</pre>
<p>You now have 100% LC, but have you found all the bugs? No you haven&#8217;t. Here are two problems:</p>
<ol>
<li>An int or System.Int32 is a 32-bit integer with a max value of 2,147,483,647.What happens if you add one or more to the max value?</li>
<li>The minimum value is -2,147,483,648. What happens if you subtract one or more from the minimum value?</li>
</ol>
<p>How should this problem be fixed?</p>
<ul>
<li>Should you enabled &#8220;checked&#8221; to disallow overflows?</li>
<li>Should you return a long so two 32-bit integers can always be added together?</li>
<li>Or should you ignore the issue as it isn&#8217;t going to matter in your project?</li>
</ul>
<p>Your answer may be different depending on your project. I am not going to tell you how to fix this bug in this article, that is not the point. The point is to show that 100% LC failed to find this bug, proving that some thing is missing, some value is not being tracked and reported on.</p>
<p>What is the missing value? Parameter Value Coverage (PVC).</p>
<p>If PVC were taken into account, the test above only provides 20% coverage as only one of the five integer value types were tested. Reaching 80% to 100% PVC would have found this bug.</p>
<p>Every bug that is found internally will cost your business far less money overall than if a customer finds the bug.</p>
<p>Return to <a href="http://www.rhyous.com/programming-development/csharp-unit-test-tutorial/">C# Unit Test Tutorial</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rhyous.com/2012/05/08/unit-testing-with-parameter-value-coverage-pvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Better Unit Tests in C#</title>
		<link>http://www.rhyous.com/2012/05/07/better-unit-tests-in-csharp/</link>
		<comments>http://www.rhyous.com/2012/05/07/better-unit-tests-in-csharp/#comments</comments>
		<pubDate>Mon, 07 May 2012 12:00:32 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[Mock]]></category>
		<category><![CDATA[Unit Tests]]></category>

		<guid isPermaLink="false">http://www.rhyous.com/?p=4933</guid>
		<description><![CDATA[When you test the right thing, you get better unit tests. Better unit tests often lead to better design, testable design, and easier maintainability of code. Look at an example of testing a Copy() method in a Person object. You will see that as you unit test this method, you are forced to think. If [...]]]></description>
			<content:encoded><![CDATA[<p>When you test the right thing, you get better unit tests. Better unit tests often lead to better design, testable design, and easier maintainability of code.</p>
<p>Look at an example of testing a Copy() method in a Person object. You will see that as you unit test this method, you are forced to think. If you think about what you really want to test (instead of just thinking about 100% coverage), and test for that, it will lead you to changing your code for the better.</p>
<p>With a Copy() method, you want to make sure that every property in the Person object is copied. You want to make sure that any new Property added in the future is copied.</p>
<p>Lets see if we can do this. Lets start with our simple example Person object with a Copy() method and lets test the copy method.</p>
<pre class="brush: csharp; title: ; notranslate">
using System;

namespace FriendDatabase
{
    public class Person
    {
        #region Properties
        public int Age { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        #endregion

        #region Methods
        public Person Copy()
        {
            Person retPerson = new Person();
            retPerson.Age = this.Age;
            retPerson.FirstName = this.FirstName;
            retPerson.LastName = this.LastName;
            return retPerson;
        }
        #endregion
    }
}
</pre>
<p>Lets list the three most obvious tests for the copy method.</p>
<ol>
<li>Age is the same value.</li>
<li>FirstName is the same value.</li>
<li>LastName is the same value.</li>
</ol>
<p>If you are an expert at Unit Testing, you are probably already thinking that there are more tests to run that just these three tests.0</p>
<h2>A simple Unit Test</h2>
<p>A test for this that would give use 100% code coverage and covers the three most obvious tests would be as follows:</p>
<pre class="brush: csharp; title: ; notranslate">
[Test]
public void Person_Copy_Test()
{
    // Step 1 - Arrange
    Person p = new Person() { FirstName = &quot;John&quot;, LastName = &quot;Johnson&quot;, Age = 0 };

    // Step 2 - Act
    Person copy = p.Copy();

    // Step 3 - Assert
    Assert.AreEqual(p.Age, copy.Age);
    Assert.AreEqual(p.FirstName, copy.FirstName);
    Assert.AreEqual(p.LastName, copy.LastName);
}
</pre>
<p>The code coverage is now 100%. But is this a good test? No.</p>
<p>What are the problems with our tests?</p>
<h2>Problem 1 &#8211; The Unit Test does not guarantee every property is copied</h2>
<p>In the Person.Copy() method, comment out the line that copies the Age. Run the test again.</p>
<p>Oops! The test still passes.</p>
<p>What is the problem? Well, int defaults to zero.</p>
<p>Change the age to a value other than zero and try again.  The test now fails.</p>
<pre class="brush: csharp; title: ; notranslate">
[Test]
public void Person_Copy_Test()
{
    // Step 1 - Arrange
    Person p = new Person() { FirstName = &quot;John&quot;, LastName = &quot;Johnson&quot;, Age = 25 };

    // Step 2 - Act
    Person copy = p.Copy();

    // Step 3 - Assert
    Assert.AreEqual(p.Age, copy.Age);
    Assert.AreEqual(p.FirstName, copy.FirstName);
    Assert.AreEqual(p.LastName, copy.LastName);
}
</pre>
<p>This fixed this one problem.</p>
<p>Is the test good enough now? Of course not.</p>
<h2>Problem 2 &#8211; The Unit Test does not guarantee every property is copied</h2>
<p>Wait, you might be saying that my problem 2 is named the same as problem 1 and you might think this is a typo. It is not a typo.</p>
<p>We still have a similar problem to the first problem.</p>
<p>Imagine a new user developer takes over the code, and decides that a MiddleName property is needed. Go ahead and add a middle name property. Don&#8217;t modify the Copy() method yet. Now run your Unit Test again.</p>
<p>Our one test still passes.</p>
<p>Shouldn&#8217;t there be a test that fails because the copy is now failing to copy to all properties? Yes there should. We have just identified a new test that actually includes the previous three tests. The goal of our three tests were all the same overall goal. Our three tests were actually slightly wrong. Instead we really had only one test: Test that when Copy() is invoked, all properties should be copied.</p>
<p>Now that we have gain more insight on what we are actually testing, how do we test it? How do we test all properties. One idea might be to use reflection.</p>
<pre class="brush: csharp; title: ; notranslate">
[Test]
public void Person_Copy_Test()
{
    // Step 1 - Arrange
    Person p = new Person() { FirstName = &quot;John&quot;, LastName = &quot;Johnson&quot;, Age = 25 };

    // Step 2 - Act
    Person copy = p.Copy();

    // Step 3 - Assert
    PropertyInfo[] propInfo = typeof(Person).GetProperties();
    foreach (var prop in propInfo)
    {
        object pObj = typeof(Person).GetProperty(prop.Name).GetValue(p, null);
        object copyObj = typeof(Person).GetProperty(prop.Name).GetValue(copy, null);
        Assert.AreEqual(pObj, copyObj);
    }
}
</pre>
<p>At first this looked like a good idea, because it will check each property for us, even properties added in the future. However, it turns out that because int and string have default values, This test didn&#8217;t exactly test what we wanted to test. This test still passes when it should fail and that is a problem.</p>
<p>Remember the test: to test that when Copy() is invoked, all properties should be copied.</p>
<p>We need guarantee that each property was called and to do this. If only we were using an interface, then we could mock the interface and assert that each public property were called&#8230;A good rule to live by when developing is if you hear yourself say or you think &#8220;if only&#8230;&#8221; you should actually try implementing the &#8220;if only&#8230;&#8221; you just thought of. So lets do that.</p>
<p>This is a little longer of a solution but a better design, so here are some steps to get there.</p>
<ol>
<li>Create an IPerson interface that has the properties and methods we want to guarantee exist.
<pre class="brush: csharp; title: ; notranslate">
using System;

namespace FriendDatabase
{
    public interface IPerson
    {
        int Age { get; set; }
        String FirstName { get; set; }
        String MiddleName { get; set; }
        String LastName { get; set; }

        IPerson Copy();
        void CopyTo(IPerson inIPerson);
    }
}
</pre>
</li>
<li>Change the person object so that you can never get a Person object, you always get an IPerson.</li>
<li>We also need to be able to mock the instance of IPerson that is getting created by the Copy method and our current design doesn&#8217;t allow for that. Lets change our Copy method and add a CopyTo method to make this possible.
<pre class="brush: csharp; title: ; notranslate">
using System;

namespace FriendDatabase
{
    public class Person : IPerson
    {
        #region Constructor
        ///
&lt;summary&gt; /// Nobody should use Person, but on GetPerson()
 /// which returns and IPerson
 /// &lt;/summary&gt;
        protected Person()
        {
        }
        #endregion

        #region Properties
        public int Age { get; set; }
        public String FirstName { get; set; }
        public String MiddleName { get; set; }
        public String LastName { get; set; }
        private String NickName { get; set; }
        #endregion

        #region Methods
        public static IPerson GetPerson(string inFirstName, string inMiddleName, String inLastName = null, int inAge = 0)
        {
            return new Person()
            {
                FirstName = inFirstName,
                MiddleName = inMiddleName,
                LastName = inLastName,
                Age = inAge
            };
        }

        public IPerson Copy()
        {
            IPerson retIPerson = new Person();
            CopyTo(retIPerson);
            return retIPerson;
        }

        public void CopyTo(IPerson inIPerson)
        {
            inIPerson.Age = this.Age;
            inIPerson.FirstName = this.FirstName;
            inIPerson.LastName = this.LastName;
        }
        #endregion
    }
}
</pre>
</li>
<li>Now we need a Mocking tool. Download you favorite mocking library (RhinoMocks, NMock2, or MOQ). I am going to use NMock2 which can be downloaded here: <a href="http://sourceforge.net/projects/nmock2/" rel="nofollow">NMock2</a></li>
<li>Lets put the mocking library in a lib directory in your test project.</li>
<li>Add a reference to the dll.</li>
<li>Now lets change our test. Lets go ahead and use the reflection still, but this time, we want to guarantee that each property was called.
<pre class="brush: csharp; title: ; notranslate">
[Test]
public void Person_Copy_All_Properties_Copied_Test()
{
    // Step 1 - Arrange
    Mockery mock = new Mockery();
    IPerson p = Person.GetPerson(&quot;John&quot;, &quot;J.&quot;, &quot;Johnson&quot;, 25);
    IPerson mockIPerson = mock.NewMock();
    PropertyInfo[] propInfo = typeof(IPerson).GetProperties();

    // Step 2 - Expect
    foreach (var prop in propInfo)
    {
        Expect.AtLeastOnce.On(mockIPerson).SetProperty(prop.Name);
    }

    // Step 3 - Act
    p.CopyTo(mockIPerson);

    // Step 4 - Assert
    mock.VerifyAllExpectationsHaveBeenMet();
}
</pre>
<p style="font-size: 12px">Note: NMock2 requires a slight variation of the Arrange, Act, Assert (AAA) unit test model in that it is more Arrange, Expect, Act, Assert (AEAA), which is just as good and just as clean. One could argue that the expectations are part of the Arrange and I would somewhat agree with that too.</li>
<li>Ok, now run your test again and it should fail because we are not calling MiddleName in our CopyTo() method. You have now written the correct unit test for the goal.</li>
</ol>
<p>So by taking time to think of the correct test and Unit Testing the correct test, solving the right problem, we gained a few benefits.</p>
<ul>
<li>Better future maintainability</li>
<li>Better design</li>
<li>Testable design</li>
</ul>
<p>Now hopefully you can go and do this when you write your Unit Tests.</p>
<p>Return to <a href="/programming-development/csharp-unit-test-tutorial/">C# Unit Test Tutorial</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rhyous.com/2012/05/07/better-unit-tests-in-csharp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Mocking an internal interface with InternalsVisibleTo in C#</title>
		<link>http://www.rhyous.com/2012/05/04/mocking-an-internal-interface-with-internalsvisibleto-in-csharp/</link>
		<comments>http://www.rhyous.com/2012/05/04/mocking-an-internal-interface-with-internalsvisibleto-in-csharp/#comments</comments>
		<pubDate>Fri, 04 May 2012 14:39:26 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[Unit Tests]]></category>

		<guid isPermaLink="false">http://www.rhyous.com/?p=4938</guid>
		<description><![CDATA[Previously, posted instructions for setting this up. How to Mock an internal interface with NMock2? Attached is a sample solution that demonstrates actually implementing this. ExampleOfIVT.zip]]></description>
			<content:encoded><![CDATA[<p>Previously, posted instructions for setting this up.</p>
<p><a title="Permanent Link to How to Mock an internal interface with NMock2?" href="http://www.rhyous.com/2012/05/03/nmock2-internal-interface/" rel="bookmark">How to Mock an internal interface with NMock2?</a></p>
<p>Attached is a sample solution that demonstrates actually implementing this.</p>
<p><a href="http://www.rhyous.com/wp-content/uploads/2012/05/ExampleOfIVT.zip">ExampleOfIVT.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rhyous.com/2012/05/04/mocking-an-internal-interface-with-internalsvisibleto-in-csharp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Mock an internal interface with NMock2?</title>
		<link>http://www.rhyous.com/2012/05/03/nmock2-internal-interface/</link>
		<comments>http://www.rhyous.com/2012/05/03/nmock2-internal-interface/#comments</comments>
		<pubDate>Thu, 03 May 2012 22:28:21 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[Mock]]></category>
		<category><![CDATA[Unit Tests]]></category>

		<guid isPermaLink="false">http://www.rhyous.com/?p=4936</guid>
		<description><![CDATA[I was attempting to mock an internal interface with NMock2 and no matter what I tried I continued to get the following failure. Obviously I have a project, ExampleOfIVT, and a test project, ExampleOfIVTTests. All the Google searching suggested that I should be adding these lines to my AssemblyInfo.cs file in the ExampleOfIVT project but [...]]]></description>
			<content:encoded><![CDATA[<p>I was attempting to mock an internal interface with NMock2 and no matter what I tried I continued to get the following failure.</p>
<pre class="brush: plain; title: ; notranslate">
Test 'M:ExampleOfIVT.PersonTests.FirstTest' failed: Type is not public, so a proxy cannot be generated. Type: ExampleOfIVT.IPerson
	Castle.DynamicProxy.Generators.GeneratorException: Type is not public, so a proxy cannot be generated. Type: ExampleOfIVT.IPerson
	at Castle.DynamicProxy.DefaultProxyBuilder.AssertValidType(Type target)
	at Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
	at NMock2.Monitoring.CastleMockObjectFactory.GetProxyType(CompositeType compositeType)
	at NMock2.Monitoring.CastleMockObjectFactory.CreateMock(Mockery mockery, CompositeType typesToMock, String name, MockStyle mockStyle, Object[] constructorArgs)
	at NMock2.Internal.MockBuilder.Create(Type primaryType, Mockery mockery, IMockObjectFactory mockObjectFactory)
	at NMock2.Mockery.NewMock[TMockedType](IMockDefinition definition)
	at NMock2.Mockery.NewMock[TMockedType](Object[] constructorArgs)
	PersonTests.cs(59,0): at ExampleOfIVT.PersonTests.FirstTest()
</pre>
<p>Obviously I have a project, ExampleOfIVT, and a test project, ExampleOfIVTTests.</p>
<p>All the Google searching suggested that I should be adding these lines to my AssemblyInfo.cs file in the ExampleOfIVT project but these line did NOT work.</p>
<p>Please not that I downloaded NMock2 from here: <a href="http://sourceforge.net/projects/nmock2/" rel="nofollow">http://sourceforge.net/projects/nmock2/</a></p>
<pre class="brush: plain; title: Not Working; notranslate">
[assembly: InternalsVisibleTo(&quot;Mocks&quot;)]
[assembly: InternalsVisibleTo(&quot;MockObjects&quot;)]
</pre>
<p>Turns out that they have changed to use Castle.DynamicProxy and so the only assembly I needed was this one.</p>
<pre class="brush: plain; title: Working; notranslate">
[assembly: InternalsVisibleTo(&quot;DynamicProxyGenAssembly2&quot;)]
</pre>
<p>Return to <a href="/programming-development/csharp-unit-test-tutorial/">C# Unit Test Tutorial</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rhyous.com/2012/05/03/nmock2-internal-interface/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Beginning Unit Testing Tutorial in C# with NUnit (Part 2)</title>
		<link>http://www.rhyous.com/2012/04/30/beginning-unit-testing-tutorial-in-c-with-nunit-part-2/</link>
		<comments>http://www.rhyous.com/2012/04/30/beginning-unit-testing-tutorial-in-c-with-nunit-part-2/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 12:55:34 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[Unit Tests]]></category>

		<guid isPermaLink="false">http://www.rhyous.com/?p=4849</guid>
		<description><![CDATA[Continued from Beginning Unit Testing Tutorial in C# with NUnit (Part 1) Step 4 &#8211; Create your first NUnit test Choose a method from the class you want to test. For example, SimpleAddSubtractTool has the Add() method. Create a corresponding test method.  Try to name your test as follows: Method_Param1_Param2_Test() and you can add any other [...]]]></description>
			<content:encoded><![CDATA[<p>Continued from <a href="/2012/04/30/beginning-unit-testing-tutorial-in-c-with-nunit/">Beginning Unit Testing Tutorial in C# with NUnit (Part 1)</a></p>
<h2>Step 4 &#8211; Create your first NUnit test</h2>
<ol>
<li>Choose a method from the class you want to test. For example, SimpleAddSubtractTool has the Add() method.</li>
<li>Create a corresponding test method.  Try to name your test as follows: Method_Param1_Param2_Test() and you can add any other words to help clarify the test.</li>
<li>Place the [Test] attribute above the test function.</li>
<li>Add code to test the Add function.<br />
Note: A common testing method is Arrange Act Assert (AAA). Lets write the code in our method with this in mind.</p>
<pre class="brush: csharp; title: ; notranslate">
        [Test]
        public void Add_1_And_2_Test()
        {
            // Step 1 - Arrange (Basically create the objects)
            SimpleAddSubtractTool tool = new SimpleAddSubtractTool();

            // Step 2 - Act
            int actual = tool.Add(1, 2);

            // Step 3 - Assert
            int expected = 3; // This makes it clear what you expect.
            Assert.AreEqual(expected, actual);
        }
</pre>
</li>
<li>Now Create a similar test for the Subtract method.</li>
</ol>
<p>You are now ready to run your test.</p>
<h2>Step 5 &#8211; Run the test</h2>
<p>You might think you can just right-click on the project and choose Run Tests. However, by default you cannot do this. To add a plugin called TestDriven.NET that provides this right-click Run Tests functionality,  see this post.</p>
<p><a href="/2012/04/28/how-to-run-unit-tests-in-visual-studio/">How to run a Unit Tests in Visual Studio?</a></p>
<p>Now you should be able to run your tests.</p>
<h2>Step 6 &#8211; Check your code coverage</h2>
<p>Code Coverage is basically the number of lines of code tested divided by the total number of lines of code.  If you have 10 lines of code but your test only touches five lines of code, you have 50% code coverage. Obviously the goal is 100%.</p>
<p>Again, by default you cannot just right-click and run the Unit Tests unless you have installed TestDriven.NET.</p>
<p>You should now see your code coverage. The Add() method should be 100% covered.</p>
<h2>Step 7 &#8211; Parameter Value Coverage</h2>
<p>You may think that you are good to go. You have unit tests, and your code is 100% covered (based on line coverage). Well, there are bugs in the code above and you haven&#8217;t found them, so you are not done. In order to find these bugs you should research the possible parameter values. You will find some more tests to run. I coined the term Parameter Value Coverage (PVC). Most code coverage tools completely ignore PVC.</p>
<p>Both parameters are of the type int or System.Int32. So these are 32 bit integers.</p>
<p>How many possible values should be tested to have 100% PVC? You should have at least these five:</p>
<ol>
<li>Positive value: 1, 2, 3, &#8230;, 2147483647.</li>
<li>Negative vlaue: -1, -2, -3, &#8230;, -2147483648.</li>
<li>Zero</li>
<li>int.MaxValue or 2147483647.</li>
<li>int.MinValue or -2147483648.</li>
</ol>
<div>So while we have 100% code coverage we only have 20% PVC.</div>
<div></div>
<div>So looking at these five possible values, the following questions come to mind.</div>
<ul>
<li>What happens if you add 1 (or any number for that matter) to int.MaxValue?</li>
<li>What happens if you subtract 1 (or any number for that matter) from int.MinValue?</li>
</ul>
<p>Well, write unit tests to answer these questions. Adding 1 to 2,147,483,647 will fail if the expected value is the positive integer 2,147,483,648. In fact, the answer is the negative integer -2,147,483,648. Figure out why this is. Determine where the bug is, then decide how to handle it.</p>
<pre class="brush: csharp; title: ; notranslate">
[Test]
        public void Add_Int32MaxValue_and_1_Test()
        {
            // Step 1 - Arrange (Basically create the objects and prepare the test)
            SimpleAddSubtractTool tool = new SimpleAddSubtractTool();

            // Step 2 - Act (Bacically call the method)
            int actual = tool.Add(int.MaxValue, 1);

            // Step 3 - Assert (Make sure what you expect is true)
            int expected = 2147483648; // This won't even compile...
            Assert.AreEqual(expected, actual);
        }
</pre>
<p>It is up to you to decide how to fix this bug, as the right way to fix this bug is not part of this article.</p>
<p>Return to <a href="/programming-development/csharp-unit-test-tutorial/">C# Unit Test Tutorial</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rhyous.com/2012/04/30/beginning-unit-testing-tutorial-in-c-with-nunit-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Beginning Unit Testing Tutorial in C# with NUnit (Part 1)</title>
		<link>http://www.rhyous.com/2012/04/30/beginning-unit-testing-tutorial-in-c-with-nunit/</link>
		<comments>http://www.rhyous.com/2012/04/30/beginning-unit-testing-tutorial-in-c-with-nunit/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 12:52:06 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[Unit Tests]]></category>

		<guid isPermaLink="false">http://www.rhyous.com/?p=4828</guid>
		<description><![CDATA[So how do you get started with Unit Testing? Well, you need a Unit Testing framework and some basic know how. This article will help you with both. NUnit is a commonly used testing framework for C#. Well, NUnit has a guide  but I found it incomplete for a newbie, though perfectly acceptable for an experienced [...]]]></description>
			<content:encoded><![CDATA[<p>So how do you get started with Unit Testing? Well, you need a Unit Testing framework and some basic know how. This article will help you with both.</p>
<p>NUnit is a commonly used testing framework for C#. Well, NUnit has a guide  but I found it incomplete for a newbie, though perfectly acceptable for an experienced developer.<br />
<a href="http://nunit.org/index.php?p=quickStart&amp;r=2.6" rel="nofollow">http://nunit.org/index.php?p=quickStart&amp;r=2.6</a></p>
<p>Lets assume that you have a project with this class:</p>
<pre class="brush: csharp; title: SimpleAddSubtractTool.cs; notranslate">
namespace NUnitBasics
{
    public class SimpleAddSubtractTool
    {
        public int Add(int value1, int value2)
        {
            return value1 + value2;
        }

        public int Subtract(int value1, int value2)
        {
            return value1 - value2;
        }
    }
}
</pre>
<p>You need to test these methods and find bugs in them and despite how simple they appear, there are bugs in the above methods.</p>
<h2>Step 1 &#8211; Install NUnit</h2>
<ol>
<li>Go to this url and download the latest version of NUnit.<br />
<a href="http://nunit.org/?p=download" rel="nofollow">http://nunit.org/?p=download</a></li>
<li>Run the installer.</li>
</ol>
<h2>Step 2 &#8211; Create an NUnit testing project in Visual Studio</h2>
<p>You can do this manually, as described below, or you can download my templates: <a href="/2012/04/25/nunit-project-and-item-templates-for-visual-studio/">NUnit Project Template for Visual Studio</a></p>
<h3>Part 1 &#8211; Creating the Project</h3>
<ol>
<li>In Visual Studio, open the solution that holds the project and code that needs to be tested.</li>
<li>Right-click on the solution and choose Add | New Project.</li>
<li>In the tree on the left, select Visual C# | Windows.<br />
Note: You would think you would select a Test project, but that is for the testing framework built into Visual Studio, MSTest.</li>
<li>Click to highlight Class Library.</li>
<li>Provide a project name.<br />
Important: It is usually best practice to give the test project the same name as the project it is test only with the word &#8220;test&#8221; at the end. For example, a project called MyProject would have a corresponding test project called MyProjectTests.</li>
<li>Click Ok.</li>
</ol>
<div>
<h3>Part 2 &#8211; Configuring the project</h3>
<ol>
<li>Click Reference | Add Reference.</li>
<li>Click the first tab: .NET</li>
<li>Find and add nunit.framework and nunit.mocks.</li>
<li>Click Reference | Add Reference (again).</li>
<li>Choose Projects.</li>
<li>Select the project that is being tested.</li>
<li>Click Ok.</li>
<li>Delete the Class1.cs as it is not used.</li>
</ol>
<div></div>
</div>
<h2>Step 3 &#8211; Start your first Unit Test</h2>
<p>You can add a class and manually change it to be an NUnit test class as described below, or you can download my template: <a href="/2012/04/25/nunit-project-and-item-templates-for-visual-studio/">NUnit Item Template for Visual Studio</a></p>
<h3>Part 1 &#8211; Create the class file</h3>
<ol>
<li>Right-click on your project and choose Add | Class.</li>
<li>Name the class after the class you are going to test. For example, a class called SimpleAddSubtractTool would have a corresponding test class called SimpleAddSubtractToolTests.</li>
<li>Click OK.</li>
</ol>
<h3>Part 2 &#8211; Convert to an NUnit class file</h3>
<ol>
<li>Add a using reference to NUnit.Framwork.</li>
<li>Add the [TestFixture] attribute above the class name.</li>
<li>Add Setup and Tear down attributes and methods if needed.</li>
</ol>
<pre class="brush: csharp; title: SimpleAddSubtractToolTest.cs; notranslate">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace NUnitBasicsTests
{
    /// &lt;summary&gt;
    /// A test class for ...
    /// &lt;/summary&gt;
    [TestFixture]
    public class SampleTest
    {
        #region Setup and Tear down
        /// &lt;summary&gt;
        /// This runs only once at the beginning of all tests and is used for all tests in the
        /// class.
        /// &lt;/summary&gt;
        [TestFixtureSetUp]
        public void InitialSetup()
        {

        }

        /// &lt;summary&gt;
        /// This runs only once at the end of all tests and is used for all tests in the class.
        /// &lt;/summary&gt;
        [TestFixtureTearDown]
        public void FinalTearDown()
        {

        }

        /// &lt;summary&gt;
        /// This setup funcitons runs before each test method
        /// &lt;/summary&gt;
        [SetUp]
        public void SetupForEachTest()
        {
        }

        /// &lt;summary&gt;
        /// This setup funcitons runs after each test method
        /// &lt;/summary&gt;
        [TearDown]
        public void TearDownForEachTest()
        {
        }
        #endregion
    }
}
</pre>
<p>You are now ready to create your first test.</p>
<p>Note: For this basic class you can actually delete the Setup and Tear down region as it won&#8217;t be used, however, it is good to know how to use it when you get started.</p>
<h2><a href="http://www.rhyous.com/2012/04/30/beginning-unit-testing-tutorial-in-c-with-nunit-part-2/">Beginning Unit Testing Tutorial in C# with NUnit (Part 2)</a></h2>
<p>Return to <a href="/programming-development/csharp-unit-test-tutorial/">C# Unit Test Tutorial</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rhyous.com/2012/04/30/beginning-unit-testing-tutorial-in-c-with-nunit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to run Unit Tests in Visual Studio?</title>
		<link>http://www.rhyous.com/2012/04/28/how-to-run-unit-tests-in-visual-studio/</link>
		<comments>http://www.rhyous.com/2012/04/28/how-to-run-unit-tests-in-visual-studio/#comments</comments>
		<pubDate>Sat, 28 Apr 2012 22:01:54 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[Unit Tests]]></category>

		<guid isPermaLink="false">http://www.rhyous.com/?p=4837</guid>
		<description><![CDATA[NUnit does not currently have a plugin for Visual Studio that allows you to run test right from Visual Studio. However, it can be done. You can use a number of tools but the tool I prefer is TestDriven.NET. Step 1 &#8211; Download and Installation Go to the TestDriven.NET web site. Click the download link. [...]]]></description>
			<content:encoded><![CDATA[<p>NUnit does not currently have a plugin for Visual Studio that allows you to run test right from Visual Studio. However, it can be done.</p>
<p>You can use a number of tools but the tool I prefer is <a href="http://testdriven.net">TestDriven.NET</a>.</p>
<h2>Step 1 &#8211; Download and Installation</h2>
<ol>
<li>Go to the TestDriven.NET web site.</li>
<li>Click the download link.</li>
<li>Download the appropriate version (Enterprise, Professional, or Personal).</li>
<li>Close Visual Studio.</li>
<li>Install TestDriven.NET.</li>
</ol>
<h2>Step 2 &#8211; Running Unit Tests</h2>
<ol>
<li>Open your solution that has a test project in Visual Studio.</li>
<li>Right-click on your test project and you should see a Run Test(s) and a Test With option.<br />
<a href="http://www.rhyous.com/wp-content/uploads/2012/04/Run-Unit-Test-From-Visual-Studio.png"><img class="alignnone size-full wp-image-4840" title="Run Unit Test From Visual Studio" src="http://www.rhyous.com/wp-content/uploads/2012/04/Run-Unit-Test-From-Visual-Studio.png" alt="" width="612" height="288" /></a></li>
<li>Select Run Test(s).</li>
</ol>
<div>You are now running tests just by right-clicking in Visual Studio.</div>
<p>Notice that you can also run tests in the Debugger and with Coverage and with Performance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rhyous.com/2012/04/28/how-to-run-unit-tests-in-visual-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

