Geoffroy’s Weblog

November 25, 2007

Unit testing with VSTS 2008 (Orcas Beta 2)

Filed under: Unit Testing, VSTS 2008 — gseive @ 11:32 pm

I was looking for a tutorial on how to do unit testing with VSTS 2008. Since I am developing an ASP.NET application I first found and read the topic “How to: Create an ASP.NET Unit Test” at http://msdn2.microsoft.com/en-us/library/ms182526(VS.90).aspx, but I couldn’t get it to work! After some more searching I found something else that looked promising, “Walkthrough: Creating and Running Unit Tests”, on MSDN again at http://msdn2.microsoft.com/en-us/library/ms182532(VS.90).aspx.

The sections on testing the public methods went well (light discrepancies between the text in the article and the code provided in the zip file though, TeamTest Samples.zip). Things started breaking down with the section on testing private methods.

The text in the article looks like this:

Within the GetAccountTypeTest() method, find the three assignment statements that are marked with // TODO: comments. Change them to read as follows:

string customerName = "Mr. Bryan Walton";
double balance = 7.00;
TestProject1.BankAccountNS_BankAccount_accountTypeAccessor val = TestProject1.BankAccountNS_BankAccount_accountTypeAccessor.Gold;

Run the GetAccountTypeTest test.
In the Result column in the Test Results window, the final test status appears as Passed. This is the expected result because with the account balance set to 7.00, the accountType should be Gold.

Now the test code generated from the code in the zip file looks like this:

        [TestMethod()]
        [DeploymentItem("Bank.dll")]
        public void GetAccountTypeTest()
        {
            PrivateObject param0 = null; // TODO: Initialize to an appropriate value
            BankAccount_Accessor target = new BankAccount_Accessor(param0); // TODO: Initialize to an appropriate value
            BankAccount_Accessor.accountType actual;
            actual = target.GetAccountType;
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

Final test code for private GetAccountType()

Updating the code wasn’t obvious. But after some trial and error I got the test method to work with the following code:

        [TestMethod()]
        [DeploymentItem("Bank.dll")]
        public void GetAccountTypeTest()
        {
            PrivateObject param0 = null; // TODO: Initialize to an appropriate value - Actually not used
            string customerName = "Mr. Bryan Walton";
            double balance = 7.00;
            BankAccount_Accessor target = new BankAccount_Accessor(customerName, balance); // Now initialized to an appropriate value!
            BankAccount_Accessor.accountType actual;
            actual = target.GetAccountType;
            //Assert.Inconclusive("Verify the correctness of this test method.");
            Assert.AreEqual(BankAccount_Accessor.accountType.Gold, actual); // Expecting Gold, are we getting Gold?
        }

I haven’t used the PrivateObject type and haven’t looked into what those are yet.

Next step is to apply my new discoveries to my project and see if it works there!

1 Comment »

  1. Wow, thanks a lot. I was struggling with this 😛
    It seems like BankAccount_Accessor is some kind of representative for BankAccount.
    However, I still don’t know what is PrivateObject used for and how can we use it.

    Comment by Duc Phong — June 20, 2008 @ 3:01 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.