Using a naming style for unit test methods as suggested in "The Art of Unit Testing"

I am usually happy with the UpperCamelCase naming style for my methods, except when I write unit tests in which I would like to follow Roy Osherove's suggestion from his book The Art of Unit Testing.

Here is one example,

[Test]

public void Analyze_TooShortFileName_ErrorLoggedToService()
{


// ...
}

The idea is to state <the method under test> _ <the state being tested> _ <the expected outcome of the test>

How do I set up ReSharper to support exactly this naming style for my unit test methods? I do not expect ReSharper to analyze my unt test and verify that each of the three parts above make sense, although that would be a cool feature, I just want ReSharper to help me to consistently use three chunks of text with under scores in between for my unit test methods - and not for all other methods.

Regards,
Brian

5 comments
Comment actions Permalink

Hello Brian,

Open ReSharper | Options | Languages | Common | Naming Style, press 'Advanced
settings...' and change the value of 'Event subscription on fields' from
'$object$_On$event$' to '$object$_$event$'. This will allow underscores in
method names (including test methods). Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

I am usually happy with the UpperCamelCase naming style for my
methods, except when I write unit tests in which I would like to
follow Roy Osherove's suggestion from his book The Art of Unit
Testing.

Here is one example,

public void Analyze_TooShortFileName_ErrorLoggedToService()
{
// ...
}
The idea is to state <the method under test> _ <the state being
tested> _ <the expected outcome of the test>

How do I set up ReSharper to support exactly this naming style for my
unit test methods? I do not expect ReSharper to analyze my unt test
and verify that each of the three parts above make sense, although
that would be a cool feature, I just want ReSharper to help me to
consistently use three chunks of text with under scores in between for
my unit test methods - and not for all other methods.

Regards,
Brian
---
Original message URL:
http://devnet.jetbrains.net/message/5280674#5280674



0
Comment actions Permalink

Thanks, this works!

I really would like underscores to be valid for unit test methods only. Is there any way to e.g. make this formatting rule active for methods marked with [TestMethod]?

Where can I learn more about the syntax used? It seems like sheer magic that changing '_On' to '_' will do the trick? And what about the '$object$' and '$event$' part? I don't get it, but I am sure that if I understood it fully I could do lots of other cool things with RS.

What do you think about my idea to let RS do some kind of checking if under scores follow the naming scheme suggested by Roy Osherove?

0
Comment actions Permalink

Hello Brian,

This is actually just a "trick" that allows underscores in method names.
We're considering to provide separate option to support underscores, so that
it could be configured for test methods separately. At the moment, this option
makes underscores possible for all methods, not only just the test ones.
Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Thanks, this works!

I really would like underscores to be valid for unit test methods
only. Is there any way to e.g. make this formatting rule active for
methods marked with ?

Where can I learn more about the syntax used? It seems like sheer
magic that changing '_On' to '_' will do the trick? And what about the
'$object$' and '$event$' part? I don't get it, but I am sure that if I
understood it fully I could do lots of other cool things with RS.

What do you think about my idea to let RS do some kind of checking if
under scores follow the naming scheme suggested by Roy Osherove?

---
Original message URL:
http://devnet.jetbrains.net/message/5280823#5280823



0
Comment actions Permalink

It looks like the trick stopped working when I downloaded the latest ReSharper build?

JetBrains ReSharper 5.1 Full Edition
Build 5.1.1766.4 on 2011-01-12T16:31:10

Any ideas?

/Brian

0
Comment actions Permalink

Hi Andrey,

This stopped working in recent versions of ReSharper. Do you have any suggestions?

Regards,

Brian

0

Please sign in to leave a comment.