Execute piece of code at the end of every Test Case without using Teardowns

Class library usage, coding and language questions.
DomantasK
Posts: 9
Joined: Mon Mar 06, 2023 11:18 am

Execute piece of code at the end of every Test Case without using Teardowns

Post by DomantasK » Tue Jul 04, 2023 8:54 am

Hi all, I want to execute a piece of code at the end of every single Test Case within test suite. My Program.cs file looks like so:

Code: Select all

       public static int Main(string[] args)
        {
            Keyboard.AbortKey = System.Windows.Forms.Keys.Pause;
            
            int error = 0;

            try
            {
                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)
        {
            if (TestSuite.Current.Parameters["IsTestSuiteMappable"] == "True")
            {
                if (TestSuite.CurrentTestContainer.IsTestCase)
                {
                    if (IsFirstTestCase)
                    {
				// My logic here
                    }
                    else
                    {
				// My logic here
                    }
                }
            }
        }
The problem with this piece of code is that it is executed at the end of Recording/Code modules as well as at the end of Setup/Teardowns and Smart Folders inside of the Test Case. I want it to be executed only once at the end of the Test Case. Using Teardowns for Test Cases is not applicable solution for me.

DomantasK
Posts: 9
Joined: Mon Mar 06, 2023 11:18 am

Re: Execute piece of code at the end of every Test Case without using Teardowns

Post by DomantasK » Wed Jul 05, 2023 2:20 pm

Up!

DomantasK
Posts: 9
Joined: Mon Mar 06, 2023 11:18 am

Re: Execute piece of code at the end of every Test Case without using Teardowns

Post by DomantasK » Wed Jul 12, 2023 9:53 am

Did someone manage to find documentation on such functionality?

csaszi89
Posts: 40
Joined: Mon Jan 17, 2022 12:10 pm

Re: Execute piece of code at the end of every Test Case without using Teardowns

Post by csaszi89 » Mon Jul 24, 2023 12:05 pm

Hey,
this event handler seems to be working (only called when the EndActivity arrives from a TestCase)

Code: Select all

static void ActivityStack_Instance_EndActivity(object sender, EventArgs e)
{
    if (sender is TestContainerActivity testCaseActivity && testCaseActivity.ContainerActivityType == TestContainerActivityType.TestCase)
    {
        // YOUR CODE
    }
}