Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to protect Unit test names that follows MethodName_Condition_ExpectedBehaviour pattern against refactoring?

I follow the naming convention of

MethodName_Condition_ExpectedBehaviour

when it comes to naming my unit-tests that test specific methods.

for example:

[TestMethod]
public void GetCity_TakesParidId_ReturnsParis(){...}

But when I need to rename the method under test, tools like ReSharper does not offer me to rename those tests.

Is there a way to prevent such cases to appear after renaming? Like changing ReSharper settings or following a better unit-test naming convention etc. ?

like image 995
pencilCake Avatar asked Dec 27 '11 10:12

pencilCake


1 Answers

A recent pattern is to groups tests into inner classes by the method they test.

For example (omitting test attributes):

public CityGetterTests 
{
   public class GetCity
   {
      public void TakesParidId_ReturnsParis()
      {
          //...
      }

      // More GetCity tests
   }
}

See Structuring Unit Tests from Phil Haack's blog for details.

The neat thing about this layout is that, when the method name changes, you'll only have to change the name of the inner class instead of all the individual tests.

like image 137
hwiechers Avatar answered Sep 30 '22 15:09

hwiechers