Handling failing tests

Dec 18, 2009 at 10:30 AM

I'm using Resharper and NUnit to drive my tests and when they fail, as in fail to find a component (NullReferenceException) not on an Assert, the application window is kept open. This means I sit and stare at my screen for a while before I realize that I should check to see if they failed.

Is this the normal behavior or can I configure it to close the application somehow?

 

Dec 18, 2009 at 10:43 AM
Edited Dec 18, 2009 at 10:44 AM

Hi

yes you can :)

 

use the TearDown to close the application

 

we use

 

mainWindow.Dispose();

application.Dispose();

 

if this don't work you can search for the process of the application and use process.kill. (this should be only used if the first way don't work)

in some cases is is also useful to search for all modal windows and close them first. and click on the titlebar of the mainwindow to get rid of opened context Menu's

Jan 29, 2010 at 9:15 AM

Thanks Throndorin! It took a while for me to actually sit down and try it, but it worked well when I did.

Now, I'm just curious if this is the proper way to use SetUp and Teardown. I'm using application as a private variable. Is there a preferred way to do this?

<TestFixture()> Public Class verify_service_object_control

    '(...)
    Private application As Application

    <SetUp()> Public Sub SetUp()
        application = LaunchApplication()
    End Sub

    <Test()> Public Sub Service_checklist_should_contain_all_elements()
    '(a bunch of code)
    End Sub 

    <TearDown()> Public Sub TearDown()
        application.ApplicationSession.Save()
        application.Kill()
    End Sub

 

Jan 29, 2010 at 2:34 PM

Hi

 

we use a static class to handle some special things called <ToolName>Management

 

in this class we have Properties for Application and the MainWindow

 

/// <summary>
/// Starts the My Tool
/// </summary>
/// <returns></returns>
public static void TestSetup()
{
  string toolPath =
	string.Format(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\MyTool\MyTool.exe");
  MyApplication = Application.Launch( toolPath );
  //Get the ETS4 window
  MainWindow = MyApplication.GetWindow( "My Tool", InitializeOption.NoCache );
  if( MainWindow != null )
  {
	
  }
  else
  {
	Assert.Fail( "Test could not be run, MainWindow is null" );
  }
}

/// <summary>
/// Shut down the ETS 
/// </summary>
public static void TearDown()
{
  try
  {
	Thread.Sleep(200);
	List<Window> windows = null;
	if (MainWindow != null)
    {  
		//Take care that there is no open context menu
        MainWindow.TitleBar.Click();
        try
        {
          windows = MainWindow.ModalWindows();
        }
        catch (ElementNotAvailableException)
        {
          // seems to be a internal problem if the application is to fast, 
          // so catch this exception
        }
        if (windows!=null && windows.Count > 0)
        {
          foreach (Window dialog in windows)
          {
            if (dialog != null)
            {
              try
              {
                Button ok =
                  (Button)dialog.Get(SearchCriteria.ByControlType(ControlType.Button).AndByText("buttonOK"));
                ok.Click();
              }
              catch (Exception)
              {
                dialog.Close();
              }
            }
          }
        }
        Button exit = MainWindow.TitleBar.CloseButton;
        if (exit.Enabled)
        {
          exit.Click();
        }
        else
        {
          WaitHelper.WaitForButtonIsEnabled(exit);
          exit.Click();
        }

        if (_sEtsWindow != null)
        {
          MainWindow.Dispose();
          MainWindow = null;
        }
      }

      if (MyApplication != null)
      {
        MyApplication.Dispose();
        MyApplication = null;
      }
    }
  }
  catch( Exception e)
  {
	Trace.WriteLine("Exception: "+e.Message);
  }
}