views:

117

answers:

3

more questions after questions in here: http://stackoverflow.com/questions/2714073/c-unit-test-code-questions

I found the VS unit test testframe treat private and protected method in the same way but deferent from public method.

The following is the generated code for a private method:

        /// <summary>
        ///A test for recordLogin
        ///</summary>
        [TestMethod()]
        [DeploymentItem("SystemSoftware.exe")]
        public void recordLoginTest()
        {
            User_Accessor target = new User_Accessor(); // TODO: Initialize to an appropriate value
            Guid userId = new Guid(); // TODO: Initialize to an appropriate value
            string action = string.Empty; // TODO: Initialize to an appropriate value
            Users user = null; // TODO: Initialize to an appropriate value
            AndeDBEntities db = null; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.recordLogin(userId, action, user, db);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

questions:

  1. [DeploymentItem("SystemSoftware.exe")] is for private and protected methods, why needs it and what is it for?

  2. In my original class/file, if I point to the original method and try to "Find All References". The reference in the unit test class/file will not show up for private and protected methods but it will show up for all public methods. Why is that? Is it right?

+1  A: 

[DeploymentItem("SystemSoftware.exe")] is for private and protected methods, why needs it and what is it for?

It defines (file) resources that test needs (you can apply to the test class or individual methods). Since test methods must be public I cannot see why you would apply this to a private or protected method.

The attribute is documented: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute.aspx

Richard
+1  A: 

Find All References won't find those tests because they use an accessor class (User_Accessor) instead of the real class (User) to access protected and private methods. The accessor class is automatically generated and does some tricks to expose those normally not accessable methods.

gix
+1  A: 

[DeploymentItem("SystemSoftware.exe")] is for private and protected methods, why needs it and what is it for?

You can't access private, protected or internal members from a unit test which is in a different assembly and doesn't inherit from the class you're trying to test (nor would it be possible if your "unit" to be tested is more than a single class). To have access to private, protected or internal members, the MSTest framework will generate an accessor assembly that gives you proxies to access these otherwise hidden members.

The DeploymentItemAttribute signals the test runner which artifacts (and dependencies such as accessor assemblies or test data files) need to be deployed so the code can be properly execute. In essence it implicitly tells the MSTest framework to generate and deploy an accessor assembly in this case.

In my original class/file, if I point to the original method and try to "Find All References". The reference in the unit test class/file will not show up for private and protected methods but it will show up for all public methods. Why is that? Is it right?

See above, you're not directly accessing them but use a proxy to do so. This proxy uses runtime reflection to bind your call, so this can't be traced inside Visual Studio.

Johannes Rudolph