14

I just upgraded to Resharper 4.5 and now see that all my BDDish test methods are marked as not conforming to the naming standard. My naming convention is like this:

public void Something_ShouldHaveThisResult()

Resharper doesn't like the underscore in the method. Is there a way to turn this off, just for test methods? I have a normal naming convention for the rest of my code.

Michael Hedgpeth
  • 7,732
  • 10
  • 47
  • 66

8 Answers8

12

How to change the ReSharper naming style for test methods

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
  • 2
    +1 Stopped the warnings, but would also be nice to enforce the "_Should" part of the method name... – g t Mar 30 '11 at 14:41
4

If you want to follow the Microsoft style guide with your non-test code sources - Have you tried using the StyleCop for ReSharper plugin?

As recommended before: disable the internal ReSharper naming rule set or toggle the inspection settings. StyleCop (thus the StyleCop ReSharper plugin) allows inheritance over the Settings.StyleCop files in your solution folder structure. So you are able to check for valid names in the "real" sources, while the analysis of the test code is disabled.

2

Resharper 4.5.1 has added this capability. You can now add a new custom naming rule that applies specifically to a test method, and allow it to contain underscores.

Kevin Dente
  • 25,430
  • 7
  • 43
  • 47
  • That is right. Set your default naming style to be what you use in the production code and add a second, non-default rule for your test naming style, so R# doesn't throw a fit when you do use it. – Jay Sep 25 '09 at 17:07
2

You could use

// ReSharper disable InconsistentNaming

// ReSharper restore InconsistentNaming

around the extremities of each class. e.g

// ReSharper disable InconsistentNaming
namespace bob
{
    [TestClass]
    public class MyTestClass
    {
        [TestMethod] 
        public void Test_Test()
        {
        }
    }
}
// ReSharper restore InconsistentNaming

This however will remove all naming warnings, and not just those on the Method name(s).

Michael Meadows
  • 27,796
  • 4
  • 47
  • 63
2

I've already added a request for this in the ReSharper bug-tracker. You can vote for it.

Joe White
  • 94,807
  • 60
  • 220
  • 330
1

You can use Agent Smith for more precise code naming conventions.

Note: the version for the final R# 4.5 seems not to be compiled yet... but I'm sure it will be there soon.

Lucero
  • 59,176
  • 9
  • 122
  • 152
  • +1 for a great plug-in; there appears to be a bug related to this particular setting, though: http://code.google.com/p/agentsmithplugin/issues/detail?id=94 – TrueWill May 13 '10 at 14:24
1

There is no need to remove rules. New Rule can be added that accept underscores

Resharper | Options -> Languages -> Common -> Naming Style and add new rule to the bottom "User defined naming rules"

Robert Vuković
  • 4,677
  • 6
  • 31
  • 47
  • 1
    It seems that adding rules here then enforces the BDD style naming conventions in your regular code. Unless you have a specific way of setting this up for just the test method names? – MotoWilliams Jun 13 '09 at 00:49
  • You are right. I don't use underscores for regular methods so I didn't noticed this. – Robert Vuković Jun 15 '09 at 10:11
0

On the menu:

Resharper | Options -> Languages -> Common -> Naming Style: remove what ever naming style you want. They should have a "disable" feature, but they don't.

CLaRGe
  • 1,821
  • 1
  • 25
  • 22
  • 7
    I'd like to enforce it for everything except for Test Methods though. If I remove the naming style, it will disable it for all methods, right? – Michael Hedgpeth Apr 16 '09 at 19:45
  • You might be right. This is new stuff for Resharper so they may update it soon. – CLaRGe Apr 17 '09 at 12:44