Tim Huffam

Dotting the I and crossing the T of I.T.

  Home  |   Contact  |   Syndication    |   Login
  152 Posts | 0 Stories | 2310 Comments | 653 Trackbacks

News

Archives

Post Categories

Interesting Blogs/Links

Unit Testing

Unit Testing
This is for TFS2008. This is based mainly on this article: http://blogs.msdn.com/b/jpr... But also had input from these: http://www.woodwardweb.com/... http://blogs.msdn.com/b/buc... http://blogs.msdn.com/b/jim... Also the following article discussing upgrading a...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

When workig on a project in VS2010 and performing a "run tests in context" the debugger was catching and halting on a test that fails, raising the exception AssertFailedException. I thought this was strange as the same test (in fact any test) that failed in VS2008 would simply just be noted in the test results as Failed... Turns out it's the way I've been running tests.. Both VS2008 and VS2010 have the same shortcut mappings: - "ctrl-r, t" for "Run tests in context" - "ctrl-r, ctrl-t" for "Debug...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

This requires a testrunconfig file that has been configured correctly. To do this I did the following: In VS2008, right click on Solution Items folder and select Add - New Item... Select Test Run Configuration and enter the name WithCodeCoverage.testruncon... Open/edit this file in VS. Select the 'Code Coverage' item on the left, then selected (checked) the assemblies I wanted to instrument Click Apply to save these changes. Note that you can select which is the active test run configuration from...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

The following will make your builds (running on a TFS Server) execute your unit tests after the build: Open your build file (eg TFSBuild.proj). VS2008 has a nice xml editor for this. Search for the RunTest node and change it from: <RunTest>false</Ru... to: <RunTest>true</Run... Look for a section that starts with: <ItemGroup> <!-- TEST ARGUMENTS ... and add nodes within this (after the comments and before the closing of the ItemGroup node) - one entry for each...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

When unit testing you sometimes need to test that values are being set to the correct literal/constant value - and this value is often stored in a resources file. However, for some reason the VS2008 'Create Private Accessor' feature did not work for me for resources (maybe be cause they're all static properties and the generated _Accessor classes need to be instantiated). So to make these resource available within the unit test project I simply made the Resources class public (by default it's internal)....
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Sometimes Visual Studio 2008 (Team System Developer Edition - with Team Explorer and TFS Power Tools) crashes (disappearing completely) when running unit tests. I have not yet tried SP1 for VS2008 - will hopefully try this in the next week or two. Not really sure what causes this - but have noticed that the following error is written to the windows event log prior to the crash (not at the same time). Event Type: Error Event Source: VSPERF Event Category: None Event ID: 1406 Date: 22/08/2008 Time:...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Using the VS2008 unit testing framework, if you want to check for an expected exception, sometimes your test will pass because the exception was raised by another line of code - after the line you were specifically trying to test. To resolve this you have to manually check for the expected exception - using a try-catch block. However there is a catch (excuse the pun).... Microsoft.VisualStudio.Test... will not fail when types are different as long as the actual...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

One of the annoying things about unit testing, is that not all members of the target code class are accessibly to the test fixtures. There are limited options to work around this (as you really don’t want to put the unit tests within the code assembly). In the past, the usual method has been to open up the access levels of the code class members. This isn’t ideal from a best practices and security point of view. However, as of .Net 2.0 a better method has been introduced. This is to use the InternalsVisibleTo...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Here's another problem I had when trying to get the Microsoft Enterprise Library - exception logging working. The following error was caused because I did not add references to all the assemblies used. MyApp.UnitTests.BuildHelper... : Microsoft.Practices.Enterpr... : The type 'Microsoft.Practices.Enterp... Microsoft.Practices.Enterpr...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

When trying to run NUnit against an assembly, it loads the assembly fine, but when you run it, it cannot find any tests (even though the assembly contains many tests). Under the 'Tests Not Run' tab, if you expand the treeview, it states: "Reason: Has no TestFixtures". This problem is caused when NUnit does not have adequate rights to the assembly. In my case it was because my project was on a network drive. A simple solution to this is to add a post build event in Visual Studio to copy the assembly...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

This is not the prettiest solution - but is the only way I could easily get it all working - so I could click the Run button from VS to startup NUnit and run my tests project. As a primer, read James Newkirks article on this (he explains the config files a bit more than I do). Firstly to get NUnit to work with .NET 2.0 assemblies you need to have this in your nunit config file (nunit-gui.exe.config): <startup> <supportedRuntime version="v2.0.50727" /> <requiredRuntime version="v2.0.50727"...
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

A thousand thank you's to James for this post:
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati