Bird
Raised Fist0
Selenium Javatesting~5 mins

Test suites (testng.xml) in Selenium Java

Choose your learning style10 modes available
Introduction

A test suite groups multiple test cases to run together in one go. It helps organize and run tests efficiently.

When you want to run several related test cases at once.
When you need to run tests in a specific order.
When you want to run tests on different browsers or environments.
When you want to separate smoke tests from full regression tests.
When you want to run tests in parallel to save time.
Syntax
Selenium Java
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SuiteName">
  <test name="TestName">
    <classes>
      <class name="packageName.ClassName"/>
      <class name="packageName.AnotherClassName"/>
    </classes>
  </test>
</suite>

The <suite> tag defines the whole test suite.

The <test> tag groups classes to run as one test.

Examples
This suite runs only the LoginTest class as a test named LoginTests.
Selenium Java
<suite name="RegressionSuite">
  <test name="LoginTests">
    <classes>
      <class name="tests.LoginTest"/>
    </classes>
  </test>
</suite>
This suite runs two tests: SmokeTests and RegressionTests, each with their own classes.
Selenium Java
<suite name="FullSuite">
  <test name="SmokeTests">
    <classes>
      <class name="tests.SmokeTest"/>
    </classes>
  </test>
  <test name="RegressionTests">
    <classes>
      <class name="tests.RegressionTest"/>
    </classes>
  </test>
</suite>
This suite runs Test1 and Test2 in parallel using 2 threads.
Selenium Java
<suite name="ParallelSuite" parallel="tests" thread-count="2">
  <test name="Test1">
    <classes>
      <class name="tests.TestClass1"/>
    </classes>
  </test>
  <test name="Test2">
    <classes>
      <class name="tests.TestClass2"/>
    </classes>
  </test>
</suite>
Sample Program

This is a simple testng.xml file defining a suite named SampleSuite with one test named SampleTest that runs SampleTestClass.

Selenium Java
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SampleSuite">
  <test name="SampleTest">
    <classes>
      <class name="tests.SampleTestClass"/>
    </classes>
  </test>
</suite>
OutputSuccess
Important Notes

Always use the full package and class name in the <class> tag.

You can run the suite file from your IDE or command line to execute all tests inside.

Use the parallel attribute to speed up tests but be careful with shared resources.

Summary

Test suites group tests to run together easily.

Use testng.xml to define which tests to run and how.

Suites help organize, order, and run tests in parallel if needed.

Practice

(1/5)
1. What is the main purpose of the testng.xml file in Selenium testing?
easy
A. To write the Selenium WebDriver code
B. To store test data like usernames and passwords
C. To group and organize test classes and methods to run together
D. To generate HTML reports after test execution

Solution

  1. Step 1: Understand the role of testng.xml

    The testng.xml file is used to define which tests to run and how to organize them.
  2. Step 2: Identify the main purpose

    It groups test classes and methods so they can be run together as a suite.
  3. Final Answer:

    To group and organize test classes and methods to run together -> Option C
  4. Quick Check:

    Test suite grouping = To group and organize test classes and methods to run together [OK]
Hint: Remember: testng.xml organizes tests to run as a group [OK]
Common Mistakes:
  • Confusing testng.xml with test code files
  • Thinking testng.xml stores test data
  • Assuming testng.xml generates reports
2. Which of the following is the correct way to define a test suite in testng.xml?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Recall the correct XML structure for TestNG suites

    The root element is <suite>, inside it we define <test>, then <classes> and <class>.
  2. Step 2: Match the options with correct structure

    correctly follows this order and syntax.
  3. Final Answer:

    <suite name="MySuite"><test name="Test1"><classes><class name="TestClass"/></classes></test></suite> -> Option A
  4. Quick Check:

    Suite > Test > Classes > Class = [OK]
Hint: Remember XML order: suite > test > classes > class [OK]
Common Mistakes:
  • Swapping <suite> and <test> tags
  • Placing <class> directly under <suite>
  • Using wrong tag names or attributes
3. Given the following testng.xml snippet, what tests will run?
<suite name="Suite1" parallel="tests" thread-count="2">
  <test name="TestA">
    <classes>
      <class name="tests.LoginTest"/>
    </classes>
  </test>
  <test name="TestB">
    <classes>
      <class name="tests.PaymentTest"/>
    </classes>
  </test>
</suite>
medium
A. No tests run due to missing methods tag
B. Both LoginTest and PaymentTest run in parallel as separate tests
C. Tests run sequentially, LoginTest then PaymentTest
D. Only LoginTest runs because it is first

Solution

  1. Step 1: Analyze the parallel attribute in <suite>

    The attribute parallel="tests" means each <test> runs in parallel threads.
  2. Step 2: Identify the tests defined

    There are two tests: TestA with LoginTest class and TestB with PaymentTest class.
  3. Final Answer:

    Both LoginTest and PaymentTest run in parallel as separate tests -> Option B
  4. Quick Check:

    parallel="tests" runs tests simultaneously = Both LoginTest and PaymentTest run in parallel as separate tests [OK]
Hint: parallel="tests" runs each <test> in parallel [OK]
Common Mistakes:
  • Assuming tests run sequentially despite parallel attribute
  • Thinking <methods> tag is mandatory to run tests
  • Confusing parallel="tests" with parallel="classes"
4. You have this testng.xml file but tests are not running:
<suite name="Suite1">
  <test name="Test1">
    <class name="TestClass">
    </class>
  </test>
</suite>

What is the likely problem?
medium
A. The <suite> tag must include thread-count
B. The <test> tag is missing the parallel attribute
C. The class name must include the package name
D. The <class> tag should be self-closing or empty, not with separate open and close tags

Solution

  1. Step 1: Check the <class> tag syntax

    In TestNG XML, <class> should be self-closing like <class name="TestClass"/> or empty without content.
  2. Step 2: Identify the error in the given XML

    The given XML uses separate open and close tags for <class> which is invalid and causes tests not to run.
  3. Final Answer:

    The <class> tag should be self-closing or empty, not with separate open and close tags -> Option D
  4. Quick Check:

    Use self-closing <class> tag = The <class> tag should be self-closing or empty, not with separate open and close tags [OK]
Hint: Use self-closing tag in testng.xml [OK]
Common Mistakes:
  • Adding parallel attribute thinking it fixes syntax
  • Forgetting package name but that doesn't stop tests
  • Assuming thread-count is mandatory
5. You want to run two test classes LoginTest and PaymentTest in parallel but limit total threads to 1 in testng.xml. Which configuration achieves this?
hard
A.
B.
C.
D.

Solution

  1. Step 1: Understand parallel and thread-count attributes

    Setting parallel="classes" runs test classes in parallel, and thread-count="1" limits to one thread, so classes run one at a time.
  2. Step 2: Check options for matching configuration

    sets parallel to classes and thread-count to 1, matching the requirement.
  3. Final Answer:

    Option A configuration runs classes in parallel but limits threads to 1 -> Option A
  4. Quick Check:

    parallel="classes" + thread-count=1 = [OK]
Hint: parallel="classes" with thread-count=1 runs classes sequentially [OK]
Common Mistakes:
  • Using parallel="tests" with thread-count > 1 runs multiple threads
  • Omitting parallel attribute runs tests sequentially
  • Confusing parallel="methods" with classes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard