Click here to Skip to main content
13,144,958 members (51,323 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

335.3K views
1.9K downloads
136 bookmarked
Posted 1 Mar 2005

How to Test Private and Protected methods in .NET

, 1 Mar 2005
This article explains some theory behind testing/not testing private methods, and then provides and walks through a downloadable code sample to demonstrate these testing techniques.
UnitTestDemo
ClassLibrary1
bin
Release
ClassLibrary1.dll
UnitTests
App.ico
bin
Release
ClassLibrary1.dll
UnitTests.exe
UnitTestUtilities.dll
nunit.framework.dll
UnitTestUtilities
bin
Release
UnitTestUtilities.dll
using System;
using NUnit.Framework;
using ClassLibrary1;

namespace UnitTests
{
	/// <summary>
	/// Summary description for MyTests.
	/// </summary>
	[TestFixture] public class MyTests
	{
		public MyTests()
		{

		}

		[Test] public void TestPublicMethod() 
		{
			string strExpected = "MyName: Value";
			ClassLibrary1.MyObject c = new ClassLibrary1.MyObject("MyName");
			string strActual = c.MyPublicMethod("Value");
			Assert.AreEqual(strExpected,strActual);
		}

		[Test] public void TestProtectedMethod() 
		{
			string strExpected = "MyName: Value, 23";
			MyObjectTester c = new MyObjectTester("MyName");
			string strActual = c.TestMyProtectedMethod("Value",23);
			Assert.AreEqual(strExpected,strActual);
		}

		[Test] public void TestPrivateStaticMethod() 
		{
			string strExpected = "Hello, 2.1";

			object obj = UnitTestUtilities.Helper.RunStaticMethod(
				typeof(ClassLibrary1.MyObject),
				"MyPrivateStaticMethod",
				new object[2] {"Hello",2.1});
			string strActual = Convert.ToString(obj);

			Assert.AreEqual(strExpected,strActual);

		}

		[Test] public void TestPrivateInstanceMethod() 
		{
			string strExpected = "MyName: Hello, 5/24/2004 12:00:00 AM, 2.1";

			ClassLibrary1.MyObject objInstance = new MyObject("MyName");
			object obj = UnitTestUtilities.Helper.RunInstanceMethod(
				typeof(ClassLibrary1.MyObject),
				"MyPrivateMethod",
				objInstance,
				new object[3] {"Hello", new DateTime(2004,05,24), 2.1});
			string strActual = Convert.ToString(obj);

			Assert.AreEqual(strExpected,strActual);

		}

	} //end of class

} //end of namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

TimStall
Web Developer
United States United States
Tim Stall is a Chicago-based technical consultant for Computer Sciences Corporation (www.csc.com), a leading global IT services company. In addition to his expertise in Microsoft.Net development projects and enterprise architecture, Tim's .Net experience includes, writing technical publications, leading internal training, and having MCAD certification. His blog is at http://timstall.dotnetdevelopersjournal.com/.

You may also be interested in...

Pro
Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 1 Mar 2005
Article Copyright 2005 by TimStall
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid