Are you explicitly unit test of a private method when you use your knowledge of a private method to select test cases

It seems that the general consensus of the testing community is not to test private methods. Instead, you should test private methods by testing the public methods that call them . However, I do not like something. Take this method, for example:

/**
 * Returns the base name of the output generator class. If the class is named
 * Reno_OutputGenerator_HTML, this would return "HTML".
 *
 * @return string
 */
protected function getName()
{
    $class = get_class($this);
    $matches = array();

    if (preg_match('/^Reno_OutputGenerator_(.+)$', $class, $matches))
    {
        return $matches[1];
    }
    else
    {
        throw new Reno_OutputGenerator_Exception('Class name must follow the format of Reno_OutputGenerator_<name>.');
    }
}

This particular function is used in several places in my class. I would like to test both operator branches ifin this function, which would mean for every public function that I would have to test in these two situations, plus everything that the public method itself does.

, . , , getName() , , , . , , ?

(BTW: , , , ).

+3
2

, , - , . white-box testing; , , . , , - , 32 .

, , , , , , .

, , , . , - , , , , ?

, - ( ). , - . , , , , , .

+2

, , :

namespace TheProject
{
    public class ClassUnderTest
    {
        protected string GetName()
        {
            return "The name";
        }
    }
}

namespace TestProject
{
    [TestClass]
    public class TheTest:TheProject.ClassUnderTest
    {
        [TestMethod]
        public void TestGetName()
        {
            string expected = "The name";
            string actual = GetName();
            Assert.AreEqual(expected, actual);
        }
    }
}

, , .

0

All Articles