Test Suite is not stopping when an error is reported

Ask general questions here.
kh123
Posts: 10
Joined: Mon Aug 20, 2012 9:39 am

Test Suite is not stopping when an error is reported

Post by kh123 » Fri Dec 14, 2012 10:59 am

I am attempting to automate the following process
Open a screen in the app
Interact with the screen which runs a data import from either a text file or an sql data base
Restore the text fil /database so that the process can be repeated continuosly
However if there is an error I want the test to stop

1 I have set each test case in the test suite to Error Behaviour stop
2 One of my colleagues wrote a piece of code which coudl be copied into Program.cs which re runs the test suite again and again until you pause so I've used this
3 I've added new Ranorex exceptions in my code modules so that if the script fails it should generate an exception

When I ran the test suite the test would stop at the failure but then restart. So my initial thought was to add a try catch for Ranorex Exception. Then I added a setting to that the initial run would only start if x = true and then within the try catch reset the value to false

However finally having debugged it appears that the Ranorex exception isn't even getting into the catch hence the suite keeps running. What have I done wrong? I'm sure it's something obvious but I'm too close to spot it
Thank you
Example of the Program.cs code

Code: Select all

bool continuerun= true ;
           do
            {
            	//Output to the console how many iterations through the CSV imput file             	//Otherwise you have to abort the test to see how many iterations were performed
            	IterationCount ++;
            	Console.BackgroundColor = ConsoleColor.Magenta;
            	Console.WriteLine("Starting Iteration " + IterationCount.ToString());
            	Console.BackgroundColor = ConsoleColor.Black;
            	
		            //run the Solution
            		try
		            {       	
		                error = TestSuiteRunner.Run(typeof(Program), Environment.CommandLine);
			            Thread.Sleep(1000);      
		            }
		            
		            catch (ThreadAbortException)
		            {
		            	error = -99;
		            	Console.WriteLine("Thread aborted");
		            	 continuerun =false;
		               	break;
		               	
		            }
		            catch (RanorexException e )
		            {
		            	 error =-1 ;
		            	Report.Error("Ranorex exception occurred: "+ e.ToString());
		              
		                continuerun =false;
		                break;
		            }
		            catch (Exception e)
            		{
	            		Report.Error("exception occurred: " + e.ToString());
	            		
		            		            	
		            	//if testrun aborted via Pause key
		            	if(e is ThreadAbortException)
		            	{
		                   	error = -1;
		                   	Report.Error("User has aborted thread: " + e.ToString());
		                   	continuerun = false;
		                   	break;	
		                   	
            			}
		            	else
		            	{
		            	//otherwise report an unexpected exception
		                Report.Error("Unexpected exception occurred: " + e.ToString());
		                error = -1;
		                continuerun =false;
		                break;
		               
		            	}
		            }	           	            

					finally
					{
					}

			//Stop looping indefinitely if the user has quit the test run (via Pause key)
			if (TestSuiteRunner.RunAborted == true)
			{
				continuerun =false;
				//Teardown.Start();
				break; //Quit the While Loop
				
			}
           }
			while (continuerun!=false);
        
		return error;
            
	

Example of Ranorex Exception

Code: Select all

if ((Record)==(0))
         			{
           			repo.OperationDataDataImport.OperationDataContainer.ButtonCancel.Click();
            		Report.Log(ReportLevel.Failure, "Fail","There are no Records for data import");
            		throw new RanorexException("Test is stopped due to reported error");

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Test Suite is not stopping when an error is reported

Post by Support Team » Mon Dec 17, 2012 12:17 pm

Hi,
kh123 wrote:However finally having debugged it appears that the Ranorex exception isn't even getting into the catch hence the suite keeps running. What have I done wrong? I'm sure it's something obvious but I'm too close to spot it
You're right, the exception will be caught before getting to your catch block.

I would suggest to use a static variable, let's name it testSuiteStop, which you can set to true before throwing the exception.

Now you can check the value of the mentioned static variable testSuiteStop and set your variable continuerun to false if the value equals true.

Regards,
Tobias
Ranorex Team
.
Image

kh123
Posts: 10
Joined: Mon Aug 20, 2012 9:39 am

Re: Test Suite is not stopping when an error is reported

Post by kh123 » Mon Dec 17, 2012 12:57 pm

Thank you Tobias
I tried adding a variable and the suite stopped as required

Kind regards