Create test methods faster

I like to create my test methods, for example:

Should_not_allow_negative_number_in_value() 

But it's pretty boring to constantly print _, and it also always has the same signature ...

so ... does anyone know to make it faster

thanks!

+6
source share
5 answers

Something that can be automated (not at all, but a bit more if you use this notation notation):

I usually call my tests as follows:

MethodToTest_State_ExpectedBehavior

Example:

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

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

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

and assign a shortcut, for example tst .

Now, every time you want to add a new method, you just need to start writing tst and pressing TAB twice, and it will create this method for you by placing the caret on the Method name. After you press Enter , the carriage will move to the place where you write the name State , then for Expected , and then it will be placed where $END$ indicated.

Edit:
This can be useful if you name all your tests using _Should . Sort of:

ConvertToInt32_NullValue_ShouldReturnTrue

Then you can change your template to:

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

You can even group your naming conventions into several groups and create a fragment / template for each of them.

Edit 2:
Learn more about this test naming convention: Unit Test Naming Standards by Roy Osherove and The Art Of Unit Testing.

+3
source

use PascalCase instead of underscore_case

such as

 ShouldNotAllowNegativeNumberInValue(); 

yay, do not stress! Code is now 80% less boring.

+1
source

Use short names and do not write sentences in the method name, use something more similar to

 DisallowNegativeValuesTest() 
+1
source

If you are looking for readable tests, look at Cucumber and Gherkin as a BDD framework.

+1
source

There are several options that I know of to make this easier: Use AutoHotkey or Use ReSharper LiveTemplates

+1
source

Source: https://habr.com/ru/post/886348/


All Articles