How to create the test dependencies in TestNG - P14 | TestNG | Testing Framework |

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys i'm yada grady and welcome to my channel hvr tutorials in this video i will show you how to create the test dependencies in test ng so this is the agenda for today's session guys so first we will see what is a test dependency then why we need the test dependencies that means why to use the test dependencies and then we will see how to create the test dependency on another test and then some special scenarios like what happens when priority is set to the test methods okay so for example one test method is having some priority set already and i want to use the test dependency on that method so what happens in that scenario which one will it will be taken for highest priority okay and next what happens when dependent test is failed so when i say test dependency obviously one test is dependent on another test right so that means the if the dependent test is failed what happens to the original tester okay so will it fail or will it skip or will it just pass okay so what happens in the scenario that also we are going to see and then what happens when the dependent test is ignored or skipped or deleted okay so any of these can be happened right so for somehow i have added the ignore annotation to the dependent test so what happens in that scenario the dependent test will be ignored right so when the dependent test is actually ignored what happens to the original test so that we are going to see and also somehow if the dependent test is kept in what happens to the original test and somehow i have removed okay so maybe knowingly or unknowingly i have removed the test case from the test suit basically okay so let's say i have 10 test cases and my 10th test is actually dependent tested so then i have removed the tenth test okay then what happens in that scenario okay so that also we are going to cover inside this video guys so we are going to cover all these five points inside this video so without wasting any time let's get started so first of all as always we are going to look at three points what why and how okay so first what is a test dependency and why we need the test dependencies so for this i'm going to give you one scenario guys okay i'm going to explain one scenario so that you will understand this concept very easily okay so let's say i want to create one shipment and then track the shipment and then cancel the shipment okay so let me just first write down here then i will explain you okay so create shipment and then track shipment and after that cancel shipment okay so let's say i want to perform these three operations okay so let me explain the scenario first of all so i want to create a shipment so i'm using the fedex for example as a provider so i'll just log into the fedex website and i want to create a shipment from hyderabad to new delhi okay so once the shipment is created what happens it will generate a tracking number obviously right so even if you go to any amazon or flipkart or anywhere if you try to you know purchase any any item so it will create a shipment internally right so it will create a shipment from the amazon warehouse to your address okay whatever the address you have mentioned so once the shipment is created it will generate a tracking number right so you will have order id and you will have a tracking number also with the tracking number you can track your shipment right so the same thing i want to do but i want to do this inside a separate test okay so this is one test and this is another test and this is another test basically okay so i have three test cases in one in the first test case i want to create the shipment and store the tracking number in some place and then in the second shipment i mean in the second case i want to use that stored tracking number and i want to track the shipment and in the third test i want to use the stored tracking number and i want to cancel the shipment okay so let me just write it here tracking number is required here as well as here also right so these are the test cases i want to actually create let's say okay so this is my scenario guys okay so if you closely observe this one right so here i want i'm creating the shipment and then tracking the shipment and cancelling the shipment so what is the relationship between all these things so basically the tracking number is the relation right so between this positive test and second test tracking number is the relation and between the first and third also tracking number is the relation so if the tracking number is actually not generated in the first test then this is waste of time okay if i try to execute the cancel shipment it's a waste of time anyway it is going to fail okay so somehow okay somehow the create shipment test is failed so what happens in that scenario the tracking number will not be generated am i correct so when the create shipment is actually failed the tracking number will not be generated so if the tracking number is not actually generated you cannot track the shipment and you cannot even cancel the shipment also right so in that scenario if i try to execute all the three test cases what happens all the three test cases will be failed okay but it's a waste of time to execute second and third when first is failed so do you see that point guys so if the first test is actually failed you don't require to execute the second and third it's a waste of time actually so anyway these two are going to fail because the tracking number is not generated in the first step okay so you see the point here the point is the dependency that means the second test and the third test is actually dependent on the first test okay so when the first test is successful that means if it is passed then only the tracking number will be generated and that tracking number can be used inside the second and third test case right that means the second test and third test is dependent on the first tester okay so that is the main concept here guys okay so that means here we are creating a concept of dependency okay so that is the basic concept of the test dependency so whenever you want to depend on another test then we need to use the concept of test dependency okay so that's you underst that that's what you understood now right so what is a test dependency test dependency is a concept in which you can wait or you can depend on another test basically okay and why we need the test dependencies obviously so for example okay let me just show you in the real time guys what happens okay so let me just create a class so i have already created one class test dependency so inside this i want to add three test methods okay because here i have three test cases right so for each one i want to add one test method public void so the first test method will be create shipment and inside this i'm going to simply use the sysout guys okay so i'll just put this one fine to make this as a test ng test i need to add the at the right test annotation right so let me just import this from the orgy.testng.annotations so this is basic guys i'm just creating the three test cases okay so this we have already done multiple times in our earlier videos okay so let me just copy the same and let me change the name here so the second one will be track shipment let me just change this one also track shipment and the third one is cancel the shipment these three are actually dependent test cases not the individual test cases basically okay so here uh i'll do one thing i'll just try to implement a simple logic here okay because i'm not going to add any real-time selenium core i want to simply put some real-time code here okay real-time means some meaningful code here okay so let's say static in tracking number okay or maybe the tracking number is going to have the alphanumeric characters also sometimes right so i'll just use the string here and i'll make this one as null first of all okay so by default this will be null empty okay so after that so once this is successfully executed i want to actually change the track number okay i want to just assign some track number random track number let's say okay so abc 1 2 yh something like this okay so basically i mean in real time what happens you need to perform the operations in real time here and once the track number is generated you are going to store the tracking number into this field okay so directly i'm assigning some value here without doing all the selenium steps here okay so next here if the tracking number is actually null okay so let me just write the scenario if the tracking number is not null then i want to execute this one this out statement that means i want to actually track the shipment okay so if the tracking number is null then i want to throw the exception guys okay so let's say i want to throw the exception new exception with some message saying that invalid tracking number okay so i will explain you guys okay let me just add the code here so that i can explain you easily and clearly basically okay so the same concept i'm going to just add here okay so let me just replace this one here okay so here again i need to throw the exception so it's very simple guys okay so here i have three test cases and the first one is create shipment and the second one is a track shipment and the third one is cancel the shipment okay so once the create shipment is actually successfully executed then the tracking number will be generated okay so i am assigning this tracking number here and if the tracking number is not null then i'll just go ahead and track the shipment and if the tracking number is actually null then i'm going to through the exception okay in the real time also you cannot use i mean if the track number is null you cannot track the shipment right so that's why i am going to just throw the exception and even the same applies to the third one so if the tracking number is not null i'll just cancel the shipment if the tracking number is actually null i'll just simply throw the exception saying that invalid tracking number okay so let me first execute this test so i'm not going to add any priority or anything first of all and i'll simply execute the test so this is already known concept guys okay so when i directly execute the step i mean when i directly execute this test test class what happens first cancel shipment is executed then create shipment then track shipment but actually why cancel shipment is executed without create shipment why cancel shipment is executed so this is the order i want to actually execute the test cases but it has executed in a different order right why did you remember the concept of priority so we have already discussed the concept all right so if you don't set the priority to any test method internally it will pick the priority at zero to all the test cases and internally the test ng follows the ascii order okay so the testing g follows the ascii order to execute the test methods okay so in the ascii order cancel shipment comes first then create shipment comes per second and then track shipment comes okay so that is why it has executed in this in this particular order but i want to execute the test cases in this order okay so for that let me just quickly add the priority here so if you don't know what is the concept of priority please watch my earlier video guys okay so in one of my earlier video i have already clearly explained about the priority okay so here the priority becomes the two and here this will be three okay first i want to create the shipment then only track the shipment then only cancel the shipment so now i have created in a proper order okay when i assign the priority it will execute in the proper order right so let me just execute this so don't feel like i am dragging the concept guys okay i want you to understand the concept very clearly okay even if it is taking five minutes or ten minutes more you need to understand the concept clearly okay so now you can see create shipment is executed first then track shipment then cancel shipment okay so that means it is executing in the proper order that is fine okay so now i want to fail the first test case okay somehow i want to fail the faster test case so that the tracking number is not going to generate okay so how do you fail the test case you can simply write this uh throw exception statement or you can actually add any scenario i mean you can actually add any code which can throw the exception okay so what i'm going to do is a simple one guys okay so you all know this one so i'm going to divide one value with zero so if you divide any value with zero what happens divided by zero exception will throw right so after that this piece of code will not be executed am i correct so when you get any exception inside this test method so after this exception whatever the code is there that is unreachable code that means it is not going to be executed because i haven't actually surrounded this with the try cache block so that means when the exception is thrown this piece of code will not be executed that means the tracking number is not going to be generated okay so the tracking number is not going to be generated so when the tracking number is not generated what happens in this scenario the second and third test also will be failed right so let me execute this so you see all the three test cases are actually fail okay so failed three past zero and skip to zero right so first one is failed and second one is failed and third one is failed but why i am actually executing the second and third so when the first is actually failed i should not execute the second and third right why that is because the first test should generate the tracking number if it is generating the tracking number then only you can execute the second and third but if the tracking number is not generated if you are executing the second and third it's a waste of time okay so you should not actually execute like that so for that we are going to introduce a concept called test dependency okay so that means we are going to create a relationship between the first test and second test and the first test and third test okay saying that this test is dependent on this test okay the track shipment is dependent on create shipment cancel shipment is dependent on create shipment like that we are going to create a relationship guys okay so in that scenario what happens if the first test is failed okay the second will not be executed it will simply skip that okay why it is like that because this test is actually dependent on this one so if it is dependent on this one if this is actually i mean the dependent test is failed you don't need to execute this one so that part testng will take care internally for that only we have the con concept called test dependency okay so when that i mean when the dependent test is actually fail here if you don't use a test dependency concept when the dependent test is failed we are i mean we are still executing all the other test cases also right but we should not execute these two things okay because this one is dependent on this one and this one is also dependent on this one so further we are going to use the test dependency concept so now you understood what is a test dependency and why we need to use the test dependency right so we need to avoid the other test cases which are dependent on the main test so if the main test is actually failed we need to avoid we need to skip the other test cases also which is dependent on the main test fine so i mean while showing you will easily understand guys okay maybe while framing the words it might be a little bit confusing but while showing the example you will easily understand so now the first point is clearly explained right so let me show you how to create the dependency now you understand what is the dependency and why we need to use a dependency so let us use the dependency now okay so here to use the test dependency we need to add one parameter called depends on methods guys okay so you need to add depends on method parameter so when you type depends so you will get these three things okay so here we are going to discuss only about depends on methods okay we are not going to discuss about the depends on groups so this point i haven't made clear right in the first so basically you can create the test dependencies on test cases as well as the test groups also okay so let me just write here test methods or test groups also so what is a test group that also we have already covered in our earlier video right so in test ng we have a concept called test groups so if you want to combine multiple test cases under one group then we will use the test groups concept okay instead of executing each and every test individually i can simply mention all these test cases inside one group and i can simply execute the group okay so basically that's a high level meaning of test group guys okay so if you want to know clearly please watch my previous video okay so you can create the test dependency on other test methods as well as other test groups also okay but in this video i'm not going to show the test groups concept okay because if i try to include the test groups concept also inside this one right this video will become very lengthy that's why i don't want to show you this one and make you confused okay so i'll show you the test group dependencies in other video guys so for now i'm going to show you the test dependencies concept on the test methods only okay so these are the test methods so whatever i have created here create shipment create track shipment cancel shipment this all are called as test methods right so now i'm going to show you the test dependency concept on test methods only okay so when you type depends here it is showing depends on groups depends on methods right so this one that depends on groups we are going to see in our next video so for now we are going to see only depends on methods so here you need to observe one thing guys depends on methods is accepting string of array why it is actually accepting the string of array that is because you can create the dependency on one test method or you can create the dependency on multiple test methods also okay it can be a single method or it can be multiple test methods also that's why it is actually taking the i mean taking uh as a string format i mean array format guys okay so when i say array you need to open the params i mean curly brackets and close the curve brackets and inside this we need to specify the values right so it's a string array basically this depends on methods is accepting the string type of array okay so what string value we need to pass we need to pass the string value of the dependent test method okay so let's say this track shipment is actually dependent on the create shipment okay so that means i need to pass this create shipment method name here okay so like this so if you have another test dependency you can simply add like this fine so it's just putting a comma and adding multiple things it's just a array guys okay so now i have created the dependency of this one on this one fine so now what happens if the faster test is failed this will not be executed this will be simply skipped there okay so it will not be executed and waste our time it will simply skip the execution fine and similarly i want to create the dependency for the third test method also right so let me just copy that so let me first remove the priority guys okay so i'm going to remove the priority here first of all and let's see because for priority we have a separate test right so i have removed the priority here and let me just execute this okay so this is how you need to create the test dependency guys okay you need to create the test dependency on test methods so you need to use one parameter called depends on methods inside the test annotation and you need to pass the values in the form of a string i mean string array okay so what you need to pass you need to pass the dependent test method so in this scenario this will be my dependent test okay so now let me just execute this test and see okay now you see we have zero pass one failed and two skipped so earlier when i did not actually use the dependency methods actually we have three values there is no skip actually right but now one is failed and the other two test methods are actually skipped okay which one is failed actually the create shipment itself is failed okay because i have intentionally failed it so when the create shipment is actually failed the track shipment and cancel shipment is directly going into the skipped okay they are not being executed so this is what we want guys okay this is what we want we don't want to waste our time by executing these two methods also when the first one is actually failed right so this is what we want actually out of the test engine okay so that is why test engine is actually introduced this concept called test dependencies okay so using this test dependencies you can actually create this relationship so when actual tester okay when this dependent test is failed this will be simply scripted it will not be executed okay so if this is passed what happens these two also will be executed so let me just comment this one that means this create shipment is going to pass okay so it will simply print this one and it will generate the tracking number so when this is passed this one and this one should be executed right so let me just execute this now you see all the three test cases are actually passed okay because the first is passed so these two methods are also passed okay so here let me tell you one thing guys so how this will be actually executed okay so now you understood what is the test dependency and everything so how it will actually execute so first it will come i mean the control comes to this one first okay so when the control comes to this test method so here i'm saying it depends on so answer test case that means this create shipment okay so immediately test ng will not start execution for this one st g will consider this one as a highest priority okay why because to execute this method first this method should be executed do you understand that so that is the dependency actually we have created right so to execute this track shipment method first create shipment should be executed okay so that's why when you when you add any depends on method uh parameter to any test method so it will take the method names from here and first it will execute those methods guys okay because those are the dependent tests okay so that's why it will first try to execute those dependent methods so once the dependent method is executed based on the status of this dependent method it will execute these methods okay so that you need to understand clearly fine that's why the control came to this test method first i mean this uh track shipment method and here it is dependent on create shipment that's why the create shipment is executed and based on the outcome of this create shipment the track shipment is executed and similarly control came to cancel shipment test method but this is dependent on create shipment it's already executed so that's why it is executing this one fine so now you understood what is a test dependency and how to create the test dependency also so if you want to add the test dependency on multiple things then it's very easy guys okay so for example let's say before canceling the shipment i want to create the shipment i want to track the shipment okay maybe these two things are dependent on this one okay sorry this is dependent on these two methods okay so in that scenario what happens i need to add another test method simply here fine so what exactly this meaning is so this says cancel shipment test case is actually dependent on create shipment and track shipment okay so if these two methods are actually executed then only execute this one like that we are saying okay so let me execute this now you see all the three test cases are passed because the first one is passed and the second one is passed because first and second is passed the third is also passed okay so somehow if i get an exception in the first one what happens so only one method will be failed and the remaining methods will be skipped do you understand that so the test create shipment is only failed and because this is fail this one and this one will go into skipped status okay so this is about the test dependency guys okay so now we have seen first and second point only okay so now let's see the third point so what happens when priority is set to the test methods okay so let me show you how it will actually look like so let me just comment this so now my first i mean this track shipment is dependent on this one right that means first the create shipment should be executed after that only track shipment should be executed okay so if you consider carefully right what is the priority for this one i mean if you want to actually write only with the priority what is the priority in this scenario first i want to execute the create shipment that means the priority for this one is one and then i want to execute the track shipment then the priority for this one will be two okay but what if i want to execute the track shipment only first and after that create shipment so how do i assign the priority in that scenario so i will simply write priority equal one here okay that means i am saying first execute the track shipment okay then execute the create shipment so i am assigning the priority as one to this one and priority as two for this one okay and let's see what happens in this scenario guys okay and anyway for this the priority i'm assigning as three okay so only after the first and second i want to execute this one so now i am saying the track shipment should be executed faster with the priority but let me execute and see so now you see all the three test cases are passed okay that is very good and if you observe here first create shipment is executed not the track shipment but i have set the priority as one to the track shipment and two for this create shipment right so based on the priority first track shipment should be executed and after that only create shipment should be executed but here it's vice versa okay that means it is not considering the priority okay even though you have set the priority it is not considering the priority because the priority is one for this one it is coming here to the track shipment but it internally checks whether this test is dependent on any other test or not okay so because this is dependent on another test first that test will be executed okay so when you use priority on top of the depends on methods the priority will be simply ignored the priority will not be considered the priority will be simply ignored fine so that you need to understand guys so what happens when priority is set to the test methods when using test dependency when using test dependencies if you set any priority that priority will not be considered that means it will be actually ignored the priority will be ignored it will consider only the test dependency concept not the priority concept there okay so the next one is what happens when dependent test is failed okay so now you saw right so for example if the dependent test is failed what happened the test case i mean this test is moving into the skipped status okay that's what we saw right so now you see now the first one is failed and then immediately this one and this one are going into the skip status right so this is what happens when the dependent test is failed but somehow my test cases are little bit different okay let's say just for the moment just imagine a different scenario where even though if your dependent test is failed you want to still execute the test okay so in that scenario what you will do so for example here this test is actually fail but even if this is failed i want to execute the track shipment so maybe i have a scenario like that okay so in that scenario what do i do so if i don't mention anything and if i simply leave it like this it will simply skip the test it will not even execute it okay but i want to execute the track shipment also so if you want to execute the track shipment also that means if the dependent test is failed and still you want to execute the main test then you need to add one parameter called always run so you need to add one parameter inside the test annotation that is always run so this is the parameter that you need to add guys so if you read here carefully if set to true that means if this parameter is set to true this test method will be always run even if it depends on a method that failed so that means if the dependent test is failed also this method will be executed okay so let me just add this one and make this value as true okay so now let me execute this so i know this concept is little bit confusing for you guys but if you understand all the things very carefully right so at the end of the video if you connect all the dots then you will easily understand the concept okay this is nothing a big deal so again i am going to give a clear explanation at the end of the video guys okay so now you see even though the first test is actually failed that means even though the dependent test is failed still we are able to execute the track shipment also that means the main test also okay earlier if this is failed we are not executing this one but somehow if i want to execute even though the dependent test is fail then we need to add one parameter called always run and we need to assign the value as true for that okay so when you ascend the value as true for that even though the dependent test is failed your main test will be still executed okay that's why you can see failed to and skip to one so the track shipment is actually executed but because the tracking number is not generated it internally failed again okay so this is the next concept that is always run attribute you need to add okay and after that what happens when dependent test is ignored or skipped or deleted so first we'll see ignored or scripted so what is meaning by ignored so let me just remove this and i can just remove the priority also because it's not at all required for me even though if i provide the priority it is not going to consider because the test dependency concept is invoked here so now i want to simply ignore the faster tester so how do i do that i'll just add the ignore annotation for the first test i want to execute the track shipment method but this track shipment is dependent on create shipment but internally i am saying don't execute the create shipment okay just ignore the create shipment so let me execute this and see what happens now you see nothing is actually executed guys okay path zero phase zero and skip also zero okay and we got some exception also what is the exception it says track shipment is depending on the method create shipment which is not annotated with at the right test or not included that means it might be skipped or it might be removed or it might be ignored okay so it is saying clearly that right so if you want to execute this track shipment whatever the dependent method is there that should be actually not ignored that means that should be enabled method okay it should be in a state to execute but here it's not in a state to execute okay i am explicitly saying that please ignore this test okay don't run this test like that i am saying clearly right so in this scenario if i want to still execute okay so or maybe somehow okay somehow i have removed this test so intentionally or unintentionally i have removed the test so now what happens so i want to execute the track shipment method but this track shipment method is exit i mean dependent on create shipment now the create shipment is not available so let me execute this test and see what happens so when the dependent test is ignored or skipped or deleted the same behavior applies guys okay now again nothing is actually executed and we are getting the same exception okay so in these scenarios if you still want to execute this one okay somehow something happened and this dependent test method is ignored or removed completely from your test suit so even in those scenarios also you want to still execute the track shipment okay that means without bothering about the dependent test you still want to execute the track shipment so in those scenarios what you need to do you need to add one parameter so when you write depends here there is another parameter called ignore missing dependencies so when i just mouse over on that what it is saying if set to true this test will run even if the method it depends on or missing or included that means when i add this parameter to the track shipment method so even though the create shipment method is not present inside this so basically this track shipment is dependent on create shipment dependent test right but that create shipment dependent test i have removed here so in those scenarios also if i want to execute the track shipment then i need to set this one as true let me save this and let me add the same parameter to this third method also because the third one is also dependent on the create shipment right save this and execute it so earlier we were getting the exception right so it is not executing anything and we are getting the exception because the create shipment method is removed but now if you see two test methods it is actually executed but because the track shipment is failed cancel shipment is skipped okay this cancel shipment is dependent on create shipment and track shipment so because this is failed this is moved into the skip status okay but do you see this track shipment is actually executed okay even though the create shipment method is not present inside this class the track shipment method is actually executed okay that is because we have added this parameter okay so this is how you need to handle guys so let me just revise the entire concept once again so what is a test dependency so if you want to depend on any other test method so if you want to execute your test cases by depending on any other test method then you need to have the concept of test dependency for your test cases okay and then why we need to use the test dependencies obviously so some of your tests might be dependent on another test so that is why you are going to use the test dependencies okay so next how to create a test dependency on other tests or test okay so it can be a single test or it can be multiple tests also so how you can create the by simply adding one parameter called depends on methods okay so when you add the depends on parameter and assign the values so what are the values that you are going to assign the dependent test methods names you are going to assign as a value here in the form of array okay so that's how you are going to create a test dependency and then what happens when priority is set to the test methods so even if you set any priority right so when you are using the test dependency even if you set any priority the priority will be simply ignored okay that means the dependency is having the highest priority over priority concept so that means the dependency is having upper hand over the priority you can say okay and then what happens when dependent test is failed so when the dependent test is failed the original test will get skipped but somehow i want to still execute the second method the the main method without bothering about the status of the dependent method so in those scenarios i will just add always run a parameter guys okay and the next one is what happens when dependent test is ignored or skipped or deleted so when any of these things happens okay when dependent test is kept or deleted or ignored your test case will throw the exception saying that it is not existed or maybe it is just simply ignored okay like that it will simply fail that it will throw the exception and it will not execute anything but even in these scenarios also if you want to still continue the execution then you need to add one parameter called ignore missing dependencies and set the value as true then even though if your dependent test methods are ignored or if it is removed also your main test method will be still executed okay so that is the concept guys so now you understood the test dependencies concept right so here we have seen how to create the test dependencies on the test cases that means the test methods not the test groups so test groups concept we are going to see in our next video for now we have only seen how to create the test dependency on the other test so that is about this video guys i hope you enjoy this video if you have any doubts please let me know in the comment section below or if you feel anything is missing inside this video that also please mention in the comment section below thank you for watching bye bye
Info
Channel: H Y R Tutorials
Views: 1,322
Rating: undefined out of 5
Keywords: hyr tutorials, h y r tutorials, selenium webdriver, selenium java, testng, testng tutorial for beginners, testng tutorial, testng annotations, testng selenium, testng framework in selenium, testng framework for selenium, testng framework, testing framework, testng tutorial for selenium webdriver, testng tutorial java, testng framework tutorial, TestNG Framework for Selenium, testng dependencies, test dependencies in TestNG, dependsonmethods, testng alwaysrun, test dependency
Id: QOF4t3UfgUI
Channel Id: undefined
Length: 40min 19sec (2419 seconds)
Published: Mon Sep 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.