TestNG @Test attributes – dependOnMethods

There are situations when we want to run our tests depends on the prior test execution like
if a particular test is run fine, then execute this test or else skip the test execution…
Basically we are creating inter dependencies in test execution.

Launch browser with URL, verify if title matches, then login
In Gmail, if login successful, then verify if Inbox link present or not
If report generated, then print the report

Let’s see the code implementation

in the above code, test1() will fail —title of page doesn’t contain string “Testing”—, but still test2() will continue to run, because there is no dependency,
but we need to create a dependency to make sure test2() will run only if test1() runs successfully.
to achieve this, let’s implement dependsOnMethods 

Here, test2() will not execute as test1() failed, this just make sure we need to click on required link if the page title is Testing,
In the above code scenario, we are not on right page, so no need to click on selenium tutorial link.
Same we have another attribute – dependsOnGroups

OutPut –

tc3: testcase1
tc4: testcase1
PASSED: method3
PASSED: method4
FAILED: method1
java.lang.ArithmeticException: / by zero
at dependonMethods.Testcs1.method1(Testcs1.java:9)

SKIPPED: method2

NOTE : We can use regular expression, a.* refers to a and ab as well.

Short link – bit.ly/qav-testngdependon

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *