Feb 5, 2009

Getting Started With Unit Testing

I'm a little late to the TDD party but since I was reading a couple of Agile development books I decided to take a fairly simple (but realistic) class that is still under development in my current application and add unit tests to it. I'm using Visual Studio 2008 Team edition. I started with the Test Wizard. The first problem happened when I tried to use the Unit Test Wizard.  My IDE crashed several times when trying to run the Wizard until it occurred to me to disable ReSharper's Code Analysis functions. Once I did this the Wizard ran to completion. Even though I only asked it to test one class it took several minutes for the Wizard to complete. It seems to analyze all the .dll dependencies in your entire project and add all the references to the test project. This seems like a good thing I was just surprised by the amount of time it took. Once the Wizard completed I tried to compile the resulting Test project. Because I have a strongly signed application and internal classes the project failed to build. The resulting error message appeared to indicate that if I signed my test project with the same key all would be fine but it wasn't that easy. Right-clicking and selecting Error Help on the error led me to the solution. I had to modify my AssemblyInfo.cs file and embed the actual full public key from the application I was trying to test. All well and good but how do you get the public key? I launched the Visual Studio Command prompt (past experience has taught me that I have fewer problems with VS tools if I start with this command prompt rather than a standard command prompt). I then used the following commands
% cd C:\PathTo\MyApplication
% sn -Tp MyApplication.exe
The resulting output is the public key. I cut and pasted this into the AssemblyInfo.cs file of my application (NOTE: Not of my test app) similar to that shown below (the actual public key was changed)
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("MyTestAassemblyName, PublicKey=" + "0024000004800000940000000602000000240000525341310004000001000100adfedd2329a0f8" + "3e057f0b14e47f02ec865e542c2dcca6349177fe3530edd5080276c48c6d02fa0a6f67738cc1a0" + "793be3322cf17b8995acc15055c00fa61b67a203c7eb2516922810ff0b17cd2e08492bdcafc4a9" + "23e6fff4caba672a4c2d0d0f5cac9aea9
5c3dce3717bb733d852c387f5f025c42c14ec8d759f7e" + "b13689be" )]
This got me a little further. Since I have .xml files that the unit under test required, the tests failed when trying to load them as they aren't copied over to the default testing directory by default. This was remedied by using the  Test->Edit Test Run Configuration menu.  In the resulting dialog I was able to Add the .xml file to the deployment as shown.
Renaming Folders
Once I had everything working, I renamed the folder that my main application was in. At some point in the process I had also turned on code coverage options in the test run configuration file. I then got a "Strong name validation failed" error. It turns out the run configuration file keeps a copy to the password key file and when the directory got renamed this reference was broken. I went back into the dialog under coverage and respecified both my target .exe for coverage and the .key file. Once this was done all the tests started working again. All is well again and I even have data driven tests working, but that's another post.
Unit Testing with a database 
My most recent issue was to hook in tests that require talking to the Sql Server Compact Edition database. When the test runs the piped value |DataDirectory| points off to the program folder where SqlCE is actually installed instead of the directory where my .exe sits. This was easily remedied by coding a new path in the the data directory variable using the following:
AppDomain.CurrentDomain.SetData("DataDirectory", @"C:\Whatever\folderpath\youwant\thatleads\toYourSDFfile");

About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.