Click here to Skip to main content
15,891,033 members
Home / Discussions / C#
   

C#

 
AnswerRe: How do I unit test this? Pin
Keith Barrow14-Feb-10 4:55
professionalKeith Barrow14-Feb-10 4:55 
AnswerRe: How do I unit test this? Pin
Luc Pattyn14-Feb-10 5:03
sitebuilderLuc Pattyn14-Feb-10 5:03 
GeneralRe: How do I unit test this? [modified] Pin
Keith Barrow14-Feb-10 5:54
professionalKeith Barrow14-Feb-10 5:54 
AnswerRe: How do I unit test this? Pin
Not Active14-Feb-10 4:57
mentorNot Active14-Feb-10 4:57 
AnswerRe: How do I unit test this? Pin
Luc Pattyn14-Feb-10 5:00
sitebuilderLuc Pattyn14-Feb-10 5:00 
GeneralRe: How do I unit test this? Pin
Member 448708315-Feb-10 6:50
Member 448708315-Feb-10 6:50 
GeneralRe: How do I unit test this? Pin
Luc Pattyn15-Feb-10 11:44
sitebuilderLuc Pattyn15-Feb-10 11:44 
AnswerRe: How do I unit test this? [modified] Pin
Jimmanuel14-Feb-10 11:34
Jimmanuel14-Feb-10 11:34 
I would never change a private method to public or anything else for testing. I'd always test it through calls to the public[1] members of the class because that's the way that they were designed to be used. If you make them public and test them directly then you have access to them in ways that the other classes and modules don't and you can stress them in ways that won't be possible once deployed. When unit testing a class I access it only in ways it was designed to be used and if code private that means that it was not designed to be used by anything other than the other code in the class.

Take the following class:
public class TestClass
{
	private void PrivateMethod(bool someBool)
	{
		if (someBool)
		{
			// do something
			Console.WriteLine("Reachable Code");
		}
		else
		{
			// do something else
			Console.WriteLine("Dead Code");
		}
	}
	
	public void PublicMethod(double x)
	{
		// do some stuff
		
		// Here we're going to do some calculation with the input and use the result to determine what to pass 
		// PrivateMethod(...).   I'm using Math.Abs just as an example.
		double calculationResult = Math.Abs(x);  
		
		PrivateMethod (calculationResult >= 0.0);
		
		// do some more stuff
	}
}


The private method has different behavior based on its parameters. If you make that public and test it directly then you can stress both cases but that's not how the code was designed! If you examine the code you'll see that the only place it's called from is from the one public method and that method will always pass it a value of true. What this means is that private method doesn't need to have different behaviors, it doesn't need to check its input and it doesn't even need any input parameters. The parameter, if statement and else block are all dead code and should be removed. At least this is what it looks like at first glance; what if the fact that the method can only be passed one value a bug? What if the calculation is wrong? If you make the private method public just for the sake of testing then this question might never be asked. Granted, this is a contrived example that shows a minor inefficiency but it highlights how easy it is to hide bugs private code if you tweak for testing.

If you have a class where everything is private then what's the point of the class? If you have private code that isn't callable in one way or another from any public methods then what's the point of the code? If code can't be executed then it shouldn't exist, otherwise it should only be accessed in the manner in which it was designed to be accessed whether it's in testing or deployment.

In the test class above I would test the private method by writing test cases that enter whatever public methods are required to fully exercise as much of the private method as possible.

Of course, that's just my methodology. YMMV. Smile | :)


[1] "public" in this post is used as shorthand for any accessibility level that allows the method to be called from another class regardless of whether that class is internal to the assembly, an inherited child or using public attributes.

[EDIT] corrected example code
Badger | [badger,badger,badger,badger...]
modified on Monday, February 15, 2010 7:06 AM

GeneralRe: How do I unit test this? Pin
Member 448708315-Feb-10 6:48
Member 448708315-Feb-10 6:48 
GeneralRe: How do I unit test this? Pin
Jimmanuel15-Feb-10 8:46
Jimmanuel15-Feb-10 8:46 
QuestionCrash inside InitializeComponent() Pin
SimpleData14-Feb-10 0:24
SimpleData14-Feb-10 0:24 
AnswerRe: Crash inside InitializeComponent() Pin
Abhinav S14-Feb-10 0:30
Abhinav S14-Feb-10 0:30 
GeneralRe: Crash inside InitializeComponent() Pin
SimpleData14-Feb-10 0:34
SimpleData14-Feb-10 0:34 
GeneralRe: Crash inside InitializeComponent() Pin
Abhinav S14-Feb-10 0:47
Abhinav S14-Feb-10 0:47 
GeneralRe: Crash inside InitializeComponent() Pin
SimpleData14-Feb-10 1:42
SimpleData14-Feb-10 1:42 
AnswerRe: Crash inside InitializeComponent() Pin
Luc Pattyn14-Feb-10 0:52
sitebuilderLuc Pattyn14-Feb-10 0:52 
GeneralRe: Crash inside InitializeComponent() Pin
SimpleData14-Feb-10 1:42
SimpleData14-Feb-10 1:42 
QuestionMy .net application stops suddenly , want to know why ? Pin
Dr.DigiTaL13-Feb-10 21:51
Dr.DigiTaL13-Feb-10 21:51 
AnswerRe: My .net application stops suddenly , want to know why ? Pin
Abhinav S13-Feb-10 22:13
Abhinav S13-Feb-10 22:13 
Questionconvert image Pin
reza kia13-Feb-10 20:41
professionalreza kia13-Feb-10 20:41 
AnswerRe: convert image Pin
dan!sh 13-Feb-10 20:51
professional dan!sh 13-Feb-10 20:51 
GeneralRe: convert image Pin
reza kia14-Feb-10 1:34
professionalreza kia14-Feb-10 1:34 
GeneralRe: convert image Pin
dan!sh 14-Feb-10 3:59
professional dan!sh 14-Feb-10 3:59 
GeneralRe: convert image Pin
reza kia14-Feb-10 21:06
professionalreza kia14-Feb-10 21:06 
QuestionHow to read an image from an .xml file can we insert an image in .xml file Pin
koganti.pardhasaradhi13-Feb-10 19:26
koganti.pardhasaradhi13-Feb-10 19:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.