Coded UI test–keep on trucking

So, have you ever wished you could make your Coded UI test keep on running to the end, even if it finds errors along the way?

I had someone ask me about that the other day and after doing a little searching, discovered that since it’s a Coded UI test, you can make it do pretty much anything you want. Including keep on running even if an assert fails.

How? It’s pretty simple, actually.

You’ll need to modify your CodedUITest1.cs file (or whatever the name of the generated code file is) to add try/catch statements around the calls to the Assert methods, and add a bit of logic to fail the test at the end of the routine if any of the assertions failed along the way.

When an assertion fails you can catch the exception and write a message to the Console, or to a log file, a database – wherever. (Someplace that you’ll be able to review after the test ends.) The sample code below writes to the console.

Also when an Assert fails I set an indicator, and at the end of the test run I check to see if all the Asserts passed. If one or more failed, I throw an Exception of my own so the test will show up as Failed in the Test Results window.

An enhancement would be to add a finally block to the try/catch, to do any necessary clean up after a failed step during the test run. The app I was testing is pretty simple and didn’t need anything extra.

Here’s the result of a failed test run in the Test Results window:

clip_image001
Click to enlarge the image. . .

In the Test Results window you’ll need to double click on the row for the failed test (highlighted with red outline in the image), which will open the details of the test run.

The specific errors will be in the “Standard Console Output” section of the test results detail window, as highlighted in the image below:

clip_image001[4]
Click to enlarge the image. . .

In this test run, both the Add and Subtract routines failed.

And that’s it!

Your Coded UI test will run to completion even if an assert fails, and you’ll also be able to see the individual errors.

Here’s the SAMPLE code that I created to do it:

//
// NOTE: This code is for illustration purposes only and is
//       provided AS-IS, with out any warranties or support.
//

        [TestMethod]
        public void CUITestCalculatorRoutines()
        {
            // Original test method calls generated by the Coded UI recorder
            //this.UIMap.CUITestAdd();
            //this.UIMap.Assert5plus6Works();
            //this.UIMap.CUITestSubtract();
            //this.UIMap.Assert4minus2Works();
            //this.UIMap.CUITestDivide();
            //this.UIMap.Assert8dividedBy2Works();
            //this.UIMap.CUICloseCalc();

            //————————————————-
            // Modified test method calls

            // Need to keep track of any failed assertions so I can throw
            // an exception at the end of the test. That way it will show up
            // as a failed test in the Test Results window.
            bool bAllTestsAreGood = true;

            //———————————————
            // Testing the Add routine
            this.UIMap.CUITestAdd();
            try
            {
                this.UIMap.Assert5plus6Works();
            }
            catch (Exception ex)
            {
                Console.WriteLine(“ERROR in Addition routine!!! ” + ex.Message);
                bAllTestsAreGood = false;
            }

            //———————————————
            // Testing the Subtract routine
            this.UIMap.CUITestSubtract();
           
            try
            {
                this.UIMap.Assert4minus2Works();
            }
            catch (Exception ex)
            {
                Console.WriteLine(“ERROR in Subtract routine!!! ” + ex.Message);
                bAllTestsAreGood = false;
            }

            //———————————————
            // Testing the Divide routine
            this.UIMap.CUITestDivide();

            try
            {
                this.UIMap.Assert8dividedBy2Works();
            }
            catch (Exception ex)
            {
                Console.WriteLine(“ERROR in Divide routine!!! ” + ex.Message);
                bAllTestsAreGood = false;
            }

            this.UIMap.CUICloseCalc();

            // Now we check to see if all the tests were good. If one or more assertion
            // failed, this assertion will fail and the test run will be marked as Failed.
           
            Assert.IsTrue(bAllTestsAreGood, “An Assertion failed during the test. See the Standard Console Output for details.”);

        }

Helpful?

3 thoughts on “Coded UI test–keep on trucking

  1. Madhu

    Hi..
    I tried your approach. But I get an Exception for Assert.IsTrue(). How To handle that exception. It stops execution of other test methods.

    Regards
    Madhu

    Reply
  2. jimblizzard Post author

    Madhu, just do something similar to what I did in the code sample above: put the Assert.IsTrue() in a Try / Catch block. When you get to the end of your routine, check to see if you had any errors.

    Reply

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s