logo

The best IT Trainig Institute In Gurgaon

Create Test Suite in TestNG

In TestNG, a Test Suite is a collection of test cases, classes, or methods that you can run together. You can organize these test cases into groups, and configure their execution order, parallel execution, and other settings within a Test Suite. The Test Suite is defined in an XML file called testng.xml.

Key Components of a TestNG Test Suite
  1. Suite Tag (< suite>): This is the root element in the testng.xml file. It defines the name of the suite and contains one or more < test> tags.
  2. Test Tag (): The < test> tag represents a collection of test classes or methods. Each < test> can have its own configurations and can run independently of others.
  3. Classes Tag (< classes>): Inside each < test>, you define the classes that contain the test methods to be executed. You do this with the < classes> tag, which holds one or more < class> tags.
  4. Class Tag (< class>): This tag specifies a single class to be executed. You provide the fully qualified name of the class (including the package name).
  5. Groups Tag (< groups>): TestNG allows you to define groups of test methods. You can specify which groups to include or exclude from a particular test.
  6. Methods Tag (< methods>): Inside a < class> tag, you can specify individual methods to include or exclude by using the < methods> tag with < include> or < exclude> child tags.
  7. Parallel Execution: TestNG allows you to run tests in parallel by setting the parallel attribute in the or tags. This can be set to methods, classes, or tests depending on your needs.
  8. Thread Count: The thread-count attribute in the or tags specifies the number of threads to use when running tests in parallel.
Example of Test Suite in TestNG

< ?xml version="1.0" encoding="UTF-8"?>
< !DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
< suite name="Master Suite">

    < suite-files>
        < suite-file path="./testng.xml">< /suite-file>
        < suite-file path="./regression.xml">< /suite-file>
    < /suite-files>

< /suite>  
  1. XML Declaration (< ?xml version="1.0" encoding="UTF-8" ?>): This line indicates that the document is an XML file and specifies the version of XML being used (1.0) and the character encoding (UTF-8).
  2. DOCTYPE Declaration (< !DOCTYPE suite SYSTEM "https: //testng .org/ testng-1 .0.dtd">): This line defines the document type for TestNG. It links to the DTD (Document Type Definition) for TestNG, which tells the XML parser how the file should be structured.
  3. Suite Tag (< suite name=" Master Suite ">): This is the root element of the XML file, representing a TestNG suite. The name attribute (Master Suite) gives a name to this suite. This name is used in reports and logs to identify the suite.
  4. Suite Files (< suite -files>): This section is used to include other TestNG XML files (test suites) within the master suite. It allows you to group multiple test suites into a single execution.
  5. Suite File ( < suite-file path="./testng.xml" />):
    • The < suite -file> tag specifies the path to another TestNG XML file that should be included and run as part of the master suite.
    • The path attribute points to the location of the XML file. In this example:
      • ./testng.xml: Refers to a TestNG suite file located in the current directory.
      • ./regression.xml: Refers to another TestNG suite file also in the current directory.
  6. Closing Tags (< /suite -files> and < /suite>): These tags close the suite-files and suite sections, respectively.
Benefits:
  • Centralized Management: Organize and manage multiple test suites in one place.
  • Reusability : Reuse existing test suites without modifying them; simply include them in the master suite.
  • Consistency : Ensure that all necessary test suites are run together, which is particularly useful for comprehensive test runs like nightly builds or continuous integration pipelines.