Problem solve Get help with specific problems with your technologies, process and projects.

Testing methods in Visual Studio 2010

This is the second part in a two part series on unit testing 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.


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 ( 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 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.

Dig Deeper on .NET Framework security tools

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.