Why TestNG structures test execution in Selenium Java - Automation Benefits in Action
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class TestNGExecutionOrder { private WebDriver driver; private final String url = "https://example.com"; @BeforeMethod public void setUp() { // Set path to chromedriver executable if needed driver = new ChromeDriver(); } @AfterMethod public void tearDown() { if (driver != null) { driver.quit(); } } @Test(priority = 2) public void testA() { driver.get(url); System.out.println("Running testA"); Assert.assertEquals(driver.getCurrentUrl(), url, "URL should be https://example.com"); } @Test(priority = 1) public void testB() { driver.get(url); System.out.println("Running testB"); Assert.assertEquals(driver.getCurrentUrl(), url, "URL should be https://example.com"); } @Test(priority = 3) public void testC() { driver.get(url); System.out.println("Running testC"); Assert.assertEquals(driver.getCurrentUrl(), url, "URL should be https://example.com"); } }
This test class uses TestNG to demonstrate how test execution order is controlled by the priority attribute.
The @BeforeMethod sets up the browser before each test, and @AfterMethod closes it after each test to keep tests independent.
Each test method navigates to the same URL and prints a message to the console to show execution order.
Assertions check that the browser is on the expected URL.
When running this test, TestNG runs testB first because it has priority 1, then testA (priority 2), then testC (priority 3).
This shows how TestNG structures test execution order based on priority, helping organize tests clearly and predictably.
Now add data-driven testing with 3 different URLs to verify TestNG execution order with different inputs
Practice
priority and dependsOnMethods?Solution
Step 1: Understand TestNG test execution control
TestNG uses features likepriorityanddependsOnMethodsto manage the order and dependencies of tests.Step 2: Recognize the benefit of structured execution
This control helps tests run smoothly and find problems faster by running in a logical order.Final Answer:
To control the order and dependencies between tests for smoother runs -> Option BQuick Check:
TestNG structures execution = Control order & dependencies [OK]
- Thinking TestNG runs tests randomly
- Believing TestNG skips tests automatically
- Assuming TestNG runs tests only once without control
Solution
Step 1: Recall TestNG annotation syntax
TestNG uses@Test(priority = number)to set the execution order of tests.Step 2: Identify the correct attribute name
The correct attribute ispriority, notorder,sequence, orrank.Final Answer:
@Test(priority = 1) -> Option AQuick Check:
TestNG priority syntax = @Test(priority = x) [OK]
- Using wrong attribute names like order or rank
- Missing parentheses after @Test
- Confusing priority with dependsOnMethods
@Test(priority = 2)
public void testB() { }
@Test(priority = 1)
public void testA() { }
@Test(priority = 3)
public void testC() { }Solution
Step 1: Identify priorities assigned to each test
testA has priority 1, testB has priority 2, testC has priority 3.Step 2: Understand TestNG runs tests in ascending priority order
Tests run from lowest priority number to highest: 1, then 2, then 3.Final Answer:
testA, testB, testC -> Option CQuick Check:
Priority order = 1, 2, 3 [OK]
- Assuming higher priority number runs first
- Ignoring priority and expecting alphabetical order
- Confusing priority with dependsOnMethods
testB to run only after testA completes successfully?@Test
public void testA() { }
@Test(dependsOnMethods = {"testA"})
public void testB() { }
@Test(dependsOnMethods = {"testC"})
public void testC() { }Solution
Step 1: Analyze dependencies declared
testB depends on testA, which is correct. testC depends on "testC" itself, which is a circular dependency.Step 2: Understand circular dependency impact
A test depending on itself causes TestNG to throw an error and stop execution.Final Answer:
testC depends on itself, causing a circular dependency error -> Option DQuick Check:
Self-dependency causes error [OK]
- Thinking testB dependency is wrong
- Adding dependsOnMethods to testA unnecessarily
- Ignoring circular dependency errors
loginTest, searchTest, and logoutTest. searchTest should run only after loginTest passes, and logoutTest should run last. Which TestNG setup correctly structures this execution?Solution
Step 1: Check loginTest priority and dependencies
loginTest has priority 1 and no dependencies, so it runs first.Step 2: Verify searchTest depends on loginTest and runs second
searchTest has priority 2 and depends on loginTest, so it runs after loginTest passes.Step 3: Confirm logoutTest runs last with priority 3
logoutTest has priority 3 and no dependencies, so it runs last.Final Answer:
loginTest (priority=1), searchTest (priority=2, dependsOnMethods={"loginTest"}), logoutTest (priority=3) -> Option AQuick Check:
loginTest -> searchTest -> logoutTest with correct priority and dependsOnMethods [OK]
- Setting wrong priority numbers
- Wrong dependsOnMethods causing wrong order
- Making logoutTest depend on searchTest incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
