xunitcontrib 0.3 – external annotations and live templates

by Matt 9. July 2009 20:33

As part of the latest release of xunitcontrib, I’ve added two new features, support for ReSharper’s External Annotations and Live Templates.

External Annotations

ReSharper performs some static analysis on your code, and displays a blue squiggly error indicator over anything it thinks will be a problem. One great example is possible System.NullReferenceExceptions – using an object which may be null.

Possible System.NullReferenceException is flagged 

One thing you might notice from that screenshot is that we’ve got an Assert.NotNull in there. This method throws an exception if the “otherFlagsAttribute” is null, meaning we can’t get a NullReferenceException. But ReSharper doesn’t know that, and so we still get the warning.

As I understand it, ReSharper can only analyse your source code, and not any compiled assemblies. So how do we tell it that Assert.NotNull ensures that we don’t get a NullReferenceException?

Well, ReSharper makes available an assembly called JetBrains.Annotations.dll that provides a number of attributes that can handle this scenario (plus a whole bunch more, like not null parameters, or marking parameters as string format specifiers). Handy, but a bit intrusive to your code, and not much use if you’ve not got the code available to change.

And that’s where External Annotations come in. You can now unobtrusively apply these attributes to the code through xml files. (And the ReSharper guys have very nicely produced files for the entire .net framework…)

So, that’s what this feature is. It adds the xml file telling ReSharper that xunit’s Assert.NotNull is an assert method that will stop execution of the method if the parameter is null. Similarly, there’s annotations for Assert.Null, Assert.True and Assert.False.

(Interestingly, it’s easy to concoct an example for NotNull and also for True or False (simply create an expression that is always false or always true) but I can’t create an example for Assert.Null. Answers on a postcard, please…)

To install, simply drop the xunit.xml file into ProgramFiles\JetBrains\ReSharper\v4.x\bin\External Annotations.

Live Templates

Live Templates are very much like Visual Studio’s built in snippets feature (I guess I ought to look into porting this to snippets…) but with a bit more oomph. They allow you to type an identifier that automatically expands into something useful. For example, the Live Template provided expands the word “fact” into:

"fact" live template expanded as a fact method

Which has the method name highlighted ready to edit, and when you hit enter, places the cursor into the body of the method. It also automatically adds any using statements necessary.

These are very useful, especially with the smart Intellisense shown for the assert templates:

Assert.Equal Live Template with smart intellisense

I’ve actually released two versions of this. One which uses acronyms such as “ae” and “ann” to represent “Assert.Equal” and “Assert.NotNull”, and another that uses similar acronyms, but beginning with “x” (“xe”, “xnn”). This is to avoid a collision with Live Templates for a certain other unit testing framework that I use.

Here’s a list of the Live Templates included (the words wrapped in dollar signs like $this$ are macros, and are quickly edited by pressing tab, and usually display some sort of useful Intellisense):

Shortcut #1 Shortcut #2 Expands to
ae xe Assert.Equal($expected$, $actual$)
at xt Assert.True($value$)
af xf Assert.False($value$)
ac xc Assert.Contains($expected$, $actual$)
an xn Assert.Null($value$)
ann xnn Assert.NotNull($value$)
athr xthr Assert.Throws<$T$>()
aiaf xiaf Assert.IsAssignableFrom<$T$>($object$)
ait xit Assert.IsType<$T$>($object$)
fact fact Creates an attributed test/fact method
fa fa [Fact]
theory theory Creates an attributes theory method
ta ta [Theory]
ida ida [InlineData()]

Note that not all of the assert methods are there. Only the most common have been added. And I haven’t added Assert.Same, namely because “as” is a poor acronym to go for.

This is a little trickier to install. In Visual Studio, go to ReSharper –> Live Templates, then click the button to “Mount storage” and navigate to and select the live template file of your choice (“ae” or “xe” format).

If you mount the file, then any changes are saved back to this file. If you import it, then any changes are kept in a copy of the config file. I prefer mounting the file, as it gives me more flexibility to change the templates and pass the changes around.

So that covers external annotations and live templates. As ever, if you have any bugs or comments, please use the Issues and Discussion pages of the CodePlex project site. And feel free to get in touch on Twitter, too (via @citizenmatt or #xunitcontrib).

Tags: ,

Comments (52) -

Neal Blomfield
Neal Blomfield New Zealand
7/10/2009 1:55:32 AM #

I have got support for the runwith attribute going although it was for a slightly older version of the code.  Requires that anyone using a custom test class command implement a simple interface to do the test / test class inspection.

I will grab a copy of the new codebase and then re-implement the change (as you have changed things a bit since I grabbed the source), but you can see a screenshot of it working at randomcode.net.nz/.../.  Might take me a while to get it working with 0.3 but I will send a patch when I can.

Essentially I factored out the test inspection and wrote a custom inspector and assembly loader that looks for all implementations of ITestInspector in assemblies loaded from wherever the xunit resharper runner is located.

With regards to TFS - have you considered moving hosting over to code.google.com?  Proper svn repository without all the TFS-SVN dramas (and a much better source control experience tbh)

Reply

matt
matt United Kingdom
7/11/2009 6:22:00 PM #

RunWith was deliberately not supported (for one reason or another, perhaps to prevent a clash between RunWithNunit and ReSharper's nunit support) but there's a bug that means you don't get the icon in the margin, but it (usually) does get run, especially now that more control has been passed over to the xunit runner. So it would probably be best to remove the RunWith restriction. That way it would get the icon in the margin, and any fact-based attributes would get icons too.

But I'd definitely be interested in what you've got.

And as for CodePlex a+ TFS... I put it on CodePlex because that's where xunit is. I did have problems with the subversion bridge not working with a really large restructuring (check in early and often? nah) but TFS got there in the end. TFS is pretty much ok, but not good if you don't have a fast connection, and the experience just isn't as nice as subversion (TortoiseSVN is great). But I'd rather not move it unless I have to.

Reply

best suv
best suv
7/20/2011 10:25:51 PM #

Fantastic task I like your type! Would really like to right here your feedback on my website! I am also seeking for someone to help you me make websites!

Reply

i phone 4g
i phone 4g
7/23/2011 8:22:28 PM #

Hi,what an excellent article this is,I found it on bing and I like it very much,I agree with what you have said, lots of things will be learned form your site,but I still have some questions with the last part,can you explain it for me ?I will appreciate your answer,and I will be back again!

Reply

Kylee Marett
Kylee Marett United States
1/15/2016 4:22:52 AM #

I love the dolphin saving you one.

Reply

Cornelius Styborski
Cornelius Styborski United States
1/15/2016 6:07:40 AM #

yeah, except the "dolphin saving you" one

Reply

Sharri Michonski
Sharri Michonski United States
1/15/2016 6:58:49 AM #

yeah, except the "dolphin saving you" one

Reply

Palmer Rinck
Palmer Rinck United States
1/15/2016 8:29:06 AM #

the style of keeping you connected with so many things of different types is awesome and you find it interesting everytime.

Reply

Merle Horenstein
Merle Horenstein United States
3/27/2016 9:29:41 AM #

Youre so cool! I dont suppose Ive learn something like this before. So good to find any individual with some original ideas on this subject. realy thank you for beginning this up. this website is one thing that's needed on the web, somebody with slightly originality. helpful job for bringing one thing new to the internet!

Reply

Anna Farris
Anna Farris United States
7/20/2016 7:43:29 AM #

Wow very thorough article. Thanks

Reply

Augustus Tohonnie
Augustus Tohonnie United States
9/27/2018 6:08:34 PM #

This website online can be a stroll-through for all the data you wished about this and didn’t know who to ask. Glimpse here, and also you’ll definitely uncover it.

Reply

Benton Dessert
Benton Dessert United States
9/28/2018 2:40:37 AM #

I'm typically to blogging and i actually respect your content. The article has actually peaks my interest. I'm going to bookmark your website and hold checking for brand spanking new information.

Reply

Lyman Pouliot
Lyman Pouliot United States
9/28/2018 4:45:21 AM #

Hi! Do you know if they make any plugins to assist with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good results. If you know of any please share. Kudos!

Reply

Bulah Corbett
Bulah Corbett United States
9/28/2018 6:24:36 PM #

Excellent blog here! Also your web site loads up fast! What host are you using? Can I get your affiliate link to your host? I wish my website loaded up as quickly as yours lol

Reply

Vertie Charnley
Vertie Charnley United States
9/29/2018 12:28:32 AM #

This article saved me a lot of time with research. Very well thought out. Now if only I get an A. If I do next one is on me!

Reply

Clayton Wittrup
Clayton Wittrup United States
9/29/2018 1:03:58 PM #

What’s Happening i am new to this, I stumbled upon this I have found It positively helpful and it has aided me out loads. I'm hoping to give a contribution & help different customers like its helped me. Great job.

Reply

Marcie Mcmillan
Marcie Mcmillan United States
9/29/2018 3:51:39 PM #

I have been reading out some of your articles and i can state clever stuff. I will make sure to bookmark your site.

Reply

Wilfredo Manatt
Wilfredo Manatt United States
9/29/2018 7:54:36 PM #

It never had the objective in being the perfect movie, but in terms of sheer entertainment, this one succeeded tremendously.

Reply

Thaddeus Schenewerk
Thaddeus Schenewerk United States
9/30/2018 3:37:38 AM #

I’d have to consult you here. Which isn’t some thing It’s my job to do! I quite like reading a post that may make people feel. Also, thank you for permitting me to comment!

Reply

香港物業按揭. 無論您是首次置業或是換樓,工銀亞洲都能夠為您提供度身訂做的按揭貸款計劃,切合您的真正需要。 全面樓宇按揭服務. 一般按揭貸款計劃. 適合購買 ...

Reply

Dorthy Mcfarlin
Dorthy Mcfarlin United States
10/9/2018 2:22:23 AM #

Hello, you used to write magnificent, but the last several posts have been kinda boring… I miss your great writings. Past several posts are just a little bit out of track! come on!

Reply

Orville Hegel
Orville Hegel United States
10/9/2018 9:07:27 AM #

I'm extremely impressed together with your writing talents and also with the layout to your blog. Is that this a paid subject matter or did you customize it yourself? Anyway stay up the excellent quality writing, it’s uncommon to see a nice blog like this one these days..

Reply

抗脂
抗脂 United States
10/9/2018 4:34:02 PM #

有關雙眼皮手術、眉壓眼、抬頭紋、三角眼、前額拉皮、前額提拉、抬眉手術眼袋手術、中下臉拉皮手術、提眉手術、全臉補脂、自體脂肪移植、隆鼻手術等一些相關、似是而非或有爭議的事項的披露與討論。

Reply

Gianna Dorward
Gianna Dorward United States
10/9/2018 7:46:07 PM #

Hi! I know this is kinda off topic but I'd figured I'd ask. Would you be interested in trading links or maybe guest authoring a blog post or vice-versa? My site goes over a lot of the same subjects as yours and I feel we could greatly benefit from each other. If you're interested feel free to shoot me an e-mail. I look forward to hearing from you! Great blog by the way!

Reply

Julian Silla
Julian Silla United States
10/9/2018 8:57:31 PM #

I don’t even know the way I ended up right here, however I thought this post was once good. I do not know who you're but certainly you're going to a well-known blogger should you aren't already ;) Cheers!

Reply

醫學美容 雷射 cosmetic.wiki
醫學美容 雷射 cosmetic.wiki United States
10/10/2018 5:53:51 AM #

香港美容業總會 X 青年企業家發展局——「生涯規劃」協作計劃工作坊 - 香港美容業總會Federation of Beauty Industry (H.K.) (FBIHK)  香港美容業總會 X 青年企業家發展局——「生涯規劃」協作計劃工作坊 - 香港美容業總會Federation of Beauty Industry (H.K.) (FBIHK)      

Reply

Tobias Beedy
Tobias Beedy United States
10/10/2018 6:25:28 AM #

I think this is one of the most significant info for me. And i am glad reading your article. But should remark on few general things, The web site style is wonderful, the articles is really great : D. Good job, cheers

Reply

Carmine Hanzl
Carmine Hanzl United States
10/11/2018 6:39:17 AM #

Great write-up, I am regular visitor of one’s website, maintain up the excellent operate, and It is going to be a regular visitor for a lengthy time.

Reply

Maximo Beckman
Maximo Beckman United States
10/11/2018 11:09:56 AM #

It's a shame you don't have a donate button! I'd certainly donate to this outstanding blog! I guess for now i'll settle for book-marking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this blog with my Facebook group. Chat soon!

Reply

Tena Kishi
Tena Kishi United States
10/12/2018 4:42:26 AM #

As I website possessor I believe the content matter here is rattling magnificent , appreciate it for your hard work. You should keep it up forever! Best of luck.

Reply

Gerry Meltzner
Gerry Meltzner United States
10/13/2018 11:20:26 PM #

A person necessarily lend a hand to make severely posts I’d state. This is the very first time I frequented your web page and to this point? I surprised with the analysis you made to make this particular submit extraordinary. Magnificent process!

Reply

Julee Prochak
Julee Prochak United States
10/14/2018 9:03:09 PM #

I’ll right away grab your rss as I can't find your e-mail subscription link or newsletter service. Do you've any? Please let me know so that I could subscribe. Thanks.

Reply

可安排以自動轉賬方式於供款日繳付每期供款. 樓宇按揭配套服務. 居屋按揭. 居屋車位貸款; 居屋補地價貸款; 居屋裝修貸款. 私人住宅物業按揭. 一、二手住宅物業按揭 ...

Reply

醫學美容 瘦腰 cosmetic.wiki
醫學美容 瘦腰 cosmetic.wiki United States
10/18/2018 12:59:10 AM #

GUERLAIN 嬌蘭 【宮廷舞宴系列】紅寶之吻 09宮廷舞宴版的商品介紹   GUERLAIN 嬌蘭,宮廷舞宴系列,紅寶之吻 09宮廷舞宴版       

Reply

Vivienne Podmore
Vivienne Podmore United States
10/18/2018 10:14:19 PM #

I'm still learning from you, as I'm trying to achieve my goals. I definitely love reading everything that is posted on your site.Keep the aarticles coming. I enjoyed it!

Reply

Rodney Irby
Rodney Irby United States
10/19/2018 5:07:12 PM #

You can certainly see your expertise in the work you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. Always follow your heart.

Reply

Sa+ 【保養系列】蘋果高效多胜肽緊緻精華液的商品介紹   Sa+,保養系列,蘋果高效多胜肽緊緻精華液       

Reply

Sueann Fronczak
Sueann Fronczak United States
10/20/2018 1:34:19 PM #

My brother recommended I might like this web site. He was entirely right. This post actually made my day. You cann't imagine simply how much time I had spent for this info! Thanks!

Reply

Karissa Vessels
Karissa Vessels United States
10/20/2018 11:14:05 PM #

You made some nice points there. I did a search on the issue and found most guys will agree with your site.

Reply

Carmine Silber
Carmine Silber United States
10/23/2018 9:50:50 PM #

I'm curious to find out what blog platform you are working with? I'm experiencing some minor security issues with my latest site and I would like to find something more secure. Do you have any solutions?

Reply

Artur Boruc has spoken for the first time of the personal turmoil which made last season the worst of his life. Celtic's Boruc rubbishes 'alcoholic' claims and blames slump on personal woe

Reply

Leticia Ramo
Leticia Ramo United States
11/6/2018 9:31:01 PM #

Hi are using Wordpress for your site platform? I'm new to the blog world but I'm trying to get started and create my own. Do you need any html coding expertise to make your own blog? Any help would be really appreciated!

Reply

Judy Lapatra
Judy Lapatra United States
11/10/2018 8:29:46 PM #

Wow, incredible weblog layout! How lengthy have you been running a blog for? you make blogging glance easy. The entire look of your web site is great, as smartly as the content!

Reply

Monty Stolinski
Monty Stolinski United States
11/11/2018 11:03:16 PM #

Heya just wanted to give you a brief heads up and let you know a few of the images aren't loading properly. I'm not sure why but I think its a linking issue. I've tried it in two different web browsers and both show the same results.

Reply

Cedric Lindquist
Cedric Lindquist United States
11/12/2018 5:39:34 PM #

I  as well  conceive so  , perfectly  indited post! .

Reply

Laronda Grisby
Laronda Grisby United States
11/14/2018 2:30:25 PM #

If your girlfriend cheating - Hire Wild hacker crew - http://456bosfo4pgtbrv3.onion You must use TOR browser to access.Wild hacker crew

Reply

Keli Eid
Keli Eid United States
11/15/2018 8:56:15 PM #

Perfectly, i need to assert that crafting talents possibly not which terrible, having said that i’ll you could increase ones own authoring proficiency. Working with not as much text to talk about the actual. You could check out bing designed for penning proficiency teaching web page and plans.

Reply

Celina Overmire
Celina Overmire United States
11/17/2018 8:41:23 PM #

Wow, amazing blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is excellent, as well as the content!

Reply

Adrianne Lagnese
Adrianne Lagnese United States
11/17/2018 11:45:17 PM #

Attractive section of content. I just stumbled upon your site and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Any way I’ll be subscribing to your augment and even I achievement you access consistently rapidly.

Reply

Sherilyn Loftis
Sherilyn Loftis United States
11/18/2018 1:37:11 AM #

I’ll right away grab your rss as I can not find your e-mail subscription link or e-newsletter service. Do you've any? Kindly let me know so that I could subscribe. Thanks.

Reply

Russell Unnasch
Russell Unnasch United States
11/18/2018 1:55:50 AM #

There is perceptibly a lot to realize about this.  I suppose you made some nice points in features also.

Reply

Sharon Tillman
Sharon Tillman United States
11/18/2018 2:29:50 PM #

Great blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple tweeks would really make my blog stand out. Please let me know where you got your theme. With thanks

Reply

Add comment

biuquote
  • Comment
  • Preview
Loading

Rel=Me

Month List

RecentComments

Comment RSS