1

I know it may sound like a silly question as I've heard many saying that you should only do unit testing for public function and indeed, visual basic.net in Visual studio IDE only allows me to do unit testing for public function.

However, I got a public function that is calling to many other private functions. If I do unit testing for that public function, that would be too complicated. I only want to test each private function individually to make sure it works correctly first, before jumping to the parent public function.

One solution I've had in my mind is that I could change all private functions to public ones so that Visual Studio allows me to do unit testing for them. But it is annoying me as I don't want them to be public.

Is there any suggestions you could let me know please?

many thanks in advance.

N.T.C

N.T.C
  • 658
  • 2
  • 9
  • 20
  • "If I do unit testing for that public function, that would be too complicated" - That suggests your function might be too complicated. – Mitch Wheat May 11 '13 at 08:40
  • Yes It is an engineering application and the function involves quite a bit of mathematical calculations. That public function is calling many other smaller and more simple private functions - and those functions are the one I want to do the unit testing. But because they are set as private function, the IDE unittesting doesn't allow me to test them - Is there a way of doing unit testing them without changing them to Public ? – N.T.C May 11 '13 at 09:01
  • 2
    http://stackoverflow.com/questions/250692/how-do-you-unit-test-private-methods : "1.If the method you'd like to test is really worth testing, it may be worth to move it into its own class." – Mitch Wheat May 11 '13 at 09:02
  • But @MitchWheat are you implying that we should do unit testing for complicated functions only ? – N.T.C May 11 '13 at 13:37
  • 1
    No! I'm suggesting breaking logic into separate testable classes.... – Mitch Wheat May 11 '13 at 14:14

2 Answers2

1

If you really can't break the code out into separate classes, you could change all of the private functions to be protected and then create a private class within your test class that inherits from the class you're trying to test (this would be named as a fake or stub, hence my advice to make it private. You don't want code outside of the test class to interact with this). Within your inherited class, create public functions for each of the now protected functions that simply call through to the base and write your unit tests against those instead.

levelnis
  • 7,665
  • 6
  • 37
  • 61
  • I gotta say that what you proposed is a really smart idea!!! However, by passing that issue, you obviously created another one here - "protected method". If we want to follow TDD, then by doing this way, the class will consist of protected and public methods. What if I really want to keep some private methods - which i don't want them to be available to the inherited classes? – N.T.C May 12 '13 at 13:49
  • But surely they would be called by one of the protected methods at the very least? The protected methods, I assume, are well-defined parts of a larger process and if they have functionality refactored into private methods, your tests will simply be testing the overall behaviour of each protected method. I'm not suggesting that you make all of the private methods protected, just the key ones – levelnis May 12 '13 at 13:53
  • thank you levelnis, your suggestion is very helpful. I will give it a try at work tomorrow :) – N.T.C May 12 '13 at 14:29
  • Can you please tell me how to accept an answer, by the way? I am pretty newbie here (and not coming from IT background either) – N.T.C May 12 '13 at 14:46
0

I apologize if this capability is not available in VB:

Create a sub-class of the class you want to test. Ensure that the sub-class has public interfaces to the private functions.

As for "only unit test public functions?" That's horse manure. You test what might fail. For instance, you might have a class with only one public function, and you want to refactor into a set of calls on private functions to decrease the complexity. If you have to refactor your solution for any reason (as one of the comments suggested), then the first step is to have all the pieces of the solution tested that you will have to change during the refactoring.

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42
  • Before the refactoring, there are no private methods so there can be no tests for them. Afterwards, the behaviour hasn't changed so the tests don't add anything. – fgb May 12 '13 at 13:15
  • Hi BobbyD, if I create an inherited class, then I simply can't make a public interface that is related to the parent class private functions. The only way to do it (in my knowledge) in VB.NET is to change them to protected functions as mentioned by @levelnis above. As for your second point as to start the class with only 1 public function , test it, then re-factor it into small functions, this approach wouldn't work for a complex function. I don't think I am able to write about 1000lines of code to find the final result first, and then refactoring the code to, says, 10 smaller functions. – N.T.C May 12 '13 at 14:11
  • Good point about protected - my bad. As for refactoring, I am used to Eclipse platform, which has a number of tools that can reliably refactor Java code. I strongly recommend Martin Fowler's material: http://www.refactoring.com/ – Bob Dalgleish May 12 '13 at 17:40