Sunday 9 September 2012

Unit testing snippets

The boilerplate code for unit tests can be the longest part of writing tests and if it isn't the longest part, it is almost always the most boring part! I have created my own NUnit snippets to help save time and keystrokes, I am going to talk about these macros in this post.


Test Fixtures


  
    
NUnit test fixture nunitfixture NUnit test fixture in C# Danny Kendrick Expansion
testfixturename testfixturename testfixturename

The key things from the snippet above are

  • Shortcut - This element is how you will call the snippet in Visual Studio
  • Literal section - These elements are describing any literal values that the user will specify once the snippet is generated.
  • Code - This element is where the code that will be generated is placed. To specify where the literal values will be placed use the syntax $variable$ as shown in the snippet.
Once you have loaded the snippet into visual studio, the snippet can be generated by typing in the shortcut and hitting tab.

nunitfiture

Will turn into the code below, with the testfixturename text highlighted by Visual Studio for replacement by the user.

[TestFixture]
public class testfixturename
{
    [SetUp]
    public virtual void SetUp()
    {
    }

}

Test Fixtures

The next snippet is used to generate the tests themselves with comments for arrange, act and assert (the three A's of unit testing).



    
        
NUnit test nunittest NUnit test method in C# Danny Kendrick Expansion
outcome outcome outcome condition condition condition

This snippet will generate following code with outcome and condition highlighted by Visual Studio for replacement.

[Test]
public void Should_outcome_When_condition()
{
    // Arrange
 
    // Act
 
    // Assert
}

Test Fixtures

The last snippet I am going to talk about in this post is one I use for testing that an argument null exception is thrown correctly. This is important for me as I don't believe that the object should be create-able unless it's prerequisites are met. But this snippet could easily be modified for any type of exception.



  
    
NUnit ArgumentNullException nunitargnull NUnit test method that throws ArgumentNullException in C# Danny Kendrick Expansion
parameter parameter parameter

This snippet will generate following code with parameter highlighted by Visual Studio for replacement.

[Test, ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "Value cannot be null.\r\nParameter name: parameter")]
public void ShouldThrowAnExceptionWhenparameterIsNull()
{
    // Act
}

Summary

Hopefully the snippets described in this post can help speed up your unit test development creation times as they have done with mine. Leaving the important part of the tests for me to fill in. To read more about creating Visual Studio snippets there is heaps of information on MSDN.

Saturday 1 September 2012

Attaching to IIS easily

By default in Visual Studio it takes several actions to attach to IIS (w3wp.exe). You have to open the attach to process window (Ctrl+Alt+P), select the option to show all processes, find w3wp.exe in the list and select attach; all of this is performed multiple times a day.

This post describes how to create a macro in visual studio then create a button for it and/or a shortcut key.


Creating the macro

The first thing you need to do is to add the macro via macro IDE.

Then add a new module, I have chosen to name mine DebuggingMacros.

Finally add the code shown below into the module, save the module and close the macro IDE.

Public Module DebuggingMacros

    Public Sub AttachToWebServer() 

       Dim AspNetWp As String = "aspnet_wp.exe"

        Dim W3WP As String = "w3wp.exe" 


        If Not (AttachToProcess(W3WP)) Then

           If Not AttachToProcess(AspNetWp) Then

                System.Windows.Forms.MessageBox.Show("Can't find web server process")
            End If

        End If 

    End Sub


    Public Function AttachToProcess(ByVal ProcessName As String) As Boolean

        Dim Processes As EnvDTE.Processes = DTE.Debugger.LocalProcesses

        Dim Process As EnvDTE.Process

        Dim ProcessFound As Boolean = False 


        For Each Process In Processes

           If (Process.Name.Substring(Process.Name.LastIndexOf("\") + 1) = ProcessName) Then

                Process.Attach()

                ProcessFound = True

            End If

        Next 

 

        AttachToProcess = ProcessFound 

    End Function 



End Module

Creating the button

Once the macro has been created, we can make a button by clicking on customize as shown.

Click on the commands tab, select the toolbar radio button and then select the menu you would like to add the button to. I have chosen to add my button to the debug menu. When you have done this click on the add command button.

Select macros on the left hand side and then select the macro we created earlier.

Finally got to modify selection as shown and rename the button.


My button

This is a demonstration of my button on the debug toolbar which I named "Attach to IIS".


Creating the shortcut key

To add a shortcut key to attach to IIS, first click on options as shown.

The final step is to find the macro, I did this by entering "webserver" in the "show commands containing" field. Then entered your shortcut (mine is Ctrl+Shift+W) in the shortcut key field and hit the assign button.


Summary

By following the steps detailed above you should now be able to attach to the web server with one key combination or one click. Saving you time by removing the need to manually select the process. The same rules as attaching to w3wp through the attach to process screen still exist, that is that w3wp.exe must be running first in order to attach to it.