how to execute a piece of code for all testcases

Class library usage, coding and language questions.
palbaret
Posts: 26
Joined: Wed Jun 27, 2012 4:21 pm

how to execute a piece of code for all testcases

Post by palbaret » Fri May 24, 2013 6:28 pm

hi every one,

I want to execute a piece of code before and after the execution of every testcase.

For example, at the beginning of a testcase i want to call a webservice with the testcaseId, and also when the testcase ends, I want to call another webservice with the testcaseId and the test result.

these calls should be done for all testcases

In the ranorex API, how can I implement a specific class to do that, is there a specific interface to implement and if it is the case, how do I use it?

thanks in advance for your help.

patrick

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

Re: how to execute a piece of code for all testcases

Post by Support Team » Tue May 28, 2013 1:19 pm

Hi,

You could for instance add Code Modules to your solution where your methods can be implemented and then you could add these modules to the setup and the teardown section of each of your test cases.
For more information please take a look at the following links:
Code Modules and General Structure of a Test Suite and its Test Cases.

Regards,
Markus
.
Image

palbaret
Posts: 26
Joined: Wed Jun 27, 2012 4:21 pm

Re: how to execute a piece of code for all testcases

Post by palbaret » Mon Jun 03, 2013 10:45 am

Thanks for your answer but it doesn't completely fit my concern.

for example I create 2 code modules TestcaseSetupModule and TestcasetearDownModule. Inside these modules, I will put all my specific code.

My questions is how I can execute each time these modules?

These modules will be mandatory in our test strategy and I don't want to insert them inside all our testcases.
When a test engineer develops a new script, he should not take care of these 2 modules, but when he' execute his testcase, these modules must be executed before and after the test execution.

Maybe we can modify, implement an interface in order to add these module during execution?

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

Re: how to execute a piece of code for all testcases

Post by Support Team » Tue Jun 04, 2013 3:39 pm

Hi Patrick,

It is unfortunately not really possible to execute them without changing anything in the test suite or in one of your modules.
Is there a reason why you won't copy them to your test cases respectively why you don't want to use the setup/teardown region?

Regards,
Markus
.
Image

palbaret
Posts: 26
Joined: Wed Jun 27, 2012 4:21 pm

Re: how to execute a piece of code for all testcases

Post by palbaret » Thu Jun 06, 2013 2:14 pm

My reason is:

I want to implemente an integration with a code coverage tool.
This tool record my actions on the server and provide me a file with all dll/methods ran during my tests.
To do that, before each testcase, I have to send a command line to this tool at the beginning at the end of every testcase.

One solution is to add for each testcase a setup and a teardown module, but as I have a lot of testcase (around 200), I'm looking for something generic.

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

Re: how to execute a piece of code for all testcases

Post by Support Team » Fri Jun 07, 2013 3:31 pm

Hello,

Thank you for the information.
As workaround you can implement an EventHandler in the Program.cs file in order to execute some code at the beginning of each Test Case.

This is an example how you can implement an event handler in the Program.cs file:
public static int Main(string[] args)
{
 // Uncomment the following 2 lines if you want to automate Windows apps
 // by starting the test executable directly
 //if (Util.IsRestartRequiredForWinAppAccess)
 //    return Util.RestartWithUiAccess();
 Keyboard.AbortKey = System.Windows.Forms.Keys.Pause;
 int error = 0;
 try
 {
     ActivityStack.Instance.BeginActivity += new EventHandler(ActivityStack_Instance_BeginActivity);
     ActivityStack.Instance.EndActivity += new EventHandler(ActivityStack_Instance_EndActivity);
     error = TestSuiteRunner.Run(typeof(Program), Environment.CommandLine);
 }
 catch (Exception e)
 {
      Report.Error("Unexpected exception occurred: " + e.ToString());
      error = -1;
 }
   return error;
}
 
static void ActivityStack_Instance_EndActivity(object sender, EventArgs e)
{
    // YOUR CODE
}
 
static void ActivityStack_Instance_BeginActivity(object sender, EventArgs e)
{
  var testCaseActivity = sender as TestCaseActivity;
  if (testCaseActivity != null)
  {
     string name = testCaseActivity.TestCaseName;
     var testCase = TestSuite.Current.GetTestCase(name);
     
     // YOUR CODE
  }
}
Please note, this is a code which cannot be found in the API documentation and that's the reason that we will not provide any support for it. It is just an example how to implement an event handler.

Regards,
Bernhard
.
Image

palbaret
Posts: 26
Joined: Wed Jun 27, 2012 4:21 pm

Re: how to execute a piece of code for all testcases

Post by palbaret » Wed Jul 03, 2013 6:52 am

For VB.net I use the following code
AddHandler ActivityStack.Instance.BeginActivity,AddressOf ActivityStack_Instance_BeginActivity

thanks for the sample