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!
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.
Use shorter names and don't write sentences into a method name use something more like
DisallowNegativeValuesTest()
If you're looking for readable tests, look into Cucumber & Gherkin as a BDD framework.
There's a few options I know of for making this easier: Use AutoHotkey or Use ReSharper LiveTemplates
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With