Welcome to MSDN Blogs Sign in | Join | Help
ReSharper and xUnit.net

Those of you who have switched over to xUnit.Net may have a received a nasty surprise with the 'type members layout' feature of ReSharper: it doesn't respect method attributes. Namely, you can tell it to not reorder methods in an NUnit fixture like this: 

<Pattern>   
<Match>  
    <HasAttribute CLRName="NUnit.Framework.TestFixture"/>  
  </Match>  
</Pattern>

But you cannot tell it to not reorder your xUnit methods like this:  

<Pattern> 
   <Match>
     <And Weight="100">
       <Kind Is="method"/>
       <HasAttribute CLRName="Xunit.TestAttribute" Inherit="false"/>
      </And>
   </Match>
</Pattern>
Apparently it will only look for attributes on classes and interfaces. In the meantime, I think the simplest solution is to just create a fake attribute you can add to your fixture. 
public class ReSharperNoReorderAttribute : Attribute 
{
}
And the appropriate pattern match to ReSharper:
<Pattern>
  <Match>
      <HasAttribute CLRName="ReSharperNoReorder"/>
  </Match>
</Pattern>
All should be well after that.
Posted: Wednesday, October 03, 2007 8:10 AM by casper
Filed under:

Comments

Eugene Pasynkov said:

This sounds quite strange. What do you mean with "don't reorder method"??? It's clear when entire class members are not re-layouted, but how can you not reorder single member????

Anyway, feel free to contact me at Eugene.Pasynkov (at) jetbrains.com to solve this problem in the future

# October 3, 2007 4:52 AM

casper said:

All I'm saying is that I don't want to re-order methods that are matched by a given attribute.

The documentation, as it is, doesn't say that I can't use the <match> attribute for <kind is="method"> and <HasAttribute CLRName> together, so why would I assume this wouldn't work? :)

Just for reference, xUnit doesn't have a [Fixture] class attribute like NUnit and MSTest do. I still don't want my unit tests re-ordered.

# October 3, 2007 9:36 AM

casper said:

In case that last line wasn't clear, I have a fixture that looks like this:

public class Foo()

{

  [Test]

  public void Test1() { ... }

  [Test]

  public void Example1() {...}

}

Because fixtures are trying to tell a story, or show examples of how a class is used, I don't want the methods with the [Test] attribute re-arranged.

In the meantime, I'll use the fake attribute for the class to get a similar, if less pretty, result.

# October 3, 2007 9:39 AM

bradwils said:

It's even more than that. You don't want ANYTHING in the class to be rearranged if the class has [Test] methods.

# October 4, 2007 8:56 AM

casper said:

True. If there are non-[Test] methods, I want them left exactly where they are.

# October 4, 2007 8:58 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker