Unit Tests for Private, Internal, and Friend Methods
Typically, methods marked as private, internal, and friend are not visible from outside the class that contains those methods. However, the Visual Studio tools provide unit tests with the ability to access them, even though the unit test file is a separate class.
Although you do not need to edit your unit test file to allow for these methods, there are some choices that you can make about how internal and friend methods are treated. This article explains those choices and describes what occurs in your project when you test private, internal, and friend methods.
For instructions about how to create unit tests see Creating and Running Unit Tests for Existing Code.
The use of Accessors has been deprecated in Visual Studio 2010 and might not be included in future versions of Visual Studio.
When you create a unit test for a private method, a Test References folder is added to your test project and an accessor is added to that folder. The accessor is also referred to in the logic of the unit test method.
Form1_Accessor target = new Form1_Accessor(); // TODO: Initialize to an appropriate value
Dim target As Form1_Accessor = New Form1_Accessor() ' TODO: Initialize to an appropriate value
This accessor allows your unit test to call private methods in the code that you are testing.
You can manually create an accessor or re-create one that has been deleted. To do this, right-click the code that you are testing, point to Create Private Accessor, and select the test project that requires the accessor.
Internal or Friend Methods
When you create a unit test for an internal method in C# or for a friend method in Microsoft Visual Basic, a dialog box appears that allows you to choose between having your internal methods accessed with the private accessor or with the InternalsVisibleToAttribute.
The dialog box might not appear if the attribute has already been added to your project.
If you choose to have your internal methods accessed with the InternalsVisibleToAttribute, the attribute is added to the AssemblyInfo.cs file. The attribute makes the internal methods in the code that you are testing available to the test project. A new attribute is added for each test project for which you choose this option.
If you choose to have your internal methods accessed with the private accessor, you see the same behavior as described earlier in this article for private methods: a Test References folder is added to your test project, an accessor is added to that folder, and the accessor is referred to in the logic of the unit test method. In this case, the private accessor provides access to both the internal and the private methods that you are testing.
If you have chosen to add the InternalsVisibleToAttribute to your project, you can still create unit tests for internal methods and have them accessed with the private accessor instead. However, you have to do this when you create the unit test. In the Create Unit Test dialog box, click Settings. In the Test Generation Settings dialog box, clear the Honor InternalsVisibleTo Attribute check box.