TestNG Suites Example (example of a Java unit Test component)

Source: Internet
Author: User
Tags testng

"The other tutorials in this series are being translated and clicked on Category: TestNG for viewing." 】

"Translation by mingming like the month QQ 605283073"

Original: http://websystique.com/java/testing/testng-suites-example/


Previous: TestNG Groups example


This article describes the TestNG Suite test, which is represented by a suite of XML files called Testng.xml in TestNG.

Each test method that uses testng is part of the suite.  A suite in testng is represented by an XML file that is typically named Testng.xml. The <suite> tag is the first label for this XML that represents a suite that can contain
<test> tags. Each <test> label corresponding may also contain one or more <classes>.
<packages>, <groups> tags.

When TestNG executes a suite, he mainly reads the XML content and executes it according to the XML definition.

Examples of Testng.xml

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" >
<suite name= "tests" parallel= "tests" thread-count = "2" >
     
    <test name= "front-end" >
        <packages>
            <package name= " com.websystique.testng.frontend.* "/>
        </packages>
    </test>
 
    <test name=" Back-end " >
        <packages>
            <package name= "com.websystique.testng.backend.*"/>
        </packages>
    </test>
 
</suite>

<test> label has two value ' front-end ' & ' Back-end ' with name.

Each <test> label contains the specified package name. When TestNG wants to run this suite, it checks the packages to get the required running

Test classes and methods.

In this case the front-end and back-end tests can run in parallel.

Note: Each category of the suite, such as front-end, will run through a separate thread.

more Suite (suite) Examples
1 Perform all tests:

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" >
<suite name= "tests" >
     
    <test name= "full" >
        <packages>
            <package name= "com.websystique.*"/>
        </packages>
    </test>
 
</suite>

2 perform all tests except the UI group

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" >
<suite name= "tests" >
 
    <test name= " Backend ">
        <groups>
            <run>
                <exclude name=" UI "/>
            </run>
        </ groups>
        <packages>
            <package name= "com.websystique.*"/>
        </packages>
    </test>
 
</suite>

2 perform all tests except the UI group (with classes instead of packages)

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" >
<suite name= "tests" >
 
    <test name= " Backend ">
        <groups>
            <run>
                <exclude name=" UI "/>
            </run>
        </groups >
        <classes>
            <class name= "Com.websystique.testng.TestCalculator"/>
        </classes>
    </test>
 
</suite>

Suite & Test Label level annotations: @BeforeSuite, @AfterSuiteTake @BeforeSuiteThe annotation method is executed once before any test method in the suite is executed.
This is a better fit to set up or initialize a shared environment for multiple groupings. @AfterSuite
The annotation method is executed once after any test method in the suite is executed.
This is a better fit to set up or initialize a shared environment for multiple groupings.
@BeforeTest, @AfterTest
@BeforeTest will be executed once before any method with <test> label is executed.
@AfterTest is the opposite. a complete example
Create Testng.xml Src/test/resources/testng.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" >
<suite name= "fulltests" >
  
    <test name= " Front-End ">
        <classes>
            <class name=" Com.websystique.testng.TestUI "/>
        </classes>
    </test>
  
    <test name= "Back-end" >
        <classes>
            <class name= " Com.websystique.testng.TestSecurity "/>
            <class name=" com.websystique.testng.TestDataBase "/>"
            <class name= "Com.websystique.testng.TestBackendConfig"/>
        </classes>
    </test>
 
</suite>


Package com.websystique.testng;
 
Import Org.testng.annotations.AfterTest;
Import Org.testng.annotations.BeforeTest;
Import Org.testng.annotations.Test;
 
public class Testui {
 
    @BeforeTest public
    void Beforetest () {
        System.out.println ("@BeforeTest");
    }
  
    @AfterTest public
    void Aftertest () {
        System.out.println ("@AfterTest \ n")
    ;
     
    @Test (groups= "UI") public
    void Openconfirmationdialog () {
        System.out.println ("Openconfirmationdialog ()") );
    }
 
    @Test (groups= "UI") public
    void OpenFileDialog () {
        System.out.println ("OpenFileDialog ()");
    }
 

Package com.websystique.testng;
 
Import Org.testng.annotations.Test;
 
public class Testsecurity {
     
    @Test (groups= ' security ') public
    void Accesshomepage () {
        System.out.println (" Accesshomepage () ");
    }
 
    @Test (groups= "security") public
    void Accessadminpage () {
        System.out.println ("Accessadminpage ()");
    }
 

Package com.websystique.testng;
 
Import Org.testng.annotations.Test;
 
The public class Testdatabase {
 
    @Test (groups= "database") is public
    void Testinsert () {System.out.println ()
        Testinsert () ");
    }
     
    @Test (groups= "database") public
    void Testupdate () {
        System.out.println ("Testupdate ()");
    }
 
    @Test (groups= "database") public
    void Testdelete () {
        System.out.println ("Testdelete ()");
    }
 


Package com.websystique.testng;
 
Import Org.testng.annotations.AfterSuite;
Import Org.testng.annotations.AfterTest;
Import Org.testng.annotations.BeforeSuite;
Import Org.testng.annotations.BeforeTest;
 
public class Testbackendconfig {
 
    @BeforeSuite public
    void Beforesuite () {
        System.out.println ("@ Beforesuite\n ");
    }
  
    @AfterSuite public
    void Aftersuite () {
        System.out.println ("@AfterSuite");
    }
     
    @BeforeTest public
    void Beforetest () {
        System.out.println ("@BeforeTest");
    }
  
    @AfterTest public
    void Aftertest () {
        System.out.println ("@AfterTest \ n");
    }
     

Right-Testng.xml, Run as–>testng Suites



Execution results:

@BeforeSuite
 
@BeforeTest
openconfirmationdialog ()
OpenFileDialog ()
@AfterTest
 
@BeforeTest
accessadminpage ()
accesshomepage () testdelete () Testinsert ()
testupdate ()
@AfterTest
 
@AfterSuite
 
===============================================
fulltests Total
tests Run:7, failures:0, skips:0
===============================================


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.