How to: Test a Private Method

You can test both public and private methods using unit tests. As with public methods, unit tests for private methods are created automatically when you generate tests from the code you want to test.

Although you can manually code unit tests to test any method, this task is more difficult for private than for public methods because it requires a better understanding of the intricacies of reflection. You will therefore find it better to generate tests for private methods instead of coding them by hand.

When you generate a unit test for a private method, Visual Studio creates a private accessor. A private accessor is an assembly through which the test can access a private method from outside the class of that method. For more information, see Unit Tests for Internal, Private, and Friend Methods.

Note

In addition to private methods, you can also use a private accessor to test private properties and fields. However, you cannot use a private accessor to test private versions of the following: events, delegates, anonymous methods, and members marked with the CompilerGeneratedAttribute attribute.

InternalsVisibleTo or a Private Accessor: Which to Use?

You can use a private accessor assembly to access both private members and internal members. If your code-under-test has internal members but no private members, it is best to use the InternalVisibleTo attribute on your code-under-test and forego the creation of a private accessor assembly. But if your code has both private and internal members that you want to test, use the private accessor, because it provides access to both. For more information, see Unit Tests for Internal, Private, and Friend Methods.

To generate a unit test for a private method

  1. Open a source code file that contains a private method.

  2. Right-click the private method, and select Create Unit Tests.

    This displays the Create Unit Tests dialog box. In the visible tree structure, only check box for the private method is selected.

  3. (Optional) In the Create Unit Tests dialog box, you can change the Output project. You can also click Settings to reconfigure the way unit tests are generated.

  4. Click OK.

    This generates the code for accessing the private method, builds the test project, and creates or updates the private accessor assembly.

    If your test project had no unit tests before this point, a source code file to house unit tests is created. As with the file that contains private accessors, the file that contains unit tests is also visible in your test project in Solution Explorer.

  5. Open the file that contains your unit tests and scroll to the test for the private method. Find the statements that are marked with // TODO: comments and complete them by following the directions in the comments. This helps the test produce more accurate results. For more information, see How to: Author a Unit Test.

    The unit test for the private method is now ready to run. For more information, see How to: Run Selected Tests.

See Also

Tasks

How to: Author a Unit Test

Walkthrough: Creating and Running Unit Tests

Concepts

Unit Tests for Internal, Private, and Friend Methods

How to: Create and Run a Unit Test

Using Publicize to Create Private Accessors

Setting the InternalsVisibleTo Attribute