Category Archives: Unit Testing

Intellitracing in Production environment – Tester meets Developer

Whenever a tester reports an exception in production, our Nosy ūüôā developers easily deny it saying that it works fine in his Dev environment. They can’t (actually donot want to) debug in production due to¬†various reasons. Also, it is difficult to install any software to collect traces or record¬†logs in Production environment. Hence MS came up with IntellitraceCollection standalone collector to record traces in production environment.

Intellitracing was introduced in VS 2010 ultimate edition. It is sort of historical debugging mechanism. It captures All Asp.Net, Ado.Net  events and Excpetion details like a recording.

This standalone collector is .cab file which will come with VS 2012 install itself. You can also download it separately.

Now with VS 2012 Intellitracing feature enables testers a way of recording their test case. Saving it as a .iTrace file and sharing it with Developer who can open it with VS2012 and replay the whole movie :).

Here are the steps we have to do it in Proudction environment. This is a one time setup. We should not keep it enabled always as .iTrace files grow in size very fast and your production server memory may be an issue. you can turn it ON and OFF as and when needed.

1. download intellitracecollctor.cab for VS 2012 RC. (Inellitrace collector)

2. With VS 2012 RC you will get it installed at this location¬†“<VSInstallDirectory>\Common7\IDE\CommonExtensions\Microsoft\IntelliTrace\11.0.0”

3. Copy this .cab file to a¬†some other location. Then¬†Open powershell. goto location of .cab file. (PS> cd “location of .cab”)

4. type command ->¬† expand /f:* IntelliTraceCollection.cab . (mind this ‘.’ it will maintain the folder structure)

5. create one more folder where you record all your trace files. On this folder give your Apppool identity user full permissions to R/W.

6. Add windows feature “Windows Powershell Integrated Scripting Environment”. (You may want to restart not mandatory though).

7. powershell cmd type -> Import-Module <IntelliTraceExecutableDirectory>\Microsoft.VisualStudio.IntelliTrace.PowerShell.dll

Now Let us say we have a divide by zero exception in prod. Tester finds it and now want to record this experience as a .itrace file.

Tester may ask prod support(ASM) to start collecting trace files for sometime. remember you can start it and stop it. so lets enable recording of tester’s experience.

8. powershell type -> Start-IntelliTraceCollection <ApplicationPool> <PathToCollectionPlan.xml> <Path To Folder Where Saving trace>

9. it will confirm Yes/No input type Y press enter.

10.Now tester can go and replicate the issue. once the exception is thrown. Stop the recording.

11.To stop type ->¬† Stop-IntelliTraceCollection “DefaultAppPool”

12. Confirm with Y then press Enter.

13. Go to your log folder. See for the latest timestamp .itrace file.

14. copy it and share this .itrace file with Developer. It may be pretty big to attach to a bug so share the location.

15. Developer just needs to double click on it and VS2012 will be launched on his machine. This is how it looks once developer opens it.

.iTrace file opened with VS 2012

.iTrace file opened with VS 2012

16. you will notice Exception data section there. Just look for one of the last exceptions occured. In my example i have got a Divide by zero exception.

17. double click on that exception and BOOM!! your code is opened and exact line where the exception was thrown is highlighted. See screenshot

Code highlight

Code highlight

Note: If start command doesnt’ work. Try to import the dll first (point 7).

Now how cool is that? ūüôā

I am still digging more into it and as soon as i find some more information i will update this post. ūüôā

Update: You might be wondering how .iTrace files will the .pdb location.Actually it can be saved in Symbol Servers which are tracked by these .iTrace files and it opens those debug session from that location from any machine.

I hope it helps!

Advertisements

Unit testing using Visual Studio 2010 — WHY and HOW

As a developer, we have been asked about Unit testing¬†during interviews. Typical answer is “yeah! i know how to write a unit test case but did not get a chance to implement it.” ¬†what cliche! ūüôā .

Why Unit testing is important :¬† Apart from SDLC and following the best practices (from my experience nobody cares for unit test cases unless it is a established product). I still found one of the reasons why we may HAVE to perform unit testing for every application in future. Cloud Hosting¬†is making news all around the world and looking at it’s cost benefits companies are trying their best to adopt it asap. With remote hosting comes lot of rules imposed by the vendor who is hosting your application and Unit testing is one of them. for example SFDC (sales force dot com) applications need at least 70% unit testing¬†coverage of before they agree to host it on cloud. ¬†So it’s about time we need to take unit testing seriously.

Good news for visual studio developers is that It’s really easy to implement unit testing in VS2010. ¬†No plugins required for a test project.

Class Library : to unit test. I have a C# class library project. which has only one class  ProductRepository.cs. I am using entity framework to access database (makes life easy. right? :)).

</pre>
public class ProductRepository
 {
 public ProductRepository()
 {
 }
 private readonly static NorthwindEntities _db = new NorthwindEntities();

public static List<Product> GetAll()
 {
 return _db.Products.ToList();
 }

public static Product GetById(int productId)
 {
 return _db.Products.Where(p => p.ProductID == productId).SingleOrDefault();
 }
 }
<pre>

it has only two methods GetAll() and GetById(). You can change the implementation the way you need.

HOW to create a Unit Test using VS 2010: 

  1. Go to any class in library project. say ProductRepository class here.
  2. Go to any method name and Right Click and Select Create Unit Tests.

3.  It opens a wizard. It shows library with all types and its methods listed.  You can select types and methods you want to write unit test cases for.

4. ¬†Select Output project. It can be an existing Test project or Select –> “Create a new C# Test project”

5.  It creates all the test methods for selected methods in library earlier in the wizard.

6. ¬†Check out the Test method created for TestGetAll(). it prefixes “Test” ¬†to all the method names.


[TestMethod()]
 public void GetAllTest()
 {
 List<Product> expected = null;

// change it here and make a db call to populate expected object with the same data source.

//something like below using entity framework.

// expected = _dbContext.Products.ToList();

 List<Product> actual;
 // call library method
 actual = ProductRepository.GetAll();
 Assert.AreEqual(expected, actual);
 Assert.Inconclusive("Verify the correctness of this test method.");
 }

7.  Note that Test project does not copy the .config  settings. Though it will reference the main project so database access layer classes are still available in test methods. Connection strings need to be added manually in test project .config.

8.   Look at a better implementation of TestGetAll () method below.


[TestMethod()]
 public void GetAllTest()
 {
 var _db = new NorthwindEntities(); // declared in class library
 List<Product> expected = null;
 expected = _db.Products.ToList();

 var actual = ProductRepository.GetAll();
 Assert.AreEqual(expected.Count , actual.Count );

// comparing each item in collection

IEnumerator<Product> p1 = expected.GetEnumerator();
 IEnumerator<Product> p2 = actual.GetEnumerator();

while (p1.MoveNext() && p2.MoveNext())
 {
 Assert.AreEqual(p1.Current.ProductID, p2.Current.ProductID);
 Assert.AreEqual(p1.Current.ProductName, p2.Current.ProductName);
 Assert.AreEqual(p1.Current.SupplierID, p2.Current.SupplierID);
 Assert.AreEqual(p1.Current.UnitPrice, p2.Current.UnitPrice);
 }
 }

Above code can be changed as per our needs. If you just want to validate the count of collection or each element.

How to Run Unit Test Cases: 

You will notice that under Solution Items¬†there is a new file created¬†YourClassLibraryProjectName.vsmdi¬†. It will list out all the test methods. you can select the ones you want to run or Run all. Every time you make any changes in Test methods don’t forget to Refresh the .vsmdi file in Test editor.

Just Hit Run Test in Current context and check the results (Passed or Failed) in Test Results window. you should see something like the below.

It may look like coding twice for every method ūüôā but you got to do what you got to do. right?

I will go deeper in my next article “Unit Testing beyond Assert statements”.¬†

I hope it is useful!