Testing with LINQ in Test Studio

Recently I was giving demo on Test Studio to one of  the customer. In demo one of the participants raised a question that, “How could I use LINQ (Language Integrated Query) to insert some data back to database after test execution gets completed “

I demoed to team  that how they could use LINQ in test. In this post, I will walk you through steps required to test with LINQ in Test Studio. We will insert data back to database as last step of the test.

Essentially we can achieve in these three steps

  1. Write LINQ and business logic in different library
  2. Add library as reference in test project
  3. Write script of coded step calling library functions.

To start testing with LINQ, add a coded step in your test. To add coded step select Test from menu and then Script Step from ribbon as given in below image.

image

Once Code step is added export test project to Visual Studio. To export to visual studio from menu click on Export to Visual Studio as shown in image below,

clip_image001

Creating Library to work with LINQ

Now in visual studio you need to add operations related to LINQ in different class library. Idea is that we will create separate library for LINQ operation and later we will add that library to Test Studio.

clip_image003

Assume that we have created a library with name DataLibrary. This Library got

  1. LINQ data context classes
  2. We added a static class in which operation is being performed to insert data back to database.

So DataHelper class contains following code to insert data to database using LINQ.


public static class DataHelper
 {

public static void InsertItem()
 {
 DataClasses1DataContext context = new DataClasses1DataContext();
 Product p = new Product
 {
 Discontinued = false,
 ProductID = 99,
 CategoryID = 2,
 ProductName = "Test Studio 9",
 UnitPrice = 200
 };

context.Products.InsertOnSubmit(p);
 context.SubmitChanges();

 }

}

Adding Reference in Test Project

There are in two ways you can work on Test Studio. Either on Visual Studio plugin or on Test Studio standalone version.

Adding reference in Visual Studio is simpler. Right click on test project and from context menu select Add Reference. Browse to DataHelper.dll library and add reference in test project.

Even though you have added reference in Visual Studio make sure that you have added reference in test project opened in standalone version else you will get exception.

image

To add DataHelper.dll assembly in Test Studio standalone version you need to follow following steps,

Select Project option in top menu

clip_image001[6]

Click on Show Settings in project tab

clip_image002

On clicking on Show Settings you will get Project Settings dialog box. In dialog box click on Script Options. To add assembly reference click on Add Reference.

image

When you clock on Add Reference Test Studio will open File Chooser dialog box. In that browse DataHelper.dll assembly reference location in file system and select that to add as reference in current Test project.

Writing Coded Step

Once reference of DataHelpe.dll is added in test project you need to write script for coded step. We can insert a row in database by calling static InsertItem method. So coded step will be as following


[CodedStep(@"New Coded Step")]
 public void LINQ_Test_CodedStep()
 {

DataHelper.InsertItem();

 }

In this way we can test with LINQ in Test Studio. To summarize

  1. Write LINQ and business logic in different library
  2. Add library as reference in test project
  3. Write script of coded step calling library functions.

I hope you find this post useful. To speedup automated testing do not forget to download trail of Test Studio from here

image

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.