Testing methods in Visual Studio 2010

Testing methods in Visual Studio 2010

Read Unit testing in Visual Studio 2010.

Renaming the method

Test methods should follow good naming conventions and you should strive to make the name self-documenting. That way other developers on the project will have a better understanding of what each test method does. Therefore, the TestMethod1 must be renamed to a more relevant name.

I prefer to use a three part name like this.

CalculateFuturePaymentDate_ValidInput_DateReturned30DaysInFuture/

The first part in the method name under test (CalculateFuturePaymentDate).  The middle part is the scenario I am testing (ValidInput) and the last part is the expected outcome (DateReturned30DaysInFuture).

Writing Tests

Obviously I need to add some testing code within the test method.

There are usually three phases needed in a useful unit test.  These steps have been formalized in the ArrangeActAssert pattern (http://c2.com/cgi/wiki?ArrangeActAssert) which I will use in my example.

The Arrange phase is where I setup the necessary preconditions and inputs.  I this example that consists of instantiating the PaymentDates object and configuring an input parameter.

The Act phase is where I act upon the object or method under test.  I my case the just

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

means to call the production method and store the results in a variable.

The Assert phase is where I assert that the expected results have occurred. The Assert class in MSTest (the underlying test engine in Visual Studio) provide all the method for verifying that the production code is valid. In this case I assert that the two values are thirty days in the future.

Here is the complete code for the first test.

The second test

I should run the above test and verify that it passes before writing any other test methods. In interest of brevity however let me show you the test code for the second test method before running the test.

Running the Tests

Like most testing frameworks, Visual Studio 2010 contains a test runner.

To run the tests, open the Test/Run/All Tests in Solution menu as shown below.

On my computer I see one successful test and one failed test. 

To fix the error the code in the Sunday case statement needs to be modified as shown.

I leave it to you to fix the code and rerun the tests. I’m sure you get the general idea.

Next month I’ll look deeper into the test runner and test reporting tools available in Visual Studio.

This was first published in September 2011

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.