TestNG Dependency Testing

Source: Internet
Author: User
Tags testng

Sometimes, you may need to call the method in a particular order in a test case, or you want to share some of the state between the data and the method. TestNG supports this dependency test method by explicitly relying on it to support claims.
TestNG allows you to specify dependencies, whether or not:
Use the attribute Dependsonmethods in @Test comment or
Use the attribute dependsongroups in the @test annotation.
Use attribute Dependsonmethods for example
Create a class
Create a Java class to test for Messageutil.java at c \ > testng_workspace
public class Messageutil {
Private String message;

Constructor
@param message to be printed
Public Messageutil (String message) {
this.message = message;
}

Prints the message
Public String Printmessage () {
SYSTEM.OUT.PRINTLN (message);
return message;
}

Add "hi!" to the message
Public String Salutationmessage () {
message = "hi!" + message;
SYSTEM.OUT.PRINTLN (message);
return message;
}
}
Create a test case class
Create a Java test class for Dependencytestusingannotation.java.
Add Methods Testprintmessage (), Testsalutationmessage (), and Initenvironmenttest () to the test class
Add Property Dependsonmethods = {"Initenvironmenttest"} to the @Test comment oftestsalutationmessage () method.
Create Java class file name Dependencytestusingannotation.java in c \ > Testng_workspace
Import Org.testng.Assert;
Import Org.testng.annotations.Test;

public class Dependencytestusingannotation {
String message = "Manisha";
Messageutil messageutil = new Messageutil (message);

@Test
public void Testprintmessage () {
System.out.println ("Inside testprintmessage ()");
message = "Manisha";
Assert.assertequals (Message, messageutil.printmessage ());
}

@Test (dependsonmethods = {"Initenvironmenttest"})
public void Testsalutationmessage () {
System.out.println ("Inside testsalutationmessage ()");
Message = "hi!" + "Manisha";
assert.assertequals (Message, messageutil.salutationmessage ());
}

@Test
public void Initenvironmenttest () {
System.out.println ("This is Initenvironmenttest");
}
}
Create Testng.xml
Create a file testng.xml in C: \ > Testng_workspace to execute the test case
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" >
<suite name= "Suite1" >
<test name= "Test1" >
<classes>
<class name= "Dependencytestusingannotation"/>
</classes>
</test>
</suite>
Compile the Messageutil test case class using Javac
C:\testng_workspace>javac Messageutil.java Dependencytestusingannotation.java
Now run Testng.xml this will run Testsalutationmessage () only after the Ofinitenvironmenttest () method is executed
C:\TESTNG_WORKSPACE>JAVA-CP "C:\TestNG_WORKSPACE" Org.testng.TestNG testng.xml
Validate output

This is Initenvironmenttest
Inside Testprintmessage ()
Manisha
Inside Testsalutationmessage ()
Hi! Manisha

===============================================
Suite1
Total tests Run:3, failures:0, skips:0
===============================================
example, using the attribute dependsongroups
You can also rely on the method of the whole group. Let's take a look at the following example:
Create a class
Create a Java class to test for Messageutil.java at c \ > testng_workspace
public class Messageutil {
Private String message;

Constructor
@param message to be printed
Public Messageutil (String message) {
this.message = message;
}

Prints the message
Public String Printmessage () {
SYSTEM.OUT.PRINTLN (message);
return message;
}

Add "hi!" to the message
Public String Salutationmessage () {
message = "hi!" + message;
SYSTEM.OUT.PRINTLN (message);
return message;
}
}
Create a test case class
Creating a Java Test class says dependency Testusingannotation.java.
Add test Methods Testprintmessage (), Testsalutationmessage (), and initenvironmenttest () test classes and their group "initialize"
Add Property Dependsonmethods = {"Init.*"} to the @Test comment testsalutationmessage () method
Create Java class file name Dependencytestusingannotation.java in c \ > Testng_workspace
Import Org.testng.Assert;
Import Org.testng.annotations.Test;

public class Dependencytestusingannotation {
String message = "Manisha";
Messageutil messageutil = new Messageutil (message);

@Test (groups = {"Init"})
public void Testprintmessage () {
System.out.println ("Inside testprintmessage ()");
message = "Manisha";
Assert.assertequals (Message, messageutil.printmessage ());
}

@Test (dependsongroups = {"Init.*"})
public void Testsalutationmessage () {
System.out.println ("Inside testsalutationmessage ()");
message = "hi!" + "Manisha";
Assert.assertequals (Message, messageutil.salutationmessage ());
}

@Test (groups = {"Init"})
public void Initenvironmenttest () {
System.out.println ("This is Initenvironmenttest");
}
}
In this example, Testsalutationmessage () is declared to be based on any set of matching regular expressions "init*", which guarantees that a method, Testprintmessage () and initenvironmenttest () will always be called before Testsalutationmessage ().
If a method fails, depending on whether you have a hard to rely on it (alwaysrun= false, which is the default), the method that has no markup depends on its failure, but as skipped. Skipping methods will be reported for example in the final report (in HTML, neither red nor green color), which is important because the skipped method is not necessarily a failure.
Create Testng.xml
Create a file testng.xml C: \ > Testng_workspace Execute test case
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" >
<suite name= "Suite1" >
<test name= "Test1" >
<classes>
<class name= "Dependencytestusingannotation"/>
</classes>
</test>
</suite>
Compile the Messageutil test case class using Javac
C:\testng_workspace>javac Messageutil.java Dependencytestusingannotation.java
Now, run Testng.xml, which will run the Testsalutationmessage () method after the Initenvironmenttest () method is executed.
C:\TESTNG_WORKSPACE&GT;JAVA-CP "C:\TestNG_WORKSPACE" Org.testng.TestNG testng.xml
Validate output

This is Initenvironmenttest
Inside Testprintmessage ()
Manisha
Inside Testsalutationmessage ()
Hi! Manisha

===============================================
Suite1
Total tests Run:3, failures:0, skips:0
===============================================
Dependsongroups Vs Dependsonmethods
In the use of groups, we no longer face the problem of refactoring. As long as we do not modify the Dependsongroups or group properties, our tests will continue to run, setting up the appropriate dependencies.
Whenever a new method needs to add a dependency graph, all we need to do is put it in the correct group and make sure it depends on the correct group. We do not need to modify any other methods.

TestNG Dependency Testing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.