03 May 2010

Automate unit testing with VisualStudio 2008

Automate Unit Tests
As soon as the developer finishes the code, the developer wants to know if it is producing the expected result before getting into any more detailed testing or handing over the component to the tester. The type of testing performed by the developers to test their own code is called Unit testing. Mainly because it's an automated way to make sure it is always behaving the way it is supposed to behave. This way we can make changes to the code and refractor pieces of the application without the fear of breaking something somewhere else in the application.Well here comes the definition now :- "Unit testing is a procedure used to validate that individual units of source code are working properly. A unit is the smallest part of an application. In object-oriented programming the smallest unit is method”
Like all my other blogs, here also I don’t want to explain what is unit testing and why we do it. More specifically I want to explain how practically we automated our application.
Creating a test project
One of the great improvements of Visual Studio 2008 brought to developers is the built in support for unit testing. With Unit Testing support, it is very easy for developer to create, execute and repeat unit test cases.Setting up a test project in Visual Studio 2008 is extremely easy. All it requires is adding a test project into a solution by selecting Test Project template under Test project type. Let’s create our test project in the File> New> Project, and selecting the test> Test Project. At this point the project template will create a solution with projects with default class.A test class is generated once the Test project is created. Visual studio automatically marks the test class and method with TestClassAttribute and TestMethodAttribute.
Adding unit test
It is recommended to create one test class per class to be tested. A test class can be added by either selecting from Add New Test popup or by selecting Unit Test from Add menu.



















Alternatively, Unit test can be added by right clicking the method name and select “Create Unit Tests” in context menu. This brings up the Create Unit Tests screen where multiple methods to be tested can be selected.
Click on settings will give another screen were we can set configurations:



















Unit testing support for ASP.NET
Visual Studio 2008 allows developers create test cases for methods defined in ASP.NET website or applications. Even better, tests are running under ASP.NET context which means settings from Web.config file are automatically picked up once the test case starts running.It worth pointing out two attributes HostTypeAttribute and UrlToTestAttribute. HostType makes sure that test case is running ASP.NET host process. UrlToTest specifies URL to test when test is run.
[TestMethod()]
HostType("ASP.NET")]AspNetDevelopmentServerHost("C:\\DemoCompany\\SourceCode\\WebApplication\\DemoWeb", "/")]UrlToTest("http://localhost:4480/")]DeploymentItem("DemoWeb.dll")]
public void GetMnemonicsTest()
   Search_Accessor target = new Search_Accessor(); // TODO: Initialize to an appropriate value
   target.GetMnemonics();
}
Yet another normal method:
[TestMethod]

public void OnNameChanged_MoreThan20Chars_TakesFirst20Chars()
{
     string testName = "JijoVenginikkadan";Customer customer = new Customer();
    string expectedName = "JijoVenginikkadanGopalakrishnan";
    Assert.AreEqual(expectedName, customer.CompanyName,"OnNameChanging should take only first 20 characters");
}
Execute test method
Test method can be started by right clicking the test method name and select Run Tests. Visual Studio 2008 displays test results in Test Result output window.The result is afcource going to be failed here based on the test case that we provided.
Assert.Inconclusive("A method that does not return a value cannot be verified.");

Debugging support in Unit Testing
All test classes/methods can be run under debug mode. Simply put a break point where the code needs to stop and start the test by clicking “Debug Tests in Current Context” button located in the toolbar or “Tests in Current Context” menu of Debug option under Test menu.

No comments: