TESTNG Group Testing

Source: Internet
Author: User
Tags testng tutorialspoint

Group testing in TestNG is a new innovative feature that does not exist in the JUnit framework, it allows scheduling to appropriate partial methods and preform complex test method groupings. Not only can you declare those methods that belong to a group, but you can also specify a group that contains other groups. TestNG can then call and ask to include a specific set of groups (or regular expressions) and exclude another collection. This gives you the greatest flexibility, how to partition the test, and if you want to run two sets of different tests back and on, there is no requirement to recompile anything.
The group specifies the Testng.xml file using the <groups> tag. It can be found either by <test> or <suite> tags. The group designation <suite> label applies to all of the <test> tags below.
Now, let's look at an example of how group testing.
Create a class
Create a Java class to test for Messageutil.java at c \ > testng_workspace
/*
* This class prints the given message on console.
*/
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 "Tutorialspoint" to the message
Public String Salutationmessage () {
message = "Tutorialspoint" + message;
SYSTEM.OUT.PRINTLN (message);
return message;
}

Add "www." to the message
Public String Exitmessage () {
message = "www." + message;
SYSTEM.OUT.PRINTLN (message);
return message;
}
}
Create a test case class
Create a Java test class for Grouptestexample.java.
The test class adds test methods Testprintmessage () and Testsalutationmessage ().
The two categories of test methods for groups are:
Check-in registration Test (CHECKINTEST): You should run these tests before committing the new code. They should usually be fast, just make sure that there is no basic function broken.
Functional Testing (Functest): These tests should cover all the features of the software, run at least once a day, although ideally you would want them to keep running.
Create Java class file name Grouptestexample.java in c \ > Testng_workspace
Import Org.testng.Assert;
Import Org.testng.annotations.Test;

public class Grouptestexample {
String message = ". com";
Messageutil messageutil = new Messageutil (message);

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

@Test (groups = {"Checkintest"})
public void Testsalutationmessage () {
System.out.println ("Inside testsalutationmessage ()");
message = "Tutorialspoint" + ". com";
Assert.assertequals (Message, messageutil.salutationmessage ());
}

@Test (groups = {"Functest"})
public void Testingexitmessage () {
System.out.println ("Inside testexitmessage ( )");
message = "www." + "Tutorialspoint" + ". com";
assert.assertequals (Message, messageutil.exitmessage ());
}
}
Create Testng.xml
Create a file testng.xml C: \ > Testng_workspace to execute the test case, where we will only execute these tests, which belong to group Functest.
<?xml version= "1.0" encoding= "UTF-8"?
<! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd",
<suite name= "Suite1";
<test name= "Test1"
<groups>
<run>
<include name= "Functest"/>
</run>
</groups>
<classes>
<class name= "Grouptestexample"/>
</classes>
</test>
</ Suite> the
compile Messageutil test case class uses Javac.

C:\testng_workspace>javac Messageutil.java Grouptestexample.java
Now, Run Testng.xml, only run the method Testprintmessage (), because it belongs to the group Functest.
C:\testng_workspace>java-cp "C:\TestNG_WORKSPACE" Org.testng.TestNG testng.xml
validates the output. Only the method Testprintmessage () is executed.
Inside testprintmessage ()
. com
Inside testexitmessage ()
www..com

===============================================
Suite1
Total tests Run:2, failures:1, skips:0
======== =======================================
Groups in groups
groups can also contain other groups. These groups are called metagroups. For example, you might want to define all of a group, including Checkintest and Functest. Let's modify the Testng.xml file as follows:
<?xml version= "1.0" encoding= "UTF-8"?
<! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd",
<suite name= "Suite1";
<test name= "Test1"
<groups>
<define name= "All",
<include name= "Functest"/>
<include name= " Checkintest "/>
</define>
<run>
<include name=" All "/>
</run>
</ Groups>
<classes>
<class name= "Grouptestexample"/>
</classes>
</test>
</suite>
performing the above Testng.xml will perform all three tests will give you the following result:
Inside testprintmessage ()
. com
Inside Testsalutationmessage ()
tutorialspoint.com
Inside testexitmessage ()
www.tutorialspoint.com

===============================================
Suite1
Total tests Run:3, failures:0, skips:0
===============================================
Exclusion Group
You can omit a group using the <exclude> tag, as shown in:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" >
<suite name= "Suite1" >
<test name= "Test1" >
<groups>
<define name= "All" >
<exclude name= "Functest"/>
<include name= "Checkintest"/>
</define>
<run>
<include name= "All"/>
</run>
</groups>
<classes>
<class name= "Grouptestexample"/>
</classes>
</test>
</suite>

TESTNG Group 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.