25 Juli 2010

How to change the ReSharper naming style for test methods

For normal methods I use the Pascal casing convention (or UperCamelCase as it is called by ReSharper). But in unit tests readability rules and therefore I use very long names like:

public void MethodUnderTest_Scenario_ExpectedResult()

ReSharper marks them as violating the naming style, which is quite annoying because this distracts from real problems. Luckily there is a way to tell ReSharper to use a different naming convention for test methods. It is a little bit hidden in the ReSharper options, but here is the way to go:

ReSharper Options –> Naming Style –> Advanced settingsimage
image
In “Affected entities” mark “Test method (property)” and disable inspections.image

Now you have no warnings in your tests anymore that complain of inconsistent naming styles. Naming styles for non test classes and methods are still working as before. This was tested with ReSharper 5.1.

Kommentare:

  1. Hi Christian!

    Funny that I came across your blog while trying to solve exactly this problem :-) Thanks for the (obvious) solution.

    Say hi to everybody from me!

    Greetings from Dublin,
    Raphael

    AntwortenLöschen
  2. Thanks for the post, it's quite helpful.

    Do you have a suggestion about what to do about the names of methods for event handlers that Visual Studio generates when you hit tab after typing += when you want to subscribe to an event?
    e.g: Button1_Clicked

    ??
    Thanks!

    AntwortenLöschen
  3. @wtheronjones
    ReSharper has settings for those, in the same "Advanced settings" window that is mentioned in this post.
    See:
    http://imagebin.org/131988

    AntwortenLöschen
  4. Thanks for saving my brain from further unnecessary expenditure of effort by relieving it from the task of compensating for this annoyance.

    AntwortenLöschen
  5. Thanks, this worked fine for me as well - even on ReSharper 6.0.
    Well, it worked, then it stopped working, then I deleted the ReSharper.user file, then it worked again.

    /Brian

    AntwortenLöschen
  6. Instead of disable inspections for your test classes and methods, you can also add for them aditional naming styles, e.g. "all_lower", or "First_upper" - top rights in your last screenshot

    AntwortenLöschen
  7. Thanks very much for this. All those wavy blue lines in my test classes were driving me nuts!

    AntwortenLöschen
  8. Something to note is that the 'Access rights' in the new rule have to include Private if your test classes or methods are private.

    AntwortenLöschen