VSTS Tip: generating unit tests against ASP.NET 2.0 App_Code

Disclaimer applies, if you have better suggestions please leave comments. 

One of the unit test features is the capability to automatically generate unit tests for ASP.NET 2.0 code inside the App_Code folder.  In my experience this is one of those areas where a little bit of investment in design and organization can pay off in the long run so that you have all the code you want to be writing unit tests for in the right place.  Creating unit tests against random methods in code behind pages is not supported.  To experiment, put some central classes and methods inside the App_Code folder that represent the type of functionality you would call from the individual pages and would reasonably expect to have unit tests for.  Then inside the IDE right click on the method you want to create a unit test for, select Create Unit Tests... and follow through the wizard options to create a test project containing the unit test.   Beginning with the end in mind and organizing your code in ASP.NET 2.0 style is probably a good idea.  You can then launch these tests as you would other tests.  If you're working against a file-based website rather than an IIS one, you'll notice it fire up an instance of the lightweight local webdev.webserver.exe behind the scenes in order to execute the tests.  

In Beta 2 at least if you are writing tests on one machine and running them on a separate machine watch out for the path information provided in the code attribute for the unit test.  A solution is in the works to increase the portability so it is not machine dependent but just something to watch out for.

Comments (4)

  1. Buck Hodges – Hold the shift key and double click

    Buck has a couple of cool shortcuts. I especially…

  2. Patrick says:

    I’ve tried doing this and used "Create Tests.." on a method in the App_Code folder. It created the code for the test fine, but when i went to compile it it wanted a reference to the assembly (of the code in App_Code) in the "VSCodeGenAccessors.cs" file. But there isn’t an assembly created when I build the website.

  3. ejarvi says:

    This was a beta bug, if you add the assembly it is asking for manually it should work. There may be additional bugs if so let me know (can contact me directly via the blog link).

  4. Tornato in albergo dopo una giornata su SQL 2005 di Kimberly Tripp, per rilassarmi :-), leggo questo…

Skip to main content