Sunday, March 27, 2016

Coded UI Test : Testing all assertions in a single test case, even any assertion is failed.

Many of us encountered this situation in which we have put more than one assertion in our single test case. The failure of any one assertion cause the script to break. What if we want to test all the assertions and see at the end that which assertion fails and which has passed.
The above can be explained with a simple example. If we want to test the login of google docs, we will test basic functionality of that login box in a single test case. We assume we want to test four simple assertions.




  1. Email text box should have a title of "Email"
  2. Password text box should have a title of "Password"
  3. Sign in button text should be "Sign in"
  4. Stay signed in check box should be checked

In the above scenario, If we put above four assertions in our test case in a simple old-fashioned manner, it would look like below.

[TestMethod]
public void Checking_LoginBox_of_Google()
{
Assert.AreEqual(TitleEmail.Text, "Email");

Assert.AreEqual(TitlePassword.Text, "Password");
Assert.AreEqual(BtnLogin.Text, "Log in");
Assert.AreEqual(CbStaySigned.Checked, true);
}

In the above code, if first assertion fails, then test case will break and it will not check the remaining three assertions.
If we want to check all the assertions and then see the status, we can enclose each assertion in a try-catch block. This will catch every failed assertion and script will not stop. At the end of script, we can check the failed assertion count. If this count is  greater than 0, we can force script to break. See the above code now transformed into something special.

[TestMethod]
public void Checking_LoginBox_of_Google()
{
int failedAssertionsCount = 0;
try
{
Assert.AreEqual(TitleEmail.Text, "Email");
}
catch (AssertFailedException ex)
{
TestContext.WriteLine("First Assertion failed");
failedAssertionsCount++;
}
try
{
Assert.AreEqual(TitlePassword.Text, "Password");
}
catch (AssertFailedException ex)
{
TestContext.WriteLine("Second Assertion failed");
failedAssertionsCount++;
}
try
{
Assert.AreEqual(BtnLogin.Text, "Log in");
}
catch (AssertFailedException ex)
{
TestContext.WriteLine("Third Assertion failed");
failedAssertionsCount++;
}
try
{
Assert.AreEqual(CbStaySigned.Checked, true);
}
catch (AssertFailedException ex)
{
TestContext.WriteLine("Fourth Assertion failed");
failedAssertionsCount++;
}
//in the end check failed assertion count
if (failedAssertionsCount > 0)
{
Assert.Fail();  //to force script to fail because there is some failed assertion present.
}

}
When script will execute, it will show you at which point your script is failed.



I hope this post will help many test automation engineers. Coded UI is a great tool and has many hidden benefits. Share your experience in the comment section, I would love to learn.
Thanks for reading.




No comments:

Post a Comment