How do you unit test private methods?

前端 未结 30 1331
无人及你
无人及你 2020-11-22 06:44

I\'m building a class library that will have some public & private methods. I want to be able to unit test the private methods (mostly while developing, but also it coul

相关标签:
30条回答
  • 2020-11-22 07:23

    If you want to unit test a private method, something may be wrong. Unit tests are (generally speaking) meant to test the interface of a class, meaning its public (and protected) methods. You can of course "hack" a solution to this (even if just by making the methods public), but you may also want to consider:

    1. If the method you'd like to test is really worth testing, it may be worth to move it into its own class.
    2. Add more tests to the public methods that call the private method, testing the private method's functionality. (As the commentators indicated, you should only do this if these private methods's functionality is really a part in with the public interface. If they actually perform functions that are hidden from the user (i.e. the unit test), this is probably bad).
    0 讨论(0)
  • 2020-11-22 07:23
    CC -Dprivate=public
    

    "CC" is the command line compiler on the system I use. -Dfoo=bar does the equivalent of #define foo bar. So, this compilation option effectively changes all private stuff to public.

    0 讨论(0)
  • 2020-11-22 07:24

    You could also declare it as public or internal (with InternalsVisibleToAttribute) while building in debug-Mode:

        /// <summary>
        /// This Method is private.
        /// </summary>
    #if DEBUG
        public
    #else
        private
    #endif
        static string MyPrivateMethod()
        {
            return "false";
        }
    

    It bloats the code, but it will be private in a release build.

    0 讨论(0)
  • 2020-11-22 07:25

    On CodeProject, there is an article that briefly discusses pros and cons of testing private methods. It then provides some reflection code to access private methods (similar to the code Marcus provides above.) The only issue I've found with the sample is that the code doesn't take into account overloaded methods.

    You can find the article here:

    http://www.codeproject.com/KB/cs/testnonpublicmembers.aspx

    0 讨论(0)
  • 2020-11-22 07:27

    Well you can unit test private method in two ways

    1. you can create instance of PrivateObject class the syntax is as follows

      PrivateObject obj= new PrivateObject(PrivateClass);
      //now with this obj you can call the private method of PrivateCalss.
      obj.PrivateMethod("Parameters");
      
    2. You can use reflection.

      PrivateClass obj = new PrivateClass(); // Class containing private obj
      Type t = typeof(PrivateClass); 
      var x = t.InvokeMember("PrivateFunc", 
          BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public |  
              BindingFlags.Instance, null, obj, new object[] { 5 });
      
    0 讨论(0)
  • 2020-11-22 07:27

    I use PrivateObject class. But as mentioned previously better to avoid testing private methods.

    Class target = new Class();
    PrivateObject obj = new PrivateObject(target);
    var retVal = obj.Invoke("PrivateMethod");
    Assert.AreEqual(retVal);
    
    0 讨论(0)
提交回复
热议问题