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 (9) -

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)


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.


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!


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!


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

I love the dolphin saving you one.


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

yeah, except the "dolphin saving you" one


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

yeah, except the "dolphin saving you" one


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.


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!


Add comment

  • Comment
  • Preview


Month List


Comment RSS