Hello all welcome back to the next one. In this one we'll see add the red before test and other it after test annotations. So let me bring up Eclipse. And we have created couple of classes here. So basically, we created two test classes in red before sweet annotation lecture. So test in G test class to understand it as class one.
They have their own ad read before and after class methods at the rate before and after method and couple of test methods inside them. So they they both have the same configuration. And then they both extend the base test suite. This is the one that we created. And we implemented before Sweden after Sweden, these method these this class, right? And then we created a regression suite dot XML, but I copied it to regression test dot XML.
It's a exactly the same. I copied it and I created a new file because I'm going to make some changes. So sweet, we understand now. Now inside the test suite, there can be multiple test classes as we understand. There is one more concept of test. Interestingly, right, this is a test tag.
In fact, this test tag consists of classes inside it right? So which means that we can have multiple test tracks inside a test and G Suite. So what I'm going to do here is I'm going to copy this here. So now we have two test tags here. And let me just name it as application test one application test two. And that's about it.
Maybe let's just remove and remove this one, maybe just let it be. So now we have two test tags here. And we have to test classes inside each test tag. And it's not necessary to have Same classes, I'm just copying it same here, I can actually remove the first one from this tag here and remove the second one from here. So it depends on our configuration use. Usually we will have different tests for different configuration, most likely we may run same test classes in both the test tags.
Depending on different configurations, we might have different classes also in different stacks. This could be a smoke test, and this could be a regression test and the suite is the complete suite. So currently, I have named as regression test suite, I can see it as like complete test suite. And this one can be regression. And the first test could be smoke test. So they can have different test classes also.
And they can have same test classes also it completely depends on what we are trying to run as a part of our automation framework. But leaving that aside, if we have multiple tests tags, then we might have a need to actually create a beef to actually create some functionality, which is common to both the test tags, right. And we want that functionality to execute before the test tag starts and after the test tag ends, and same thing for every test tag. So it can have like multiple tag test tracks here in this XML file. It can have multiple test tags here. So to implement those kind of common functionality, we may need one more kind of annotation which is already existing in testan G, and it's known as at the rate before test and after test.
So since it's again a kind of a common functionality, we prefer to implement it in the base test suite. We can implement it in separate classes also, but obviously it does not make a lot of sense because this is something Just comment to a test tag not common to class, right? So sorry, not specific to class, if there's something specific toward test class, then it makes sense to put it inside the test class. But if it's something common and it can span over multiple classes, then obviously it makes sense to put in some common location. And for us, the common location is our base test suite, because all our test classes are extending this. So basically, everything inherits from this base test suite so we can implement the common stuff here.
What I'm gonna do here is I'm going to copy this, these two. So I can actually just write it here and I will change the name so I'll call it as before, test, and I'll change it as after test and I'll change this hair also. And then I'll change the annotation. So annotation is something I want. I show you so I'm leaving before and then I'm gonna hit control space, it should show me all the options. So we're gonna pick before test and please always make sure that it shows from our dot test nj dot annotations.
If you have j unit also JAR file j unit JAR files added to your project. Don't be misguided by that always make sure you have correct emulation. So I'm gonna do the same thing for after test also. So after test and this is all done. So now what we have here is before class after class before test after test and before sweet after sweet. What I actually want to do here is reduce the number of methods maybe or you know what, let it be let them be lot of methods.
Maybe I would just read, like delete these slashes because it creates a lot of new lines. Somehow I feel like it's not needed. To have all those new lines. So we'll have a cleaner output. So it's just a little easy for you guys to see because now we have too many things. So now here, I'm just gonna run this XML file regression does dash test dot XML.
And if I right click, Run As always, once more, one more thing, guys. So you cannot use these before test and before sweet. Especially these two, before test and before sweet after test and after. So these annotations with just directly with the test classes, you cannot run them by right click and run as test in G test. They always have to be used when you're using the XML file. Because it will always be in effect when we are running tests as a suite.
Right click run as test in G Suite and we'll see the output So, what happens here, first base test suite before suite was run and then before test was run and then before class was run from base test suite. So, now you see the order before suite because we it is the parent and then before test and then before class, so it's hierarchical right now, then, the methods were run from specific classes. So, test class one before class. And then before method, test method, obviously, there will be after method after the test method. And then before test method two starts there will be before method, test method two after method and after class from the test class one. Then from base test to it after class is executed and then after test is executed.
Point to be noted here is after sweet is not executed because it will be run after everything is done right Now again before test is executed before class and then same thing happen to test class to test class to specific methods are executed. And then after class is executed from base test suite, then based test suite after test and then finally only once based as sweet after sweet method is executed. So, we can see we have total test as for because obviously, we only had four test methods and this counts the test methods and we can see the result here. Both the test are executed now you can see that it is showing other than classes it is showing two different test application test one and application test two and that's what we have here right. So, that's pretty much it. That's what I wanted to show you I wanted to cover that we can have before test and after test also for test tags inside our XML files.
That's about it, please practice and let me know if you have any Thanks