Ways to run multiple testng.xml files | from code or testng.xml

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back so in our selenium with java series we are seeing the test ng features so we have seen couple of test ng features like the annotations attributes and also how we can use the test ng as an exception as well so today we are going to see how we can run multiple test ng xml files so we have seen the single test ngxml right how we can run we can just go to the single test testng.xml we can just do a right click and run that by using the plugin testnj plugin be it your eclipse or in intellij you can execute your test cases right but what happens when you need to run multiple tests in gxml files let's say that you have one xml file for your sanity for your smoke and then regression then how do we execute them do we have any options so that is what we are going to learn today so for this i will create a new package and let me just give this one as multiple test strangely xml okay some name i'm giving okay this is to be under this click on okay refactor okay so now under this package i will be copy pasting all these three things and here let me just make sure that couple of the test cases to fail so i'll say int i equal to 1 by 0 something i'm giving here and this one let me throw new skip exception fine okay so now i have two test cases i mean it means that i have five test cases but there are two files are there now let's do one thing how we can run so we can run right click on this and run this particular test case by using this right click into the code editor and if you want to run this second test case also it will say right click and run this one what happens when you want to execute these two different classes you cannot just select all and then you can't just right click and run this one right always that is not a good approach even though you can execute it here so the best approach that testng is recommending that you need to have a testng.xml so for that let me create a tng xml file so this is already one template i'll just copy paste into this location and here i'll just at this moment i'll just say testng multiple dot xml file and here what i'm going to do i'm going to use that two different test cases right this dc1 and tc2 so for this what i'm going to do here that is actually multiple right multiple dot dc one and the same thing i need to do for this second test method as well so what i'm going to do i'll copy this one paste it here but this has to be tc2 okay now simple right what i can do i can just run these two test cases okay or the two java files which is having five different test methods and as you can see five test methods run successfully for an instance let's say that i want to keep this tc one in one xml file and tc2 in another xml file let me show you that so what i'm going to do here i'll again copy the same thing this one right click copy and then i'll say paste in this case i'll say multiple dc one so single one i'm taking here and in this case i will be just copying this one okay and that there will be another test case i will be creating copy and then paste it and then in this case i will say tc2 okay and in this case this has to be tc2 so let me tell you actually in detail now so i have two individual xml files are there one is test ng multiple tc1 which is only holding this java file there is another one test ng multiple tc2 which is holding the tc2 test case or the java file actually now these two xml files i can run it individually right like this and then i can come back to here and i can run this one as an independent one are there any other options where we can combine these two xml files i can execute it in one xml file yes you can do that so for this what you have to do that you have to come back to this another xml file you can create a parent xml file i would say so instead of classes see but you just see here what is the difference between these two actually so here this shoot is different right i mean this xml is holding one shoot this xml is holding one shot now here because i wanted to execute multiple classes that's why i have kept it under classes tag the same thing if i want to keep it under multiple shoots then what you need to do so simple thing what you have to do there you have to use there is a test ng what you call attribute is there let me show you that so something called as suit files so this will hold all the suit files now suit files are nothing but the xmls now here you can provide the path of each shoot file now suit files will have a single element that is just shoot file and give the path of that so you can say test ng you can see it is even giving you the intelligence also what exactly you want to keep it so here this is one and instead of this you can just keep it one short format and the same thing i can have another shoot file also that is nothing but the tc2 okay now instead of this class level you can just go here and do it and you comment this entire suit level here okay this is test level now you can see each suit will have this shoot files now this one you can now run it very clearly now i can just run this one and as you can see total five test cases run three passed one failed one skipped so like this now it is only not only limited to one or more than shoot files you can just keep on appending into this particular parent xml file okay which you can run directly fine so this is all about the files right now there is another way also which you can run it by using the code itself so test ng even gives you another feature where you can execute the xml shoots inside the code itself let me show you that so for this what we can do we can go back to this multiple test ngxml package and i'll create a new java class and i will just give a name runner multiple xml i'll just name something and now here what i'm going to do now i won't be writing a test method actually here i can write a main method or even you can write a test method also not a problem but because i want to create a public static void main let's see in this way so public static void main and it will say string arcs so this is the signature of main method right now inside this you need to create one array actually first thing so what is that area that has to be a string actually and give some name so xml files you can just give some name create an object of that as an array list now this array list will have all the string okay that's it so this is the way you can create one list and just import that it should be from java util make sure that now here what you can do you have to keep on adding the files okay what you will add some string it should accept right now that is nothing but you need to give the path of this particular xml file okay so what i can do i can just copy the name so you can even copy in this way control c and come back to here and now here you can just say this one okay now this is my first xml file the same thing i can add into list right multiple elements i can add or multiple strings i can add to this xml see so far this is pure java only i didn't even use the testng concept here so the main logic is that first create an error list which will hold all the path of the xml file okay now after that what you need to do you need to use the test ng class there is a test ng class you can see here now create an object of this okay equal to new test ng now once you create an object now this test change object will have something called as set test shoots you can see which is accepting the number of files or the suits see i told you already right the xml testng xml file is nothing but one suit now that is what here list of suits i need to give so that is nothing but xml files or else you can even give here as suits you can give inside this shoes also okay so now i have set the test energy suits into the test change object now what i can do by using this test change object i can do a run actually here you can see you can even run so here one of one or more what you call strings as the test ng xml file you add it then you set that and then you can execute it it is that simple now let's do one thing because this is a main method even i can run it from the code editor right so let me run so as you can see here let's go here so this is my first one actually you can see there are two test cases run now in that one god failed one got skipped now how do you know that you can just go back here you can see this is got failed one got failed one got skipped now the second one you can see here see you can see two sets of testing gxml got run here now this is my second set of test ng class file and here you can see three test cases got passed because all three are past scenarios only how do you know so for this actually you can see this is actually my suit one and here you can see suit one one now you can see this is the suit one one and this is my shoot one so two sets of output you are getting here in this way so this is the way you can use even the test ng class or class object and you can run it from the code itself so these are the two different ways you can execute it now there is a question comes okay we are all executing from here run this one or else if you are going to this parent xml file you can even run in this way right what if you want to run it by using some scheduler in that case what you can do you can take this testnj underscore multiple dot xml or the parent xml whichever you are creating you can just use it in pom.xml in this your file plugin or else you can use the maven command line as well okay mvn space test hyphen d test equal to the path of this particular test ng xml file okay so these are the two ways you can run it if you want to run it from your jenkins or azure devops or any other ci cd pipelines as well okay so that's that's pretty much it for today so hope this session is helpful stay tuned and do subscribe to this youtube channel we will be seeing some more test energy features and also preparing a selenium with java framework in our upcoming tutorials thank you for watching
Info
Channel: qavbox
Views: 158
Rating: undefined out of 5
Keywords:
Id: floBxhqMB40
Channel Id: undefined
Length: 12min 35sec (755 seconds)
Published: Tue Aug 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.