Tuesday, April 26, 2016

Verification and Validation

Verification

  • When we check that application is created as per the SRS (software Requirement Specification). Means application performs what user wants it to do.
  • It comes before validation.
  • Static testing like reviews is verification process.

Validation
  • When we test that application is performing its action in correct way. Here we do not check whether application meets user requirement or not, that part is already tested in verification. Here we test whatever application is doing, it does it in correct way.
  • It is performed after verification.
  • Test are executed in validation.

Let us go through one example - 

Requirement specification says that - 
User wants to control the lights in 4 rooms by remote command sent from the UI for each room separately.

Then functional specification is created as follows - 
1. The UI will contain 4 checkboxes labelled according to rooms they control.
2. When a checkbox is checked, the signal is sent to corresponding light. A green dot appears next to the checkbox.
3. When a checkbox is unchecked, the signal (turn off) is sent to corresponding light. A red dot appears next to the checkbox.


Verification
We now verify that  - 
  • Requirement specification is complete and correct such that anyone can understand the requirement easily.
  • Functional specification creates design correctly.
  • Source code has functions for 4 checkboxes to send the signals.


Validation
Now we validate that - 
  • Checkboxes accepts input from user 
  • Lights are actually controlled by checkboxes.

Please let me know your views about the post.

Monday, April 25, 2016

Severity and Priority of bug


Every bug has two fields called as severity and priority that has always confused most of testers. 
Let us understand them with simple examples.

Severity 
It means how severe is the impact of bug on application.

Priority
It means how soon bug should be fixed.

Now we can four combinations of severity and priority.

High Priority & High Severity
Consider an application that maintains student details, and when new record is created application terminates abruptly.

High Priority & Low Severity
The spell mistakes that happens on the cover page or title of an application or company logo is missing.

Low Priority & High Severity
A feature that is rarely used in application is broken, say application has annual report feature but it picks wrong months like from Jan to Dec instead of April to March.

Low Priority and Low Severity
Any spell issues which is with in a paragraph or in alternate text of image.


Friday, April 22, 2016

Create Text File using C#


This post shows a sample code in C# to create a text file. This code can also be used to create a log file for any console or windows form based application.

It is assumed that you already have basic knowledge about C#.

class Program
{
  static void Main(string[] args)
  {
    String fileName = @".\File.txt";
    TextWriter fileWriter = new StreamWriter(fileName, true);
    String fileText = "First Line";
    fileWriter .WriteLine(fileText );

    fileText   = "SecondLine";
    fileWriter .WriteLine(fileText);

    fileWriter .Close();
  }
}

Understanding the code - 

@".\File.txt";
\ in C# has a special meaning. It is used to define an escape sequence. But here we want to use it to represent directory path. So to tell the compiler to not to treat \ as part of escape sequnce we can either put an extra \ with existing \ or put @ before starting the string. 
Strings with @ are called as verbatim string 

Below two strings as same - 
@".\File.txt";
".\\File.txt";

(.) represents current directory which contains the executable. If it is executed from VSTS IDE, (.) represents bin directory.

So overall .\File.txt means text file is in same directory where executable is.

TextWriter fileWriter = new StreamWriter(fileName, true);
This creates a text file in hard disk and associates a default buffer to it.
FileName contains path, file name and extension.
True means if file already exists, append the new text.

string file Text   = "SecondLine";
fileWriter .WriteLine( fileText);
WriteLine method writes the content of string in buffer associated with the file.

fileWriter .Close();
Finally Close() methods writes all the contents of buffer to actual file, saves the file and releases the buffer.

Did you like the post. Please share your thoughts.

Empty Recycle Bin programmatically using C#

Below is the sample code in C# to clean/empty the recycle bin of windows.

private static void EmptyRecycleBin()
{
string recycleLocation = String.Empty;
string strKeyPath = "SOFTWARE\\Microsoft\\Protected Storage System Provider";
 
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(strKeyPath);
string[] arrSubKeys = regKey.GetSubKeyNames();
if (IsVista() || IsWin7())   //Methods are described below
{
  recycleLocation = "$Recycle.bin";
}
else
{
  recycleLocation = "RECYCLER";
}

ObjectQuery query = new ObjectQuery("Select * from Win32_LogicalDisk Where DriveType = 3");

ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

ManagementObjectCollection queryCollection = searcher.Get();

foreach (ManagementObject mgtObject in queryCollection)
{
  string strTmpDrive = mgtObject["Name"].ToString();

  // default is true
  foreach (string strSubKey in arrSubKeys)
  {
    string regKeySID = strSubKey;
    string recycleBinLocation = (strTmpDrive + "\\" +
                recycleLocation + "\\" + regKeySID + "\\");

    if (recycleBinLocation != "" &&
     Directory.Exists(recycleBinLocation))
    {
      DirectoryInfo recycleBin = new
        DirectoryInfo(recycleBinLocation);

      // Clean Files
      FileInfo[] recycleBinFiles = recycleBin.GetFiles();
      foreach (FileInfo fileToClean in recycleBinFiles)
      {
        try {
            fileToClean.Delete();
        } catch (Exception)
        {
            // Ignore exceptions and try to move next file
        }
      }

   // Clean Folders
   DirectoryInfo[] recycleBinFolders=recycleBin.GetDirectories();
   foreach (DirectoryInfo folderToClean in recycleBinFolders)
   {
     try {
         folderToClean.Delete(true);
     } catch (Exception)
     {
         // Ignore exceptions and try to move next file
     }
    }
    Console.WriteLine("Cleaned up location:
     {0}",recycleBinLocation);
   }
  }
 }
}

private static bool IsVista()
{
   string majorOSVersion =
            Environment.OSVersion.Version.Major.ToString();

   if (majorOSVersion.Equals(Convert.ToString(6)))
       return true;
}

private static bool IsWin7()
{
   string majorOSVersion =
      Environment.OSVersion.Version.Major.ToString();

   if (majorOSVersion.Equals(Convert.ToString(7)))
      return true;
}

Share it if you like it.

Extension Methods in C#

Suppose you have created a class where operations are performed on class fields using public methods.

If you want to add more methods to extend the functionality of this class, you will typically create a child class, write a method in child class and use this class whereever required.

With extension methods you don't need to create a child class. It enables you to add methods to existing types (classes) without creating a new derived type, recompiling, or otherwise modifying the original type.
Extension methods are actually static methods but of special type. They are called as if they were instance methods on the extended type.

They are introduced in C# from 3.0 version.

They are only in scope when the namespace in which  they are defined is explicitly imported into the source code with a using directive.

Example - LINQ methods like GroupBy(), OrderBy(), Average(), are extension methods on IEnumerable<T> types like List<T> or Array.
They are not defined in these types but can be accessed using dot operator on list objects.

Example -
class MyExtensionMethod
{
    static void Main()
    {          
        int[] arr = { 10, 45, 15, 39, 21, 26 };
        var result = arr.OrderBy(s => s); 
        foreach (var i in result)
        {
            Console.Write(i + " ");
        }        
    }      
}

Output - 10 15 21 26 39 45

Here OrderBy() method is an extension method.

Create your own extension method -
--------------------------------------
namespace MyExtensions
{
    public static class MyExtensionsMethod
    {

//Extension Method
        public static int WordCount(this String str)
        {
            return str.Length;
        }
    }
}

Parameter in public static int WordCount(this String str) i.e. specifies which type the method operates on, and the parameter is preceded by the this modifier.

And it can be called from an application as below:
using MyExtensions;
string s = "Hello Extension Methods";
int i = s.WordCount();

Note -
1. Extension methods cannot access private variables in the type they are extending.

2. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself. In other words, if a type has a method named Process(int i), and you have an extension method with the same signature, the compiler will always bind to the instance method.


Please share your views about this post.

Create Log file for Coded UI Tests

Logs containing results and other information helps in debugging the test failures. This post will help you to create a log file for coded UI test in Microsoft Visual Studio 2010.

Suppose there is one coded UI test method CodedUITestMethod1 with one recorded method as SignInTest in UIMap.Designer.cs.

[TestMethod]
public void CodedUITestMethod1()
{
   this.UIMap.SignInTest();
}

To add logs to it, modify partial class UIMap.cs as follows -

public void WriteLogs(string message)
{
  FileInfo f = new FileInfo(strAppPath + "Results.txt");
  StreamWriter w = f.AppendText();
  w.WriteLine(message);
  w.Close();
}

Call this method from test method as follows -

[TestMethod]
public void CodedUITestMethod1()
{
  this.UIMap.SignInTest();
  this.UIMap.WriteLogs("Logging Into Aplication Event Success");
}

Please share your views about this post.