Dependency
is a feature in TestNG that allows a test method to depend on a single or a
group of test methods. This will help in executing a set of tests to be
executed before a test method. Method dependency only works if the
“depend-on-method” is part of the same class or any of the inherited base class
(i.e. while extending a class).
Method
dependency only works with other methods that belong to the same class or in
one of the inherited classes but not across different classes. In case you need
a test method that exists in a separate class; you can achieve this by
assigning the said test method to a group and configuring the dependent test
method to be dependent on that group.
Test with single test method
dependency
TestNG lets
you create a sample test method that depends on another test method of the same
class.
public class
DependentTestExamples
{
@Test(dependsOnMethods = {
"testTwo" })
public void testOne() {
System.out.println("Test method
one");
}
@Test
public void testTwo() {
System.out.println("Test method
two");
}
}
The
preceding test class contains two test methods which print a message name onto
the console when executed. Here, test method testOne depends on test method
testTwo. This is configured by using the attribute dependsOnMethods while using
the Test annotation.
Let’s run the tests now.
Test method
two
Test method
one
PASSED:
testTwo
PASSED:
testOne
In the above
test result you can see the message Test method two printed before the Test
method one message. This shows that the testOne method got executed after
testTwo as it depends on testTwo.
Test with multiple
test methods dependencies
Sometimes it may
be required for a test method to depend upon multiple other methods. This
feature is very well supported by TestNG as part of the dependency support.
public class DependentTestExamples { @Test(dependsOnMethods
= { "testTwo", "testThree" }) public void testOne()
{ System.out.println("Test
method one"); } @Test public void testTwo()
{ System.out.println("Test
method two"); } @Test public void testThree()
{ System.out.println("Test
method three"); } } |
The preceding test class contains three test methods which print a
message name onto the console when executed. Here test method
testOne
depends on test methods testTwo
and testThree
. This is
configured by using the attribute dependsOnMethods while
using the Test annotation.
Let’s run the test now.
Test
method three Test
method two Test
method one PASSED:
testThree PASSED:
testTwo PASSED:
testOne
|
By looking at the console message we can see that methods
testTwo
and testThree
got executed before testOne
.
Inherited Dependency Test:
Till
now we have seen samples in which the dependent test methods were part of the
same class. Dependency on test methods can only be mentioned for test methods
that belong to the same class or any of the inherited base classes. Let’s see
how TestNG executes the test methods when the dependent methods are part of the
inherited base class.
public class ParentClassTest { @Test(dependsOnMethods = { "testTwo" }) public void testOne()
{ System.out.println("Test method
one"); } @Test public void testTwo()
{ System.out.println("Test method
two"); } } public class DependentTestExamples extends ParentClassTest { @Test(dependsOnMethods = { "testOne" }) public void testThree()
{ System.out.println("Test three
method in Inherited test"); } @Test public void testFour()
{ System.out.println("Test four method
in Inherited test"); } }
|
The preceding test class contains two test methods which print a message
name onto the console when executed. Here test method
testThree
depends on test method testOne
. This is
configured by using the attribute dependsOnMethods while using the Test annotation.
Let’s run the test
now.
Test four
method in Inherited test Test
method two Test
method one Test
three method in Inherited test PASSED:
testFour PASSED:
testTwo PASSED:
testOne PASSED:
testThree |
As you can see from the test results the sequence of execution is
testFour
, testTwo
, testOne
, and lastly, testThree
. AstestThree
depends on testOne
and on testTwo
, TestNG executes
all the test methods based on the dependency and finally the respective test
method.
Test that depends on a group
Similar
to dependent methods TestNG also allows test methods to depend on groups. This
makes sure that a group of test methods get executed before the dependent test
method.
public class DependentTestExamples { @Test(dependsOnGroups = { "test-group" }) public void groupTestOne()
{ System.out.println("Group Test
method one"); } @Test(groups = { "test-group" }) public void groupTestTwo()
{ } @Test(groups = { "test-group" }) public void groupTestThree()
{ System.out.println("Group Test
method three"); } } |
The preceding test class contains two test methods which print a message
name onto the console when executed. Here, test method
testOne
depends on test method testTwo
. This is configured
by using the attribute dependsOnMethods while using the Test annotation.
Let’s run the
tests now.
Group
Test method three Group
test method two Group
Test method one PASSED:
groupTestThree PASSED:
groupTestTwo PASSED:
groupTestOne |
No comments:
Post a Comment