Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create test methods more quickly

I like to create my test methods like:

Should_not_allow_negative_number_in_value()

But it's pretty boring to keep typing _ every time, and it also has always the same signature...

so... anybody knows to make it faster??

thanks!

like image 357
Carol Avatar asked Apr 21 '11 02:04

Carol


4 Answers

Something that might automate (not at all, but a little more if you use this naming notation) this process:

I usually name my tests like this:

MethodToTest_State_ExpectedBehavior

Example:

[Test]
public void ConvertToInt32_NullValue_ThrowException
{
    //Test method body
}

You can install ReSharper, and create a new Live Template like:

[Test]
public void $Method$_$State$_$Expected$()
{
    $END$
}

and asign a shortcut like tst.

Now, everytime you want to add a new method, you just need to start writing tst and press TAB two times, and it will create that method for you, placing the caret on the Method name. After you press Enter, the caret will move to the place you write the State name, then for the Expected, and then it will be placed where says $END$.

Edit:
That could be helpful too if you name all your tests with _Should. Something like:

ConvertToInt32_NullValue_ShouldReturnTrue

Then you can modify your template to:

[Test]
public void $Method$_$State$_Should$Expected$()
{
    $END$
}

You could even try to group your naming conventions into a few groups, and create a snippet/template for each of them.

Edit 2:
More about this test naming convention: Naming Standards For Unit Tests, by Roy Osherove, author of The Art Of Unit Testing.

like image 93
Oscar Mederos Avatar answered Oct 24 '22 04:10

Oscar Mederos


Use shorter names and don't write sentences into a method name use something more like

DisallowNegativeValuesTest()
like image 37
Craig Suchanec Avatar answered Oct 24 '22 06:10

Craig Suchanec


If you're looking for readable tests, look into Cucumber & Gherkin as a BDD framework.

like image 37
Austin Salonen Avatar answered Oct 24 '22 05:10

Austin Salonen


There's a few options I know of for making this easier: Use AutoHotkey or Use ReSharper LiveTemplates

like image 1
Richard Banks Avatar answered Oct 24 '22 05:10

Richard Banks