How to Unit Test Private Methods in MS Test

Before we start to see, 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 school of thoughts,

  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.

image

A system under 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.

Read full article on the Infragistics blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com