Loading...
General

Unit tests and private method & properties

Controversial subject? Excellent.

Believe it or not, I’ve had situations which warrant private method / property testing. I believe the classic argument against this is “A private method is an internal detail of a class and subject to change, therefor it is discouraged”. Agreed. It is all that, but there are indeed situations where we would be needing this.

As a contractor, I have to deal with existing code. Often times this code lacks a test harness altogether, thousands of lines of code in one file. You’ve got 2 strategies here, 1 – make a change push to prod and pray it works ORRRR… Test what you can around the changes introduced and have the confidence you need.

I am of the opinion that testing is done for the person to gain confidence, not because “Team X standards dictate that we must have 80% code coverage”. Nothing against code coverage, but understand why you need testing, its to do with your own confidence levels.

In a situation where you’ve got limited time and scope, existing code is spaghetti – you have to make the best with what you’ve been given. At this stage, do whatever you have to to get the functionality tested absolutely. A good way to do this is to isolate your changes using the spout method, then on this piece apply automated tests.

Leave a Reply