Forum Discussion

j416's avatar
j416
Occasional Contributor
14 years ago

DUnit test + try/finally

Hi,



I'm trying to get my TestedApp, which is a DUnit GUI, to terminate after a completed run. It works well if all DUnit tests pass, but if any of them fails, it seems the finally clause (see below) is not reached and the application remains running.



How can I solve this?





procedure CloseApp;


var global: OleVariant;


begin


  global := TestedApps.Global;


  if not global.Close then begin


    global.Terminate;


  end; 


  global := Sys.WaitProcess('Global', 0); 


  if global.Exists then begin


    global.Close;


  end;


end;






procedure DUnitTestRunner;


begin


  CloseApp;


  try


    TestedApps.Global.Run;


    DUnitTests.DUnitTest.Execute;  


  finally


    CloseApp;


  end;


end;




Thank you,

Johan

4 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Johan,



    What if you add one more pair of try/except clauses?

    I.e. :

    procedure DUnitTestRunner;

    begin

      CloseApp;

      try 

        try

          TestedApps.Global.Run;

          DUnitTests.DUnitTest.Execute;   

        except

          Log.Error(ExceptionMessage, ExceptionMessage);

        end;

      finally

        CloseApp;

      end;

    end;
  • j416's avatar
    j416
    Occasional Contributor
    Hi Alexei,



    Thank you for your suggestion. Unfortunately no luck with that. Neither the except section nor the finally section seems to get called. Weird.





    Johan
  • Hello Johan,


    Your situation is different from the one when an exception was thrown. In your case, the script stops because an error message has been posted to the log. So, the finally clause will not be triggered just because TestComplete stops the test earlier. You can make TestComplete continue playing the test even if an error message has been posted to the log, so TestComplete will execute the finally clause. To do that, disable the Stop On Error setting.

  • j416's avatar
    j416
    Occasional Contributor
    Thank you Alex!



    That would explain it. I haven't confirmed what you say yet, but I am quite sure you are right. Problem solved.





    Thank you very much.



    Johan