How to Unit Test Private Methods in MS Test
Should you test private methods in MS Test, and if so, how do you perform them? Here is a step-by-step guide.
Join the DZone community and get the full member experience.
Join For Freethis article originally appeared on the infragistics blog by dhananjay kumar
before we show how a private method can be unit tested using the ms test, let us discuss whether it is a good idea to test a private method or not. often i have seen there are two schools of thought:
1. private methods should be tested.
2. private methods should not be tested.
to put these things in perspective, let us consider a system under test (sut) salary class as shown in the listing below.
namespace calculator
{
public class salary
{
public int calculatesal(int bs, int nwd)
{
int ts ;
if(isvalidnwd(nwd))
{
ts= bs*nwd;
}
else
{
ts = 1000;
}
return ts;
}
private bool isvalidnwd(int nwd)
{
if (nwd > 8)
{
return true;
}
else
{
return false;
}
}
}
}
a system under a test salary class has two functions:
1. the calculatesal method is a public method and it takes two parameters to calculate the salary.
2. the isvalidwd method is a private method and it takes one parameter. this function returns true if the number of working days are more than 8, or else it returns false.
3. the calculatesal method first checks whether the number of working days is valid or not using the private method isvalidwd.
4. if the number of working days is valid, then salary is calculated by the multiplication of basic salary and the number of working days, or else it is fixed at $1000.
now we have a system under test salary class with two functions.
one opinion is that the private method should not be unit tested separately. this is because the private method is used inside the public method and when we test the behavior of the public method, the behavior of the private method also gets tested.
another opinion is that the private method must be unit tested for its own behavior in isolation with the other public or private methods. before we go ahead and see how to test a private method, let us give some thought on whether there is another possible way to work around this.
violating single responsibility principle
currently, the system under test salary class is violating the single responsibility principle because it has two responsibilities:
-
to calculate the salary (public method).
-
to validate the number of working days (private method).
we can’t be sure of good code coverage of sut salary class unless we test the validate working days behavior of the class, and is private by design. a good option could be this: while creating the salary class, we can spilt the responsibilities in two separate classes with public methods to calculate the salary and validate working days. we can then write unit tests for public methods of both classes. let’s see how to do that:
writing unit test for private methods
if you’re of the opinion that a private method in the sut class should be tested, then you have two options:
1. use refactoring – but this is bit complex.
2. use vsts privateobject class - this is simple!
let’s see how we can use a privateobject class to unit test a private method. to use a private object class you need to:
1. add a reference of microsoft.visualstudio.qualitytools.unittestframework in the test project. if you have created project by selecting unit test project templet then this reference would be added by default in the project.
2. add a namespace microsoft.visualstudio.testtools.unittesting.
the constructor of privateobjectclass takes the type as the parameter, so here you’ll need to pass type of the sut salary class and then on the object of privateobjectclass call the invoke method to invoke the private method.
we can test the private method isvalidnwd as shown in the listing below:
[testmethod]
public void returntrueforvalidworkingdays()
{
privateobject objtotestprivatemethod = new privateobject(typeof(salary));
bool result = convert.toboolean(objtotestprivatemethod.invoke("isvalidnwd", 6));
assert.areequal(result, true);
}
essentially, we are performing the following tasks:
-
creating an object of privateobject class.
-
passing a type of salary class as an input parameter in the constructor.
-
using the invoke method to execute the private method of the sut salary class.
-
passing two parameters in the invoke method: the first parameter is the name of the private method and the second parameter is the argument which will be passed to the private method while executing.
and that’s how we can unit test a private method!
conclusion
in this post we learnt about:
-
whether to test a private method or not.
-
how to test the private method using the privateobject class.
i hope this post is useful for you - thanks for reading. happy coding!
Published at DZone with permission of Josh Anderson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments