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.

Comments (5)
  1. Eugene Pasynkov says:

    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

  2. Casper says:

    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.

  3. Casper says:

    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.

  4. bradwils says:

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

  5. Casper says:

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

Comments are closed.

Skip to main content