Rok Povsic
Rok Povsic

Reputation: 4935

Testing (not unit testing) private method correctness

Let's say I write a private method which has a string as an argument and returns a string.

private string SomeMethod(string a)
{ ... }

After writing it, I want to make sure this method works correctly and input a sample string to see what the return value is. Now I test it the following way. I either:

I don't like either method. Is there any better way to simply check and test the method while developing code? I can't (shouldn't) write a unit test for this method as it's private.

Upvotes: 0

Views: 77

Answers (3)

FallenSquirrel
FallenSquirrel

Reputation: 197

If you just want to test it once after creating it, I would recommend to keep another instance of your Development Environment (e.g. Visual Studio) open, with a simple console application where you can just copy-paste the function and test it there.

If you want it regularly tested to ensure it doesn't go wrong with later changes, then I fear Unit testing is exactly what you need. You might use precompiler flags to disable the private scope for testing purposes:

#define TESTING
(...)
#if TESTING
        public void foo()
#else
        private void foo()
#endif

Upvotes: 1

Lawrence
Lawrence

Reputation: 3297

Put the logic in a public method, which you unit test while your are developing. When you are satisfied you have got the logic correct, you can copy and paste the logic in to your private method.

Upvotes: 0

Jacob Pollack
Jacob Pollack

Reputation: 3761

This is the academia approach for all first year classes at the University of Waterloo, I imagine a similar approach is taken in the real world (my first co-op is 2014 so I don't know just yet). I find it very useful but it takes some time.

If you create a class (henceforth mentioned as a module) then you need you create an associative driver module for each class. This driver file will consist of all your tests: testing edge cases and expected input to ensure your function(s) from your module behave as expected. Each driver module will have a main method and for your testing you will need to change the scope of your function from private to public (or call them all in the constructor).

Keep in mind we are programming in C and driver modules are much easier to make without changing scope (the occasional static for modular scope).

Upvotes: 0

Related Questions