Showing posts with label VSTS. Show all posts
Showing posts with label VSTS. Show all posts

Tuesday, May 10, 2011

.NET/ASP.NET Interview Question - Can we do unit testing using VSTS? How?

Answer:
Unit testing is validation and verification technology where the developer tests the individual units of source code. These individual units can be functions, methods or class.
Below is the simple Math class which has a Add function with two input parameter. The Add function basically adds the number and gives the addition of these two numbers.
As a developer we would like to test is this Add function actually works properly or not.

namespace TestVSTS
{
public class Math    {
public int Add(int i, int j)       {
        int sum;
        sum = i + j;
       return sum;
     } }
}

 
In order to do unit testing by using VSTS the first thing you need to create a simple unit test. In visual studio team system (VSTS)they provide the test menu in which you can select the unit testing as you can see in the below diagrams.






After selecting unit test open the .cs file and write the logic in [TestMethod] attribute which you want to test.

[TestMethod]
public void Check()
   {
     Math obj = new Math();
     int r = obj.Calculate(10,20);
     Assert.AreEqual(30,r);
   }

Once you have completed the above steps, now set unit test project as startup project, and run the application.

If the function work properly it will gives us Passed result as you see in below diagram.


If the Unit test gives us Failed result it means something wrong in function as you see in the below diagram.



Please click here to see more .NET/ASP.NET interview questions

Regards,


Visit Authors blog for more .NET and ASP.NET interview questions

Monday, August 23, 2010

VS 2010 Deployment (Package, One click deploy and Web.config transformation)

VS 2010 Deployment (Package, One click deploy and Web.config transformation)


Introduction and Goal
Problems with deployment
The Package and “MSDeploy”
Creating the package
Deploying the package
The one click deploy
Web.config transformation
Locate and Transform
More Details of Locaters and Transformation


Introduction and Goal


In this article we will discuss about 3 important features provided by VS 2010 to ease our deployment task on production and other environment. We will first start with understanding problems with deployment and then move ahead by creating packages , one click deploy and web.config transformation.

Please feel free to download my free 500 question and answer eBook which covers .NET, ASP.NET, SQL Server, WCF, WPF, WWF . For more details Click here

Problems with deployment


When we talk about deployment it’s not just compiling and shipping DLL’s to the production server. It involves lot of other things like IIS settings, SQL scripts, text file, configuration etc.


As a developer you would like to give a package to the administrators to install with all the dependencies or you would like to install the same from your visual studio IDE itself.

The Package and “MSDeploy”

Package is nothing but a ZIP file which has all the necessary components (DLL, SQL Scripts, configuration files, IIS settings etc) needed by your application.



Creating the package


To create package, click on project – properties tab you will find two tabs ‘package/publish web’ and ‘package / publish SQL’. The first tab has all the IIS and general settings while the SQL tab is for deploying SQL Scripts on production.


The Package and publish web tab has certain key things which you can define from IIS stand point and what should go in the package. Below figure describes some important key features



The “Package/Publish SQL” tab helps you to configure everything what is related to SQL Server. The first point is you can import “web.config” file and all the connectionstrings of the web.config is taken to generate the SQL scripts.


You can also specify source and destination of SQL server for the package. The source destination is imported from the “Web.config” automatically, but you can always go and change it as per your requirements.


There are three more points in the “Package/Publish SQL” tab:-

• You can see the auto-generated schema, in other words the schema is generated automatically from specified source SQL Server.

• If you want to go and add some scripts manually, like insert dummy data etc, you can always use the ‘add script’ tab as shown in the figure.

• The final thing which I personally love is that you can sequence how the scripts should run. For instance in the below figure first the auto generate schema SQL will run and then ‘indexes.sql’ script will execute. This is a nice feature to exploit from versioning stand point.

In order to create the package, click on build deployment package menu and you should see the necessary package files in your project package folder.


Deploying the package

Ok, now the package is generated, let’s understand how we can go and install the same. To install the package we need the Microsoft web deploy software. Microsoft web deploy a.k.a “msdeploy” was created by Microsoft IIS team to migrate from IIS 6 to IIS 7.

To leverage its power it’s used as a back bone to install the package created by VS 2010.

If you remember the package folder created in the first step has some XML files. There are three files which are very important one is the source manifest, destination manifest and a cmd file. The manifest file is read by MS deploy for Meta data and deployment configuration and later these all things are executed via the cmd file.

The first thing you would like to do is check if the package is right and it will execute properly on the environment. To do a trial installation you need to go to “Microsoft Web Deploy” command and execute the cmd file with “/t”. This does not actually install but does a brief check of whether there can be any issues while actual installation.

C:\Program Files\IIS\Microsoft Web Deploy>E:\deleteit\MyWebApplicationToPackage\
MyWebApplicationToPackage\obj\Debug\Package\MyWebApplicationToPackage.deploy.cmd
/t


Once you execute the cmd file you should see summary of changes
which will take place on your production server as shown in the below figure.



Once you have cross checked, you can deploy the package by using “/y” attribute as shown in the below snippet. If you run your package using the ‘y’ command it should create the IIS applications as well execute necessary database scripts to create SQL Server objects.

C:\Program Files\IIS\Microsoft Web Deploy>E:\deleteit\MyWebApplicationToPackage\
MyWebApplicationToPackage\obj\Debug\Package\MyWebApplicationToPackage.deploy.cmd
/y


The one click deploy


Many times you would like to install the package from the VS IDE itself rather than giving it to administrators for installing. In order to achieve the same VS 2010 IDE has something called as ‘One click deploy’.


As the name says, you can deploy your package in one click in different modes by clicking on ‘Build’ and then ‘Publish’ menu. Below is the screen shot of how the one deploy publish looks like.
You can publish on FTP. Web deploy, file system etc.


Web.config transformation


The final feature which is worth discussing is the ‘Web.config’ transformation. Many times you have different config files for different environments. It causes of lot of confusion when it comes to handling different versions of web.config files.



So go to tools – Configuration manager and add a new config file as shown in the below figure. You can see that it creates a parent ‘Web.config’ files which can be overridden by child config files with their own settings.


In order to see your newly added configuration you need to right on the web.config file and click ‘add config transforms’ as shown below.


Depending on your active configuration selected the appropriate ‘Web.config’ will be created for the environment.


Locate and Transform


One of the questions which will come to your mind is how to do the transformation in the child web configuration file. In other words your parent web config file has a connection string how can we override/ transform the same in the child config files like production, acceptance, test etc.

The transformation of the config file is a two step process first is to search/locate what needs to be transformed and second to do the actual transformation.


There are 3 types of mechanisms by which you can search/locate a particular property / element Match, Condition and XPath. There are eight ways by which you can set / replace / transform the searched property or element as shown in the above figure.

Let’s take one example how the search and transform works. Below is an image code snippet of the parent ‘web.config’ file and a child file having ‘web.production.config’ file.

We want to change the connection string to a different value for the production environment. You can see in the below code snippet the production environment config file has used “xdt:locator” to search by ‘name’ and then we want to set the attribute to a different value so we have used ‘setattribute’ transformation.


More Details of Locaters and Transformation

Let’s have a cursory look on the 3 locators and 8 transformations.
All the below stuff I have ripped from http://vishaljoshi.blogspot.com/2009/03/webdeployment-webconfig-transformation_23.html.

xdt:Locators


The inbuilt xdt:Locators are discussed below.
Match
In the provided syntax sample below the Replace transform will occur only when the name Northwind matches in the list of connection strings in the source web.config.Do note that Match Locator can take multiple attributeNames as parameters e.g. Match(name, providerName) ]

<connectionStrings>
<add name="Northwind" connectionString="connectionString
goes here" providerName="System.Data.SqlClient"
xdt:Transform="Replace" xdt:Locator="Match(name)" />
</connectionStrings> 
Condition

Condition Locator will create an XPath predicate which will be appended to current element’s XPath. The resultant XPath generated in the below example is “/configuration/connectionStrings/add[@name='Northwind or @providerName=’ System.Data.SqlClient’ ]”
This XPath is then used to search for the correct node in the source web.config file

<connectionStrings>
<add name="Northwind" connectionString="connectionString goes here"
providerName="System.Data.SqlClient" xdt:Transform="Replace"
xdt:Locator="Condition(@name=’Northwind or @providerName=’
System.Data.SqlClient’)" />
</connectionStrings>


Xpath
This Locator will support complicated XPath expressions to identify the source web.config nodes. In the syntax example we can see that the XPath provided will allow user to replace system.web section no matter where it is located inside the web.config (i.e. all the system.web sections under any location tag will be removed.)
<location path="c:\MySite\Admin" >
<system.web xdt:Transform="RemoveAll" xdt:Locator="XPath(//system.web)">
...
</system.web>
</location>

xdt:Transform


Replace
Completely replaces the first matching element along with all of its children from the destination web.config (e.g. staging environment’s web.config file). Do note that transforms do not modify your source web.config file.

<assemblies xdt:Transform="Replace">
<add assembly="System.Core, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>


Remove

Removes the first matching element along with all of its children
<assemblies xdt:Transform="Remove"></assemblies>


RemoveAll
Removes all the matching elements from the destination’s web.config (e.g. staging environment’s web.config file).
<connectionStrings>
<add xdt:Transform="RemoveAll"/>
</connectionStrings>


Insert

Inserts the element defined in web.staging.config at the bottom of the list of all the siblings in the destination web.config (e.g. staging environment’s web.config file).
<authorization>
<deny users="*" xdt:Transform="Insert"/>
</authorization>
SetAttributes

Takes the value of the specified attributes from the web.staging.config and sets the attributes of the matching element in the destination web.config. This Transform takes a comma separated list of attributes which need to be set. If no attributes are given to SetAttributes transform then it assumes that you would like to Set all the attributes present on the corresponding node in
web.staging.config

<compilation batch="false"
xdt:Transform="SetAttributes(batch)">
</compilation> 

RemoveAttributes
Removes the specified attributes from the destination web.config (i.e. staging environment’s web.config file). The syntax example shows how multiple attributes can be removed.
<compilation
xdt:Transform="RemoveAttributes(debug,batch)">
</compilation>
InsertAfter (XPath)

Inserts the element defined in the web.staging.config exactly after the element defined by the specified XPath passed to “InsertAfter()” transform. In the syntax example the element <deny users="Vishal" />will be exactly inserted after the element <allow roles="Admins" /> in the destinationXML.

<authorization>
<deny users="Vishal" xdt:Transform="InsertAfter
(/configuration/system.web/authorization/allow[@roles='Admins'])” />
</authorization&gt>

InsertBefore (XPath)

Inserts the element defined in the web.staging.config exactly before the element defined by the specified XPath passed to “InsertBefore()” transform. In the syntax example the element <allow roles="Admins" />will be exactly inserted before the element <deny users="*" />in the destinationXML.

<authorization>
<allow roles=" Admins" xdt:Transform="InsertBefore
(/configuration/system.web/authorization/ deny[@users='*'])" />
</authorization>

Friday, December 4, 2009

12 Important FAQ’s on VSTS Testing (Unit testing, load testing, automated testing, database testing and code coverage)

12 Important FAQ’s on VSTS Testing (Unit testing, load testing, automated testing, database testing and code coverage)




Introduction

This article has 12 important FAQ’s which covers unit testing, automated testing, data driven test, load/ performance test, code coverage , database testing and ordered testing.
I have collected around 400 FAQ questions and answers in Silverlight, Azure, VSTS, WCF, WPF, WWF, SharePoint, Design Patterns, UML etc. Feel free to download these FAQ PDF’s from my site http://www.questpond.com/

VSTS 2010 download

This article uses VSTS heavily so in case you do not have the BETA download the same from:-
http://www.microsoft.com/downloads/details.aspx?FamilyID=255fc5f1-15af-4fe7-be4d-263a2621144b&displaylang=en

What is Unit testing?

Unit testing is validation and verification methodology where the developers test the individual units of source code.
Some key points to be remembered from: -
  • A unit is the smallest part in the application which can be tested. So it can be either a method. Function or class.
  • These tests are conducted during development.
  • Unit test belongs to the white box testing category.

What are the different ways by which you can do unit testing with .NET?

There are 2 primary / accepted ways of doing unit testing:-
  • NUNIT
  • Visual studio team edition test system

Can we start with a simple NUNIT example?

Ok so let’s say we want to do unit testing for a simple class ‘clsInvoiceCalculation’ shown below. This class calculates total cost by taking per product cost and number of products as input.
public class clsInvoiceCalculation
    {
        public int CalculateCost(int intPerProductCost, int intNumberOfProducts)
        {
            return intPerProductCost * intNumberOfProducts;
        }
}


Let’s say we want to execute the below test case on the above class using NUNIT.



Step 1:- The first step is to download to the NUNIT software from
http://nunit.org/index.php?p=download
Step 2:- Create a new C# class project and add reference to “C:\Program Files\NUnit 2.5.2\bin\net-2.0\framework\nunit.framework.dll”. We also need to add reference to the class project which we are testing i.e. the invoice project.


Step 3:- Add reference to the NUNIT and your invoice project in your code.
using NUnit.Framework;
using Invoice;


Step 4:- We need to create a simple class with ‘TestFixture’ as attribute. We then to create a method attributed with ‘Test’ which will have our test.


You can see in the above figure how the ‘TestInvoiceCalculation’ method creates a object of ‘clsInvoiceCalculation’ , passes the values and checks if the returned values has the expected results. ‘Assert’ command is used to check if the expected output and the results return match.


Step 5:- Once we are finished with our test case we need to compile the same to create a DLL. Once the DLL compilation is done go to program files – nunit and click on nunit. Now the nunit user interface will open. Click on file – open project and select your test DLL.

Once you select your DLL you will get a screen as shown below. On the right hand side you can see your test class with test function which has the test case. Check the test function and hit run. Now if the test case passes you will see a full green color or else you will see a red color with details of why the test case fails.


Screenshot when the test case fails.



How can we make NUNIT test cases data driven?

In the previous questions we had hardcoded the test data in the NUNIT test case itself. But in real time you would like to have test data inputs coming from a XML file or a database, in other words you would like to create data driven test cases. In order to create data driven in NUNIT we can attribute the unit test method with ‘TestCaseSource’ attribute as shown in the below figure. In ‘TestCaseSource’ we need to provide the function which will return the test case data, in this case it is ‘TestCases’.


Below is the code snippet which provides dynamic data to unit test method. The function which is providing the dynamic data should return ‘IEnumerable’. NUNIT provides something called as ‘TestCaseData’ class. This class defines the test case data for NUNIT. The class provides ways by which you can specify input and the expected output from the test case.

Finally to return test case by test case we need to use ‘yield’ keyword with for each. You can see the below code snippet to understand how yield works.


How can we do unit testing using VSTS test?

If you do not have VSTS get the beta @
http://www.microsoft.com/downloads/details.aspx?FamilyID=255fc5f1-15af-4fe7-be4d-263a2621144b&displaylang=en

In order to do unit testing using VSTS right click on the visual studio solution explorer and select add new project. In the test project right click and select add new test you should be popped with a dialog box as shown in the below figure. From the dialog box select ‘Basic Unit test’.


In order to create a function which will execute our unit test you need to attribute the function with “[TestMethod]”. If you remember in Nunit it was ‘[Test]’. The assert function does not change.

Once you have done the unit testing coding compile the same, click on test, run and select ‘Tests in the current context”.


Once you do the previous test, the test starts running from pending to in progress and finally showing the results.

In case your test fails and you would like to see detail results of the same double click on the test to see a detailed result as shown below. The below results shows that the test failed because it was expecting “200” as the output and it got “30”.


How can we create data driven unit test using VSTS Test?

Creating data driven unit test is pretty simple in VSTS. First create a simple table with test data values. For instance you can see in the below figure we have created a table with 3 fields i.e. 2 input fields and 1 expected value. To consume this table apply ‘DataSource’ attribute with a proper database connection string as shown in the below figure.

To get field data from the table we can use the ‘DataRow’ with index.


We have 3 test cases in the table, so when we executed the above test’s we got the below test results in our visual studio IDE result window.



How can we do automated testing using VSTS?

In order to do automate testing in VSTS we need use the ‘Web test’ template. So click on add new test and select ‘Web test’.


Once you have selected web test browser will open with a record, pause and stop button as shown in the below figure. Once you start posting and requesting, the URL recorder starts recording every request and response as shown in the below figure. On the right you can see how the recorder has recorded all the get and posts.


Below is a snapshot of simple login screen which is recorded. There are two requests, the first request is for the login page and the second request is when you post the login page with userid and password. You can see the values i.e. ‘Admin’ and ‘Admin’ in userid and password textboxes.


We will also need to define in which conditions the test will pass. Now right click on the second post and select ‘Add validation rule’ as shown in the below figure.


Select the rule saying if anywhere on the browser response we found ‘Logged in’ text that means that the test is passed.






If you run the test you will get the executed values with the test specifying it’s a pass or fail.





 



 



 



 



 



 



 



How can we make the automated test data driven in VSTS?

Once you have created the web test, right click on the web test and click add data source as shown in the below figure.



 Once you have added the data source you can then specify the database fields as inputs to the text boxes as shown in the below figure.


We need to perform one more step to ensure that the data driven test runs fine. Right click on the testrunconfig file and select one per data row as shown in the next figure.





Once you are done you can run the test and see how VSTS picks up row by row test cases and executes the test. You can see in the below figure the first test case has failed while the second test case has passed.


How can we do coverage testing using VSTS?


Code coverage is a 3 steps process as shown below. The first step is to
enable code coverage. So right click on the ‘.testrunconfig’ file in the
solution explorer as shown in the below figure.





The next step is to select the assembly / DLL which we want to monitor for
code coverage, below is the figure which shows the same.



Once you run the test, right click on the test results and select code
coverage results. You will be shown a details result as shown below where you
can see which part of your application is covered and tested.



What are the different steps involved to execute performance test using VSTS?

I will be updating in 2 days the pictures are not fitting well.Coming soon…..

I have heard about Database testing in VSTS, what does it do?

I will be updating in 2 days the pictures are not fitting well.Coming soon…..


What is ordered testing?

I will be updating in 2 days the pictures are not fitting well.Coming soon…..