You probably shouldn't (see other answers why), but you can do it using Microsoft Visual Studio Test Tools . The following is a simplified example.
Given the following class you want to test:
public class ClassToTest { private int Duplicate(int n) { return n*2; } }
You can use the following code to test the private Duplicate method:
using Microsoft.VisualStudio.TestTools.UnitTesting; // ... [TestMethod] public void MyTestMethod() { // Arrange var testClass = new ClassToTest(); var privateObject = new PrivateObject(testClass); // Act var output = (int) privateObject.Invoke("Duplicate", 21); // Assert Assert.AreEqual(42, output); }
Grinn source share