BDD Style Naming Macro for Visual Studio
I've really jumped on board with BDD (Behaviour Driven Development) in the past week. I've been reading about it for awhile, but it wasn't until I started actually doing it on a side project of my own that I've really begun to realise how good it can be. The natural language naming style of the 'specs' (not 'tests' anymore) in different 'contexts' is an excellent way to express through code what your systems does (or should do).
If you have no idea what I'm blabbering on about, you should probably do a google search on 'BDD' or check out the links below:
Writing software BDD style really changes the way you think about how you express what your system does through your tests/specs. Writing specs in different 'Contexts' have enabled me to make my tests more precise and to the point. It is definitely something that I will strive to start using for real projects in the coming few months.
Anyway, so now that you know what BDD is, I can get to the original reason I started this post: a handy BDD naming macro that speeds up your development time by formatting your naturally written sentences into a properly constructed test case shell.
eg. You type:
should be able to load all items
<macro executed via keyboard shortcut> and you get:
[Test]
public void Should_be_able_to_load_all_items()
{
}
Having something like this just makes writing your specs a whole lot simpler.
Now I just want to point out, I didn't come up with the idea for this macro. It was developed originally by Scott Bellware and then evolved by Terry Hughes. This is outlined in a blog article here.
The code for the macro shown on the link above just replaces the spaces with underscores. I've evolved it further to also:
- Append '()' to the end of the line
- Prepend '[Test]' to the line above (because I'm still using NUnit as my testing/spec framework)
- Capitalise the first letter of the spec name
- Provide an empty method body enclosed by '{' and '}'.
Code is below:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Public Module CodeEditor
Sub FormatTestCaseBDDStyle()
If DTE.ActiveDocument Is Nothing Then Return
Dim selection As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
selection.SelectLine()
If selection.Text.Trim() = "" Then Return
Dim prefix As String = "public void "
Dim index As Integer = selection.Text.IndexOf(prefix)
Dim description As String = selection.Text.Replace(prefix, String.Empty).Trim() + "()"
description = description.Replace(" ", "_").Replace("'", "_")
description = description(0).ToString().ToUpper() + description.Substring(1)
selection.Text = ControlChars.Tab + _
ControlChars.Tab + _
"[Test]" + _
ControlChars.NewLine + _
ControlChars.Tab + _
prefix + description + _
ControlChars.NewLine + _
"{" + _
ControlChars.NewLine + _
ControlChars.Tab + _
"}"
selection.EndOfLine()
selection.LineUp()
End Sub
End Module
So basically when you sit down to write your next spec, you just write the sentence as you would if you were speaking to a human, invoke the macro keyboard shortcut, and it does the rest for you.
For help on getting the macro setup in Visual Studio check out the following links:
Just as a side note, the project I've started using BDD on (and NHibernate too for that matter) is going to be basically a CMS for another website of mine that I'm slowly building up: www.pineappletinfeet.com. I will be releasing the source code of this project in the future so you can see my journey with BDD.
Trackbacks
-
15 January 2008, 7:30 PM
DotNetKicks.com wrote:
You've been kicked (a good thing) - Trackback from DotNetKicks.com






Nice, I definitely like. But I made some changes when I added it to my macros.
The two issues I have is that it won't camel case the method name, and actually writing out the method name causes intellisense to freak out. I added some code to fix the first issue (I prefer no _'s and camel casing), changed it for VS unit testing and solved the intellisense issue by allowing the method name to be extracted from a comment, i.e.: // null case test
If DTE.ActiveDocument Is Nothing Then Return
Dim selection As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
selection.SelectLine()
If selection.Text.Trim() = "" Then Return
Dim text As String
text = selection.Text.Replace("//", "")
text = text.Trim()
Dim result As New StringBuilder()
result.AppendLine("[TestMethod()]")
result.Append("public void ")
Dim parts As String() = text.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
For Each part As String In parts
result.Append(part.Substring(0, 1).ToUpper)
result.Append(part.Substring(1, part.Length - 1))
Next
result.AppendLine("()")
result.AppendLine("{")
result.AppendLine("}")
selection.Text = result.ToString()
DTE.ExecuteCommand("Edit.FormatDocument")
selection.LineUp()
selection.NewLine()
selection.LineUp()
Reply to this
I had the same issue with IntelliSense trying to take over the world, so I turned the autocomplete of 'Letters and digits' off in my Resharper settings (Resharper had taken over the standard VS intellisense). This allowed me to type natural sentences, and if I need to invoke intellisense I just use Ctrl+Space.
Reply to this
I've been using it today and determined that I suck. Here's a better version; you 1) write, in comments, one test description per line (one or more lines) 2) select all the lines 3) run the macro. It creates multiple at once, and uses VB's facilities to do upper casing. Using the TextSelection object rather than a stringbuilder makes sure indentation of new lines is correct. Also, adds your original description as a description property
Sub FormatTestCaseBDDStyle()
If DTE.ActiveDocument Is Nothing Then Return
Dim selection As TextSelection = _
CType(DTE.ActiveDocument.Selection(), _
EnvDTE.TextSelection)
If selection.Text.Trim() = "" Then Return
Dim text As String
text = selection.Text.Replace("//", "")
selection.Text = ""
Dim lines = text.Split(Environment.NewLine.ToCharArray(), _
StringSplitOptions.RemoveEmptyEntries)
Dim result As New StringBuilder()
For Each line As String In lines
line = line.Trim()
selection.Text += "[Description(""" + line + """)]"
selection.NewLine()
selection.Text += "[TestMethod()]"
selection.NewLine()
selection.Text += "public void " + StrConv(line, _
VbStrConv.ProperCase).Replace(" ", "") + "()"
selection.NewLine()
selection.Text += "{"
selection.NewLine()
selection.NewLine()
selection.Text += "}"
selection.NewLine()
Next
selection.LineUp()
selection.LineUp()
End Sub
Reply to this
That was inspiring,
I'm new to BDD. I folowed your links ,And I liked it so much
Thanks for writing, most people don't bother.
Reply to this
A computer virus is a computer program that can copy itself and infect a computer without permission or knowledge of the user.
Reply to this
Really i am impressed from this post....the person who create this post he is a great human..thanks for shared this with us.i found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article
Reply to this
Which is the best c language to write this program?and why?
Reply to this
How to make asp.net applications work without visual studio platform?
Reply to this
Visual studio is one of the recent improvement in the field of software where the designs and animation pictures are being modified which has the major business.
Reply to this
What is the best Antivirus Software for a person who uses internet as wel as lots of pendrives?
Reply to this
Most ASP.NET developers use the standard F5to start debugging from Visual Studio. However, there is a much faster way to start debugging if you already have an instance of web application running.
Reply to this
What is BDD style naming macro for visual studio?
Reply to this
Give more details regarding this program.
Reply to this
You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it! As a rule I download everything I need (all the books, articles and tutorials) by torrent search engine but I'm really glad to find your site. keep up the good work!
Reply to this
I like how you implemented it.
Reply to this
To be a good Professional in Graphic Design or at Web Designing jobs
one must have good command on showing their expressions in images.
Reply to this
So far, I managed to go though only some of posts you discuss here, but I find them very interesting and informative. Just want say thank you for the information you have shared. Regards
James
Reply to this
What is the best Antivirus Software for a person who uses internet as wel as lots of pendrives?
Reply to this
A business (also called a company, enterprise or firm) is a legally recognized organization designed to provide goods and/or services to consumers.[1] Businesses are predominant in capitalist economies, most being privately owned and formed to earn profit that will increase the wealth of its owners and grow the business itself. The owners and operators of a business have as one of their main objectives the receipt or generation of a financial return in exchange for work and acceptance of risk. Notable exceptions include cooperative enterprises and state-owned enterprises. Businesses can also be formed not-for-profit or be state-owned.
Reply to this
A partnership is a form of business in which two or more people operate for the common goal which is often making profit. In most forms of partnerships, each partner has personal liability of the debts incurred by the business. There are three typical classifications of partnerships: general partnerships, limited partnerships, and limited liability partnerships.
Reply to this
hey!mattcalla , you have a great sense of posting, that is a great achivement in you, just keep it up.
thanks and regards to all.
Reply to this
Companies generally hold online fast cash for the purpose of meeting the transactions of the business. For instance, cash payments are to be made for the purchase of goods, for wages and for various operating and financial charges. To ensure that the firm is able to meet its obligations when payments become due in a situation in which the disbursements are in excess of the current receipts, it must have adequate cash balance.
Reply to this
Topic which you have discussed is very informative...
Reply to this
The Visual Studio Express Editions versions are definitely powerful tools for software development.
Reply to this
The United Nations found that, in 2005, there were nearly 191 million international migrants worldwide, 3 percent of the world population. This represented a rise of 26 million since 1990. Sixty percent of these immigrants were now in developed countries, an increase on 1990. Those in less developed countries stagnated, mainly because of a fall in refugees.[2] Contrast that to the average rate of globalization (the proportion of cross-border trade in all trade), which exceeds 20 percent. The numbers of people living outside their country of birth is expected to rise in the future.[3]
Reply to this
Thanks for the post. Keep the great work.
Reply to this
Appreciate the info, it’s good to know.
Reply to this
Excellent blog post, I look forward to reading more.
Reply to this
Thanks for this very useful info you have provided us. I will bookmark this for future reference and refer it to my friends. More power to your blog.
Reply to this
I participated to this kind of online tournaments since I know myself but I never managed to win the final prize...I always ended up on like 2nd place when I had a winning hand. I came to the conclusion that they were made from inside.
Reply to this
It is really a nice post, its always great reading such posts, this post is good in regards of both knowledge as well as information. Thanks for the post.
Reply to this
Hey firstly not a bad post. I am not surely i entirely agree with this point of view.
Reply to this
How do real estate experts manage to overcome the cleaning and damages after construction ?
Reply to this
That information will help me very much. thank you so much!
Reply to this
that's a good question dude. hope anyone will answer that...
Reply to this
yeah! that will be very informative if somebody will answer that.
Reply to this
Your blog is so educationally based that I was just afraid to read it as not to feel unqualified.
Reply to this
Thanks for the post. Keep the great work
Reply to this
This article gives the light in which we can observe the reality. this is very nice one and gives in depth information. thanks for this nice article Good post.....Valuable information for all.I will recommend my friends to read this for sure
Reply to this
This great blog is very interesting and enjoyable to read. I am a big fan of the subjects discussed. I also enjoy reading the comments, but notice that alot of people should stay on topic to try and add value to the original blog post. I would also encourage everyone to bookmark this page to your favourite service to help spread the word.
Reply to this
wooosh, this went straight over my head haha!
Reply to this
This great blog is very interesting and enjoyable to read. I am a big fan of the subjects discussed. I also enjoy reading the comments, but notice that alot of people should stay on topic to try and add value to the original blog post. I would also encourage everyone to bookmark this page to your favourite service to help spread the word.
Reply to this
Pretty Interesting post. Couldnt be written any better. Thanks for sharing!
Reply to this
Evaluates to the revision number component of the compiler's version number. The revision number is the fourth component of the period-delimited version number. For example, if the version number of the VC++ compiler is 15.00.20706.01, the _MSC_BUILD macro evaluates to 1.
Reply to this
Pretty Interesting post. Couldn't be written any better.
Reply to this
The United Nations found that, in 2005, there were nearly 191 million international migrants worldwide, 3 percent of the world population. This represented a rise of 26 million since 1990. Sixty percent of these immigrants were now in developed countries, an increase on 1990. Those in less developed countries stagnated, mainly because of a fall in refugees.[2] Contrast that to the average rate of globalization (the proportion of cross-border trade in all trade), which exceeds 20 percent.
Reply to this
Your blog is so educationally based that I was just afraid to read it as not to feel unqualified.
Reply to this
Thanks for sharing.
Reply to this
I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
Reply to this
This great blog is very interesting and enjoyable to read. I am a big fan of the subjects discussed.
Reply to this
Its always good to learn tips like you share for blog posting. As I just started posting comments for blog and facing problem of lots of rejections. I think your suggestion would be helpful for me. I will let you know if its work for me too. Thanks and keep post such a informative blogs. We owns a website called “24x7-LiveSupport.Com” which is a great place for 24x7 Live Chat Support.
Reply to this
I've really jumped on board with BDD (Behavior Driven Development) in the past week. I've been reading about it for a while, but it wasn't until I started actually doing it on a side project of my own that I've really begun to realise how good it can
Reply to this
that posting was a great one, it is really helpful to others.
Reply to this
Great post, thank you for sharing.
Reply to this
Good post, keep up posting.
Reply to this