50 likes | 59 Views
What Is TestNG Annotations Its Benefits, Hierarchy & TestNG Case Priorities
E N D
What Is TestNG Annotations-Its Benefits, Hierarchy & TestNG Case Priorities? Annotationsgenerallyrepresentanoteoracommentonadiagram,etc.,toconveyitsmeaning.TestNGalsousesthemfortheexactreason.TestNGannotationsarethecodewritteninsideyoursourcetestcodelogictomanagetheflowoftheexecutionoftests? Therefore,itisvitaltoannotateyourprocessesinTestNGtoexecutethetests.TestNGwill ignoretheprocessthatdoesnotcontainanannotationsinceitwon'tknowwhentorunit.AnnotationsinTestNGcanbecomprehendedasdistinctpiecesofcodeintegratedwithinthelogicofaprogramtomanagethesequentialexecutionofthetestmethods.Inotherwords,TestNGAnnotationsassistindecidingthesequenceofthetestsmadetoexecute.TheseannotationsareextractedfromJavalanguageandareacriticalcomponentofTestNextGeneration(TestNG),anautomationframeworkthatSeleniumuses. ATestNGannotationbeginsfromthesymbol"@,"andtheannotationnamefollowsafter that.Apartfromtheheaderfileand'@'symbol,yourequirenothingtoexecuteTestNGannotations. DifferentKindsOfTestNGAnnotations • InTestNG,therearetenkindsofannotations: • @BeforeSuite-InTestNG,the@BeforeSuitemethodexecutesbeforeallothertestmethods. • @AfterSuite-InTestNG,the@AfterSuitemethodrunsafterallothertestmethods • havebeencompleted.
@BeforeTest-The@BeforeTestfunctioninTestNGrunsbeforeallofthetestmethodsinthatfolderareexecuted.@BeforeTest-The@BeforeTestfunctioninTestNGrunsbeforeallofthetestmethodsinthatfolderareexecuted. • @AfterTest-TestNG's@AfterTestfunctionrunsafteralltestmethodsinafolderare • executed. • @BeforeClass-InTestNG,the@BeforeClassfunctionrunsbeforethefirstmethodcollectsthecurrentclass. • @AfterClass-InTestNG,the@AfterClassmethodiscalledafterallofthecurrent • class'stestmethodshavebeencompleted. • @BeforeMethod-InTestNG,the@BeforeMethodmethodexecutesbeforeeachtestmethod. • @AfterMethod-InTestNG,the@AfterMethodmethodexecutesaftereachtest • methodisrun. • @BeforeGroups-InTestNG,the@BeforeGroupsmethodrunsbeforethetestcasesinthatgrouprun.Itonlyexecutesonce. • @AfterGroups-The@AfterGroupsfunctioninTestNGexecutesafterthetestcases • inthatgrouphavebeenrun.Itisonlyusedonce. • TheseTestNGannotationshaveself-explanatorydefinitions.OneoftheforemostreasonstopreferTestNGisthatitisstraightforwardtolearn. • BenefitsOfTestNGAnnotations • Let'sdiscussthebenefitsofTestNGAnnotations- • TestNGannotationsaidinthecreationofacomprehensiveandcompletetestreportthatincludesinformationonthenumberoftestcasesrun,passedtestcases,failedtestcases,andskippedtestcases. • Itenablesthesimplegroupingofmultipletestcasesusingthetesting.xmlfile.Moreover,onecanprioritizetheexecutionoftestcases. • TestNGAnnotationscanimplementthesametestcasemultipletimesbyusingthe • keyword,‘invocationcount.’ • AnnotationsinTestNGareeasilyunderstandable. • TestNGAnnotationsprovidesimplecodingofteststoeliminatetherequirementforastaticprimarymethod. • TestNGassistsinthegroupingoftestcasesandtheirexecution. • TestNGofferstheabilitytoprioritizetestcasesbasedonthedemandsoftheprogrammer. • TestNGhelpstoexecutethetestcasesinaparallelmanner. • TestNGprovidesanidealwayofreportgenerationaccordingtothetestresults. • TestNGisstronglytyped,whichspecifiesthecapabilityofsoliderrorchecking. • TestNGcanalsolinktoexternaldatasources. • Toexecutetestcases,thereisnorequirementtofollowapatternorformat. • AdditionalparameterscanbepassedtoTestNGannotations. • InthecaseofTestNGannotations,thereisnorequirementtoextensionofanytestclasses. • TestNGAnnotationsarehighlytyped,whichmeansthaterrorsaredetectedatcompiletime. • HierarchyoftheTestNGAnnotations:
TestNGhasalotofannotationsthathelpyouwritegoodtestsourcecodewhenyou'retestingsoftware.So,howdoesTestNGdecidewhichtestcasetorunfirst,followedbythenext,andsoon?Intheseannotations,theanswerisahierarchy.Therefore,testNGincludesahierarchyamongtheannotations.Thishierarchyisasfollows-thetopbeingthehighestpriority):TestNGhasalotofannotationsthathelpyouwritegoodtestsourcecodewhenyou'retestingsoftware.So,howdoesTestNGdecidewhichtestcasetorunfirst,followedbythenext,andsoon?Intheseannotations,theanswerisahierarchy.Therefore,testNGincludesahierarchyamongtheannotations.Thishierarchyisasfollows-thetopbeingthehighestpriority): • @BeforeSuite • @BeforeTest • @BeforeClass • @BeforeMethod • @Test • @AfterMethod • @AfterClass • @AfterTest • @AfterSuite • TestNGPrioritization • PrioritizationinTestNGisamethodtoprovideaseriestothemethodssothattheydonotexecuteoutoforder.SincealphabeticallyrunningtestcasesinTestNGhavenologicalsequence,equippingprioritytothesetestcaseshelpsusorganizeourtests'execution. • PriorityinTestNGtestcasesisaparameterwithcharacteristicvalueasapriority. • Theprioritiesareanaddedoptionthatwecanusewiththetestannotations.Thischaracteristicdeterminestheimportanceoftheannotation.ButalwaysrememberthatprioritycheckoccursaftertheannotationcheckbyTestNG.Asaresult,theTestNGannotationhierarchycomesfirst,followedbypriority-basedexecution. • ItisnecessarytoconsiderthefollowingpointsconcerningpriorityinTestNG: • Only@TestmethodscanbeusedtodefinePriorityinTestNGtestmethods. • Thetestcasemethodhasahigherprioritywhentheprioritynumberislower.
PriorityinTestNGcontainsonlyintegervalues.Thevaluecanbezero,negativeorpositive.PriorityinTestNGcontainsonlyintegervalues.Thevaluecanbezero,negativeorpositive. • InTestNG,ifapriorityisdefinedindecimal,itmustfirstbeconvertedtoaninteger(throughtypecasting). • OneprocessisenabledtohaveonlyonepriorityinTestNG. • PrioritycannotpassthroughtheXMLfiles. Conclusion- TestNGAnnotationsareusedtohandlethenextmethodtobeaccomplishedinthetestscript.Itrepresentsabatchofcodeinsertedintotheprogramorbusinesslogicusedtomanagetheflowofprocessesinthetestscript. Theannotationsareveryeasytounderstandandexecute.Thereisnopredefinedformator rule,andthetesterneedstoannotatemethodsusingtheirjudgment.TheymakeSeleniumtestscriptsmoresophisticated,effective,andmanageable.Usingthemisextremelyhelpfulfortestersandmakestheirlivesmuchmoreaccessible.