name

Unit test naming convention



Sharing buttons:

the naming convention that we used to

name the unit test is very important

make sure the same naming convention is

used for all unit tests a good unit is

named provides all the information about

that test I like Roy a shows naming

strategy for the unit test here it is

unit of work underscore state and the

test under school expected behavior unit

of work is the name of the method being

tested State and the test represent the

input values for the method expected

behavior is what the method returns for

the specified input let's understand

this with an example let's rename this

test underscore divide method to stick

to Roy ourselves naming strategy

according to Ryan or shrubs naming

strategy the first part of the unit

tests name is the unit of work which is

nothing but the name of the method that

we are testing here we are testing the

divide method so unit of work is divide

followed by that underscore and then

state and a test which represents the

input values for the method that we are

testing so if you look at this divide

method right here it has got two input

parameters numerator and denominator and

if you look at the values for those two

parameters both of them are positive

numbers so divide underscore positive

numbers so those are the input parameter

values and the final part of the unit

test name is the expected behavior and

what do you think is going to happen

when we divide two positive numbers we

are going to get positive quotient so

the expected behavior is returns

positive quotient

let's create another unit test let's

actually make a copy of this now let's

divide a positive and a negative number

when we do that we are going to get a

negative quotient so divide positive

numerator and negative denominator what

are we going to get back we are going to

get back negative quotient so the

expected

here is returns negative quotient let's

create one more unit test so let's make

a copy of this method when we divide two

negative numbers what are we going to

get back we are going to get back

positive quotient so divide on the score

negative numbers underscore returns

positive quotient so here we are going

to divide two negative numbers so

numerator is going to be negative

denominator is also going to be negative

we get a positive quotient this unit

test we are passing positive numerator

and negative denominator so numerator is

going to be negative sorry denominator

is going to be negative numerator is

positive and what are we expecting back

we are getting a negative quotient all

right

so now within this calculator test class

we have got three unit tests but look at

the test explorer window we only have

one unit test here for this list to be

updated we will have to build a solution

I'm going to use the keyboard shortcut

control shift B to build a solution as

soon as the build succeeded notice we

have the list updated to run all these

unit tests I'm going to click on this

link run all by just going to run all

the three unit tests so that's the

naming strategy for unit tests so if you

have a method like divide and if you're

dividing two positive numbers you might

have a method name like this divided

underscore positive numbers underscore

returns positive quotient now let's look

at the naming strategy for the test

class so the class that contains our

divide method is calculator so the test

class is usually calculated tests so the

suffix that word test to the same class

name so if you look at the calculator

class here this is present in calculator

dot cs5 and if you look at our unit

testing project the file name is

calculated test notice I have not

pluralized

word test some people like to pluralize

it some people doesn't let's actually

PluralEyes it

so let's rename the file when we rename

the file and when I hit enter look at

this it's prompting me to rename the

class name as well I'm going to click s

so the class name is also calculated

test which makes it makes more sense so

that's the naming strategy for test

classes similar naming strategy for test

projects so if the project name is

calculated library your test project

will be calculator dot library dot tests

so look at the project right here the

project that contains our calculator

class is calculated or to library but

look at the unit test project name I

named it calculator dot test it would

have been more meaningful if I have

named it like calculator dot library dot

tests now since we named the project to

calculate our library test it also makes

more sense to change the namespace name

as well so let's change it to calculator

dot library test the moment we do that

look at that we have caught a

compliation error that's because we are

already within calculator dot the

library name space within which we have

that calculator class so I can simply

remove this calculator dot library from

all the three unit test names and that

should get rid of those compilation

errors let's a build of solution to make

sure the solution still complies and

let's run the unit tests and all of them

succeeds

you