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.
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
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).
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 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.