Pages

Friday, July 7, 2017

Make your own C# snippet to stub out Unit Tests

The point of snippets is to avoid repetitive typing and if you are currently creating unit tests, aren't you tired of writing arrange,act and assert ?

If you are like me, then the answer is Yes. And not only that but there are other areas that we can use to improve our coding experience. So take this as an introductory example to snippets, and lets create our unit test one.

What we wan to achieve is that after a couple of keystrokes we get this:


And the focus on the WhatAreAreTesting part of the function name so we can change it then one TAB press for ExpectedResult and then a final ENTER to get us to the arrange line so we can start coding.

A code snippet is just an XML file with an especific structure, you can create it in any text editor you want, in this case I will use Visual Sutio Code .

So lets open any Folder on VS Code:


then Add a new XML File:



Open the file and paste this base template on it:


The Header can have a number of different Tags (you can get a complete list in the links at the end of the article) , lets modify our Header,we will add mostly information about our snippet, but also one important part the shortcut we want to use (that is the text we want to put on vs that will trigger the insertion of the snippet), in this case lest use "uts" (for Unit Test Stub), so the Header should look like this:


The snippet Section is the one that actually performs the job, we can put our literal code in the code subsection and inside that code we can use variable, those variables are where the cursor will move on each subsequent TAB, and will allow us to change the highlighted text. So Under Our snippet section let begin by creating the two variables we want for the name of the unit test function, like this:


The IDs are the name of the variables that we can use in the code section. Our Code section is the code we want to create just replacing some text with the variables we created surrounded by $ <- that is the default variable delimiter, Also note the "end" variable is used to choose where should the cursor go in the end when we press ENTER. Our Code Section should look like this:


The Kind property is set to 'method decl', That specifies that the code snippet is a method, and therefore, must be inserted inside a class or module.

Once we have the snippet created rename it to 'unitteststub.snippet' and we have to save it to our visual studio snippet folder, for example:

c:\Users\Administrator\Documents\Visual Studio 2017\Code Snippets\Visual C#\My Code Snippets\

To test it we will open Visual Studio, locate you self inside a class and type 'uts':


Notice the helper notes about our snippet ;).
Then press TAB twice and voila, the code is displayed and the cursor is on our first variable:

change the first part of the name and press TAB to go to the second part of the name and change it then press ENTER if you are done and the cursor set us ready to code.


The complete snippet is on my github gists Here.

You may find the following links useful to develop more elaborated code snippets:

And that, my friend, It's all !

No comments:

Post a Comment