12,064,615 members (29,165 online)
Visual Studio 2005 ships with a huge number of new features, and one of the nice and useful features is Code Snippets. The Code Snippet feature allows you to add a piece of generic code that you can customize to make it work with your specific needs. In this article, I will be introducing Unit Testing Code Snippets, showing you how to invoke a code snippet, and how to quickly navigate and edit the code snippet tokens to give you the ability to quickly create test methods and fixtures. You will find both NUnit and Microsoft.VisualStudio.TestTools.UnitTesting snippets in the download.
I've been doing test-first development for a couple of years now, and when I started to use Visual Studio 2005, I was a bit disappointed at how unfriendly the unit testing tools where for developer productivity. The other reason for these snippets is to allow other developers who are not as comfortable with writing tests, to learn the unit test tools quicker and have some guidance while writing their tests.
Download the testfirstsnippets.zip file, then extract the contents of the file to either <My Documents>\Visual Studio 2005\Code Snippets or to <Visual Studio 2005 install directory>\VC#\Snippets\1033. Next, we want to open Visual Studio 2005 and open the code snippet manager window (found in the Tools menu).
On the Code Snippet Manager window, click the Add button, and the Code Snippet Directory dialog opens. Now, you need to navigate to the directory you have extracted the code snippets to, and then select the Test Driven Development folder.
After selecting the Test Driven Development folder, you should see the folder displayed in the Code Snippet Manager window. You can now browse the folder structure to see all the new snippets that are available. The Code Snippet Window should display the Test Driven Development folder structure like the topmost image on this page.
You can use the Unit Test code snippets in four different ways in Visual Studio 2005:
You can now add a new code file and then invoke Test Driven Development > NUnit > Code Fragment > Full Test Class snippet.
After you have invoked a code snippet, you'll see the code editor window displaying the following:
You can now modify the green highlighted values. Use Tab and Shift+Tab to navigate between the highlighted values, and press either Enter or Esc to exit the edit mode on the code snippet. When you use Enter, your cursor will move to the specified end tag of the snippet. On the Full Test Class snippet, you will be taken to the test method.
As the developer, you will need to think about what specification, behavior, or functionality the tests are describing. And here is where it becomes critical to name your test with an intent describing the name to communicate the behavior you are implementing with the test.
The following suggestions help to construct a good test name:
E.g., create a stack and verify that
IsEmpty is true.
- Desired Result: IsEmptyTrue - Coordinator: When - Conditions: StackCreated => IsEmptyTrue_When_StackCreated
When you create a Test method using the Unit Testing snippets, you will find this naming pattern:
One of the real nice features of the Test Method snippet, is that your method name is automatically updated as you complete the Desired Result, Coordinator, and Conditions sections of your comments, saving you heaps of coding time.
I would appreciate if people using these snippets can give me any feedback on what they'll like to see added, or if they find any bugs to leave a comment so that I can improve the library.