How to test the private private/internal method

Source: Internet
Author: User
Tags assert instance method

In actual development, this situation is often encountered.

A common public method implements a major function, but because the implementation of this feature is very complex, it requires a lot of helper classes and helper methods. Because of the need for code encapsulation, we typically define these helper class methods as Non-public, static (not required, but static methods improve performance), such as private, internal, and so on.

But it also poses a problem, how do you measure these non public classes, which are the code to complete the logic?

As a developer, if let me say, there are several ways to do this:

The modifier is public and the test is done before it is modified-but this destroys the meaning of unit Test because it cannot be run under any circumstances.

Reflection-write the reflected code, dynamically invoke the corresponding method. But it would be a waste to write so many tool codes for a unit test.

Use the Internvalvisibleto property--[internalsvisibleto ("unittestproject.assembly")] to set the current project to be visible to the unit test, all private methods using the Internal limit, But will this break the package?

Has Microsoft not considered the issue?

Of course, the answer is privateobject/privatetype-actually used my second way, but did a lot of Microsoft to deal with Oh, save us trouble.

More Wonderful content: http://www.bianceng.cn

Privateobject the corresponding test is an instance method, Privatetype corresponding test is static method.

Suppose we have a Calculate class as follows:

public class Calculate
    internal static int addstatic (int a, int b)
        a + b;
    private int Add (int a, int b)
        a + b;

And then we're going to test it.

Then the Add method for the instance tests the code as follows

      public void Testprivateadd ()
          privateobject po = new Privateobject (new Calculate ());
          Assert.AreEqual (PO. Invoke ("Add", 1, 2), 3);

The addstatic code for testing static Internal is as follows:

      public void Testinternalstaticadd ()
          Privatetype po = new Privatetype (typeof (Calculate));
          Assert.AreEqual (PO. Invokestatic ("Addstatic", 1, 2), 3);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.