The next Killer Tool for TDD

By Mike Hanson at April 04, 2012 16:58
Filed Under: Visual Studio, Testing, Test Runners, TDD, Productivity

If you practice TDD in any fashion you are going to want to get NCrunch.  It is still in beta, and may become a commercial product at some point but it is pretty stable and for me has become an essential tool.  Imagine being able to just write code (test or functional) without having to pause to run tests.  Imagine making a change to existing code and knowing immediately if it broke any tests without having to manually run the tests.  This continuous development ability is what NCrunch delivers and it has made me significantly more productive in my coding and in a couple of weeks I found this tool as necessary as having a productivity tool like CodeRush or Resharper.

 

Just spend a few minutes watching the video on the NCrunch home page and if you aren’t immediately hooked on the idea I will be very surprised.

UI Automation Build Validation

By Mike Hanson at July 30, 2011 05:26
Filed Under: .NET, Testing, UI Automation, Visual Studio

On my last few contracts I have advocated acceptance testing through UI automation, but one of the most common wishes expressed by those coding up tests was for some way to validate that our automation wrappers actually still reflected the control they wrapped in the application under test.  Something as simple as renaming a button would break our tests.  It wasn’t until the next scheduled run of our acceptance tests that the breakage would be identified.

 

As mentioned in my last post I am working on a Windows Automation Toolkit (WATKit), I haven’t finished it yet but I figured an early feature should be some kind of build time validation to resolve the above issues.  As of today WATKit includes an MSBuild Task and a few attributes to do just that.

 

WATKitBuildTask

 

The build task basically compares types in a test assembly that are decorated with AutomationTypeMappingAttribute against elements on types in one or more source assemblies.  The test assembly and source assemblies are specified in the build configuration file (aka VS 2010 project file).  Here is the what I added to the WATKit.Tests.csproj file in the WATKit source available on GitHub:

 

   1: <UsingTask TaskName="WATKIt.Build.WATKitBuildTask"
   2:             AssemblyFile="$(TargetDir)\WATKit.dll"/>
   3:   <Target Name="AfterBuild">
   4:         <WATKitBuildTask TestAssembly="$(TargetDir)WATKit.Tests.dll"
   5: SourceAssemblies="$(SolutionDir)\WATKit.TestApp.WPF\bin\Debug\WATKit.TestApp.exe" />
   6:         <WATKitBuildTask TestAssembly="$(TargetDir)WATKit.Tests.dll"
   7: SourceAssemblies="$(SolutionDir)\WATKit.TestApp.WinForms\bin\Debug\WATKit.TestApp.exe" />
   8:   </Target>

 

So the first thing it does is reference the build task with a <UsingTask /> element.  With this in place the task can be referenced as an element within any <Target /> element.  I simply uncommented the AfterBuild target that is included in most project files.  I have the task running to validate against the two test apps in the solution.

 

The build task has two required properties:

 

TestAssembly points to the assembly containing the wrappers to be validated.  It doesn’t have to be a test assembly, if you keep your wrappers in a separate class library then it is this library you should reference.

 

SourceAssemblies is a comma separated list of one or more assemblies that contain real controls and windows.  It works with WPF and WinForms applications (I haven’t tested it on other project types, I don’t see any reason it won’t work with Silverlight 4.* assemblies, but I haven’t tested it yet)

 

AutomationTypeMappingAttribute

 

In the test assembly only types with the AutomationTypeMappingAttribute are checked.  Each Property on these types is checked unless it is decorated with an IgnoreAttribute.  Methods and Fields are implicitly ignored.

 

This attribute has a single constructor argument that must be a fully qualified name of the type to validate against in one of the source assemblies.  For example the MainWindow wrapper control in the WATKit.Tests project is declared like this:

   1: [AutomationTypeMapping("WATKit.TestApp.MainWindow")]
   2: public class MainWindow : Window
   3: {
   4: }

This tells the build task to validate MainWindow in the test assembly against the first WATKit.TestApp.MainWindow it finds in the source assemblies (the search is carried out in the order assemblies are listed)

 

IgnoreAttribute

 

By default the build task will check every Property on the wrapper type unless you tell it otherwise by decorating a Property with this attribute.  You can optionally specify a Reason for ignoring it, so that others will know why.  For example the DynamicButton property of MainWindow mentioned above looks like this:

   1: [Ignore(Reason = "Button does not exist until run time")]
   2:  public Button DynamicButton { get { // removed for brevity } }

Many properties in the base classes in WATKit are decorated with this attribute to avoid build failures.

 

AutomationMemberAttribute

 

By default the build task will look for an exact match of the wrapper Property name with the name of a Field (child controls in WPF and WinForms are exposed as fields not properties) in the type it is validating against.  If for some reason the names do not match you can decorate the wrapper property with this attribute and specify the name to match.  For example the ChangeMyNameButton property of MainWindow mentioned above looks like this:

   1: [AutomationMemberMapping("IChangeMyNameButton")]
   2: public Button ChangeMyNameButton { get { // removed for brevity } }

This tells the build task to validate the ChangeMyNameButton property against a field named IChangeMyNameButton.

 

That’s it, no rocket science but it does the job and based on my experiences should help to give early feedback about breaking changes in applications being tested via UI Automation.  The build task is not limited to use with tests that use features of WATKit, it should work with any automation framework as long as you create wrappers for your controls and those wrappers expose properties representing child controls and elements exposed by the real types.

 

If you have any feedback or suggestions for improving this please let me know by commenting here or over at GitHub.

Silverlight Testing with WebAii–Part IV

By Mike Hanson at November 08, 2010 17:35
Filed Under: .NET, Testing, WebAii, Visual Studio

The fourth part of my series on testing Silverlight applications with the WebAii UI Automation Fx is now available here

Adding a dictionary for the CodeRush spell checker

By Mike Hanson at September 25, 2010 18:01
Filed Under: CodeRush, Visual Studio

As a long time CodeRush + Refactor Pro user I was pleased when DevExpress added a Spell Checker plugin, I was even more pleased when I opened the options page and saw I could change the culture to English (United Kingdom).  However my joy was short lived when I discovered that changing the culture effectively disables spell checking.  For a while I just switched it back to English (United States) and lived with the irritating differences, but finally I got fed up and decided to figure out how to get the spell checker working for proper English.  What I discovered was that changing the culture is not enough, you actually have to install a dictionary that matches that culture.  So when I select English (United Kingdom) in the culture list the spell checker is looking for a dictionary with the name en-GB the standard code for the UK culture.  One option would be to take the standard en-US dictionary and modify it, but I also discovered that CodeRush supports the Open Office dictionary format, so I went off in search of one to install.  This article describes the steps to download and install an Open Office dictionary with CodeRush.

Clean Namespace AddIn

By Mike Hanson at September 18, 2010 20:50
Filed Under: Visual Studio

One of my pet hates about Visual Studio is that it assumes that you organise folders in projects according to namespaces and adds folder names to the namespace declaration for classes.  Worse still there is no way to turn this behaviour off.

Usually I get by this by adding my classes to the root of the projec then dragging them to the relevant folder, and I strongly suggest that developers working for me do the same.  This week I came across a developer who complained about this and come up with reasons based around the lack of tool help if they forget to do this, so I decided to write an addin that automagically sets the namespace declaration of new C# classes added to match the Default Namespace setting in Project Properties.  I have called it Clean Namespace and published it on Visual Studio Gallery

No commands, menus or UI it just detects when a new C# class file is added to your project and changes the namespace declaration.  You can turn it off simply by disabling the addin via Tools, Add-in Manager...

Download Clean Namespace Visual Studio Installer

Tag cloud

Previous Articles