Saturday, October 19, 2013

Learn UnitTest in .Net

You want to write unit test for any class. Following are the attributes you need to write a unit test.

Mandatory:
1 - TestClass - This attribute identifies that attributed class is a test class.
2 - TestMethod - This attribute identifies that the attributed method is a test method in the test class.

3 - TestInitialize: This attributed method runs before the every test method.
4 - TestCleanup: This attributed method runs after the every test method.
5 - ClassInitialize: This attributed method runs once when the unit test class is initialized. Normally it can be used when we perform DB seeding or mocking objects globally.
6 - ClassCleanup: This attributed method runs once when the unit test class has run all the test methods. It can be used when the data inserted needs to be rollback.
7 - TestCategory: When the group of classes need to executed simultaneously or in a one go then on the class this attributed is binded with any name. e.g. Financials or something else.
How to group module unit test classes.
8 -ExpectedException: When any function raise any exception then this attribute can be used.

Here's a snippet of a code that will show the above attributes.

//A business class that performs add operation.
public class Maths
   {
      public int Add(int a, int b)
      {
         return checked(a + b);
      }
   }


// A test class that checks the functionality of the business class Maths.
   [TestClass]
   public class UnitTest1
   {
      Maths obj = new Maths();
      
      [TestInitialize] // you can use ClassInitialize attribute. 
      public void Setup()
      {
         System.Diagnostics.Debug.WriteLine("In a child debug ");
      }

      [TestCleanup] // you can use ClassCleanup attribute.
      public void Cleanup()
      {
             obj.Dispose();
      }
//// A test method.
      [TestMethod]
      public void TestMethod12()
      {
         Assert.IsTrue(obj.Add(2, 3) == 5);  
      }
/// A test method that shows exception checking.
      [TestMethod]
      [ExpectedException(typeof(OverflowException))]
      public void TestOverflowException()
      {
         int i = obj.Add(int.MaxValue, int.MaxValue);
      }
   }

Verification of the Function return values.
There are some classes in the unit testing framework from which you can test your expectations or actual result.

1 - Assert - Description of the methods in this class are here
2 - CollectionAssert - Description of the methods in this class are here 

You can check here to get more detail and more attributes about this testing framework.

TDD (Test Driven Development) .Net

In this post I am going to explain a little bit about the TDD, code coverage and feature coverage.

What is TDD? Simply Test Driven Development, what does it mean? It means that you are going to make your test cases first and then you are going to write minimum amount of code to pass all the test cases.

Not all the classes can be tested 100% because of some DB limitations or web service limitations or sometimes related to views or events but 50% of them that have limitation can be mocked and tested. Later on in my posts I will discuss about mocking but right now I am going to talk about a simple method 'Add' that takes two integer and returns an integer value. The code snippet will be in C# .

      public int Add(int a, int b) //prototype

What you expect from the signature what it should perform? Simply it will take two integer values and return the sum of them. e.g. 2 + 3 = 5. I am assuming the class name will be Maths.

Here's a Unit test of it.

   [TestClass]
   public class MathsTest
   {
         Maths obj;
      [TestInitialize]
      public void Setup()
      {
             obj = new Maths();
      }

      [TestCleanup]
      public void Cleanup()
      {
          //dispose any object or cleanup DB
      }

      [TestMethod]
      public void TestMethod1()
      {
         Assert.IsTrue(obj.Add(2, 3) == 5);
      }
   }

Now our test class is completed and now we can write a code for the Maths class. So here's the code snippet of Maths class with 'Add' function.

 public class Maths
   {
      public int Add(int a, int b)
      {
         return a + b;
      }
   }

Is it completed? We have tested this method so it should be but my answer is no, we have got a 100% coverage of the code but we have not tested it completely. So what are the missing functionalities and missing test cases. Following are the listed below.

1 - The arguments are not tested on the minimum values of integer.
2 - The arguments are not tested on the maximum values of integer.
3- The arguments are not tested on the negative values of integer.

Let's test the #2 test case. I expect if I am providing the max value of int type and it returns int type value then it should raise an exception.

      [TestMethod]
      [ExpectedException(typeof(OverflowException))]
      public void TestOverflowException()
      {
         int i = obj.Add(int.MaxValue, int.MaxValue);
      }

Run and test it, is it raising exception. No, why? Why it is not raising exception because there is a fault in a very simple code. Instead of writing the above one it should be like below. Just replace return line with this one.
         return checked(a + b);

Now run it, it is perfectly working as expected.

So you have seen how much the test cases are important. You can try yourself the other test cases as well.

There is one misconception if your class code is traversed 100% by your test class then it won't be a 100% test coverage until you have covered all the test cases as well. Hope this will help you in writing the code.

My next post will be about a design pattern of a unit test that you should follow.  

Wednesday, April 10, 2013

Amazon S3 file download

A code snippet to download files from S3 by providing rootbucket, access key and secret key.


 AmazonS3 _client;  
 using (_client = Amazon.AWSClientFactory.CreateAmazonS3Client(accesskey, secretkey))  
 {  
      var util = new TransferUtility(_client);  
      var request = new TransferUtilityDownloadRequest()  
           .WithBucketName(rootbucket)  
           //e.g. full path is sohail/video/123.mp4  
           // root bucket is sohail, video/123.mp4 is WithKey  
     .WithKey(at which location have to download S3 file)   
           // Where to download location of hard disk.  
     .WithFilePath(ToDownloadPath);  
   util.Download(request);  
 }  

Files Downloader by extension from webpage


Download the files just providing the url and the desired extensions. Create a folder where the downloaded files will be placed. D:\FileDownloaderPath1\

Note: the .net framework must be available.

This will download those files that are linked via anchor tag.

It can be downloaded from the below URL.

FilesDownloader App

Tuesday, April 9, 2013

WMI and Custom configuration with multiple hierarchies


Please find the below link that will show how to use Multiple custom configuration hierarchy and WMI management.

WMI and Custom Configuration

Description
In the configuration file a custom configuration can be made to handle the services on a server. This configuration is using  a multiple hierarchy. So the following classes are being used to manage the multiple custom configuration.

ConfigurationSection
ConfigurationElement
ConfigurationElementCollection

Second thing, WMI will use that configuration and the relevant services on the specified server and will show in a grid. It can be stopped and started again via a desktop client.

Note: I have not used threading yet. This is just a demo so if a form hangs on then do wait.
Following classes are used to handle remote services.
ConnectionOptions
ManagementScope
ManagementObjectSearcher
ManagementObject

Monday, January 14, 2013

QueryRange error

Some of the time when you are applying expression in a QueryRange, you may get this error.

Query extended range failure: Right parenthesis expected near pos ....

There is a minor fix of this issue.

See the difference in both expressions.

This will produce error. Syntax wise it is correct but there is a minor error.

queryBuildRange.value(strFmt('(ItemType == %1 || ItemId == "%2")', 
    any2int(ItemType::Service),
    queryValue("B-R14")));


This will work fine.


queryBuildRange.value(strFmt('((ItemType == %1) || (ItemId == "%2"))', 
    any2int(ItemType::Service),
    queryValue("B-R14")));



Got the difference in both queries?

Yes, the every sub expression must be enclosed in the round bracket. See in the above one (ItemType == %1 && ItemId == %2), there is no round bracked enclosed with the expression. In the other one brackets are enclosed.

Copied the code from
http://www.axaptapedia.com/Expressions_in_query_ranges






Thursday, December 27, 2012

LINQ Join Query

    class Employee  
    {  
      public int EmployeeID { get; set; }  
      public string EmployeeName { get; set; }  
      public int DeptId { get; set; }  
    }  
    class Dept  
    {  
      public int DeptId { get; set; }  
      public string DeptName { get; set; }  
    }  
      List<Dept> dept = new List<Dept>();  
      List<Employee> emp = new List<Employee>();  
      dept.AddRange(new Dept[]   
      {   
       new Dept{ DeptId=1, DeptName="IT" },  
       new Dept{ DeptId=2, DeptName="Engineering" }  
      });  
      emp.AddRange(new Employee[]  
      {  
       new Employee{EmployeeID = 1, DeptId = 1, EmployeeName="Sohail"},  
       new Employee{EmployeeID = 2, DeptId = 1, EmployeeName="Zeeshan"},  
       new Employee{EmployeeID = 3, DeptId = 2, EmployeeName="Zubair"},  
      });  
      emp.Join(dept, empRow => empRow.DeptId, deptRow => deptRow.DeptId, (selectEmpRow, selectDeptRow) =>  
       new  
       {  
         EmpId = selectEmpRow.EmployeeID,  
         EmpName = selectEmpRow.EmployeeName,  
         DeptName = selectDeptRow.DeptName  
       })  
       .ToList()  
       .ForEach(_ => { Console.WriteLine("{0} {1} {2}", _.EmpId, _.EmpName, _.DeptName); });