You are currently browsing the monthly archive for June 2008.
In my attempts to remove even more friction from writing tests I have a new VS macro for running the tests.
I use ReSharper’s unit test runner as it significantly reduces context switch from writing the code to running it. Besides, it looks so damn sexy!! It has a Unit Test Explorer which shows you all the tests in the solution, and then a Unit Test Session where you can select groups of tests to run (and it’s tabbed so you can have different groups of tests). I use the Unit Test Session to isolate just the tests relevant to the code I’m working on.
With the previously posted BDD macro and R# templates, I get smooth flowing writing of tests but then I find myself moving to the mouse to run the tests. What I wanted to do was use shortcuts.
There is an existing shortcut in R# for showing the Unit Test Session (Ctrl+Alt+R). You can then set another shortcut for the command ReSharper_UnitTestSession_RunAll. I set Ctrl+T as the shortcut to this command.
With these 2 in place, it is reasonably quick to write test, hit Ctrl+Alt+R, Ctrl+T and see the selected tests run (including the newly written test). This is reasonably smooth.
But what I want to do is get it down to one shortcut and ideally switch focus back to the text editor so I can carry on writing my tests. Well, a quick macro bound to the shortcut Ctrl+T and I am partway there. The macro I have runs both previous commands for me but it doesn’t yet put the focus back to the text editor. I will hopefully crack this soon but I just wanted to post all this first.
Here’s the macro. Remember to bind it to your shortcut of choice.
Imports System Imports EnvDTE Imports EnvDTE80 Imports System.Diagnostics Public Module ChainedCommands Public Sub RunSelectedUnitTestSession() Dim selection As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection) Dim textPoint As TextPoint = CType(selection.ActivePoint, TextPoint) Dim line As Integer = textPoint.Line Dim offset As Integer = textPoint.LineCharOffset Dim index As Integer DTE.ExecuteCommand("ReSharper.ReSharper_ShowUnitTestSessions") DTE.ExecuteCommand("ReSharper_UnitTestSession_RunAll") selection.MoveToLineAndOffset(line, offset) End Sub End Module
I’ve been using a BDD macro based on the one JP Boodhoo published for getting more descriptive test names along with a R# template so that I can type “test” + tab, type the test name (with spaces) and then hit Alt+- and voila – BDD style test names.
This worked well for a while until some point where R# started to do autocompletion on the test name as I typed (for instance, typing ‘Check that blah is not null’ often ended up as ‘Check that blah is NotImplementedException null’). That was OK though after a quick rework of the R# template and macro so R# wraps the test name in quotes which the macro then removes.
But R# doesn’t format the end result as well (the method braces don’t indent correctly) and this bugged me for a while.
So, a quick change to the macro and the R# template and now it’s much more smooth. Typing ‘tests’ + tab and R# outputs public class “FIXTURENAME” (with fixturename highlighted and ready to type over). Type out the name of the fixture, Alt+- and the macro replaces spaces, puts in the [TestFixture] attribute, drops in the opening and closed braces and puts the caret right where I want it. Sweet.
Do the same with ‘test’ + tab, type test name, Alt+- and you get the same for a test. Extra sweet.
This is the BDD macro:
For the ReSharper templates, open up the live templates and add a template called ‘tests’ with the body of template as below:
public class "$FIXTURENAME$"
And then another one called ‘test’ with the body as
public void "$TESTNAME$"()
Mark both of these as available for C# only (or if for VB.Net, adjust the template body for the VB equivalent), and for the ‘test’ one, mark this as available only where a method declaration is allowed.
That’s it. Now you can get into the flow of typing ‘test’ TAB ‘name of test’ Alt+- and then typing in the test itself.