Bird
Raised Fist0
Selenium Javatesting~20 mins

Why TestNG structures test execution in Selenium Java - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
TestNG Execution Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of TestNG Test Execution Structure

Why does TestNG organize tests using annotations like @BeforeMethod, @Test, and @AfterMethod?

ATo make tests run only once without any setup or cleanup
BTo force tests to run in alphabetical order by method name
CTo control the order of test execution and setup/cleanup steps automatically
DTo disable parallel execution of tests by default
Attempts:
2 left
💡 Hint

Think about how setup and cleanup code can be reused for each test.

❓ Predict Output
intermediate
2:00remaining
TestNG Execution Order Output

What will be the output order when running this TestNG test class?

Selenium Java
import org.testng.annotations.*;

public class SampleTest {

  @BeforeMethod
  public void setup() {
    System.out.println("Setup");
  }

  @Test
  public void testA() {
    System.out.println("Test A");
  }

  @Test
  public void testB() {
    System.out.println("Test B");
  }

  @AfterMethod
  public void cleanup() {
    System.out.println("Cleanup");
  }
}
A
Setup
Cleanup
Setup
Cleanup
Test A
Test B
B
Test A
Test B
Setup
Cleanup
Setup
Cleanup
C
Setup
Test A
Test B
Cleanup
Cleanup
D
Setup
Test A
Cleanup
Setup
Test B
Cleanup
Attempts:
2 left
💡 Hint

Remember that @BeforeMethod and @AfterMethod run before and after each @Test method.

❓ assertion
advanced
2:00remaining
Correct Assertion for TestNG Test Result

Which assertion correctly verifies that a web page title equals "Home Page" in a TestNG Selenium test?

Selenium Java
String actualTitle = driver.getTitle();
AAssert.assertEquals(actualTitle, "Home Page");
BAssert.assertTrue(actualTitle = "Home Page");
CAssert.assertEquals("Home Page", actualTitle, false);
DAssert.assertTrue(actualTitle.equalsIgnoreCase("Home Page"));
Attempts:
2 left
💡 Hint

Check the correct method signature for equality assertion in TestNG.

🔧 Debug
advanced
2:00remaining
Identify the TestNG Execution Problem

Given this TestNG test class, why does the @AfterMethod not run after testLogin?

Selenium Java
import org.testng.annotations.*;

public class LoginTest {

  @BeforeMethod
  public void setup() {
    System.out.println("Setup");
  }

  @Test
  public void testLogin() {
    System.out.println("Login Test");
    throw new RuntimeException("Fail");
  }

  @AfterMethod
  public void cleanup() {
    System.out.println("Cleanup");
  }
}
ABecause the exception in testLogin stops the @AfterMethod from running
BBecause @AfterMethod is missing the alwaysRun=true attribute
CBecause @BeforeMethod and @AfterMethod must be static methods
DBecause @AfterMethod only runs if @Test passes without exceptions
Attempts:
2 left
💡 Hint

Consider how TestNG handles cleanup when tests throw exceptions.

❓ framework
expert
3:00remaining
TestNG Parallel Execution Behavior

When TestNG is configured to run tests in parallel by methods, what is the expected behavior regarding @BeforeMethod and @AfterMethod?

A<code>@BeforeMethod</code> and <code>@AfterMethod</code> run in the same thread as their test method, independently for each test
B<code>@BeforeMethod</code> and <code>@AfterMethod</code> run sequentially in the main thread regardless of parallel tests
C<code>@BeforeMethod</code> runs once before all tests, and <code>@AfterMethod</code> runs once after all tests
D<code>@BeforeMethod</code> and <code>@AfterMethod</code> are skipped when tests run in parallel
Attempts:
2 left
💡 Hint

Think about thread safety and how setup/cleanup should behave when tests run at the same time.

Practice

(1/5)
1. Why does TestNG structure test execution using features like priority and dependsOnMethods?
easy
A. To make tests run randomly without any order
B. To control the order and dependencies between tests for smoother runs
C. To skip all tests automatically
D. To run tests only once without retries

Solution

  1. Step 1: Understand TestNG test execution control

    TestNG uses features like priority and dependsOnMethods to manage the order and dependencies of tests.
  2. Step 2: Recognize the benefit of structured execution

    This control helps tests run smoothly and find problems faster by running in a logical order.
  3. Final Answer:

    To control the order and dependencies between tests for smoother runs -> Option B
  4. Quick Check:

    TestNG structures execution = Control order & dependencies [OK]
Hint: TestNG orders tests to avoid chaos and errors [OK]
Common Mistakes:
  • Thinking TestNG runs tests randomly
  • Believing TestNG skips tests automatically
  • Assuming TestNG runs tests only once without control
2. Which of the following is the correct way to set test priority in TestNG?
easy
A. @Test(priority = 1)
B. @Test(order = 1)
C. @Test(sequence = 1)
D. @Test(rank = 1)

Solution

  1. Step 1: Recall TestNG annotation syntax

    TestNG uses @Test(priority = number) to set the execution order of tests.
  2. Step 2: Identify the correct attribute name

    The correct attribute is priority, not order, sequence, or rank.
  3. Final Answer:

    @Test(priority = 1) -> Option A
  4. Quick Check:

    TestNG priority syntax = @Test(priority = x) [OK]
Hint: Use 'priority' attribute inside @Test for order [OK]
Common Mistakes:
  • Using wrong attribute names like order or rank
  • Missing parentheses after @Test
  • Confusing priority with dependsOnMethods
3. Given the following TestNG code, what will be the order of test execution?
@Test(priority = 2)
public void testB() { }

@Test(priority = 1)
public void testA() { }

@Test(priority = 3)
public void testC() { }
medium
A. testA, testC, testB
B. testB, testA, testC
C. testA, testB, testC
D. testC, testB, testA

Solution

  1. Step 1: Identify priorities assigned to each test

    testA has priority 1, testB has priority 2, testC has priority 3.
  2. Step 2: Understand TestNG runs tests in ascending priority order

    Tests run from lowest priority number to highest: 1, then 2, then 3.
  3. Final Answer:

    testA, testB, testC -> Option C
  4. Quick Check:

    Priority order = 1, 2, 3 [OK]
Hint: Lower priority number runs first in TestNG [OK]
Common Mistakes:
  • Assuming higher priority number runs first
  • Ignoring priority and expecting alphabetical order
  • Confusing priority with dependsOnMethods
4. What is wrong with this TestNG test method if we want testB to run only after testA completes successfully?
@Test
public void testA() { }

@Test(dependsOnMethods = {"testA"})
public void testB() { }

@Test(dependsOnMethods = {"testC"})
public void testC() { }
medium
A. testA should have dependsOnMethods attribute
B. testB does not depend on testA correctly
C. No errors, code is correct
D. testC depends on itself, causing a circular dependency error

Solution

  1. Step 1: Analyze dependencies declared

    testB depends on testA, which is correct. testC depends on "testC" itself, which is a circular dependency.
  2. Step 2: Understand circular dependency impact

    A test depending on itself causes TestNG to throw an error and stop execution.
  3. Final Answer:

    testC depends on itself, causing a circular dependency error -> Option D
  4. Quick Check:

    Self-dependency causes error [OK]
Hint: Avoid tests depending on themselves [OK]
Common Mistakes:
  • Thinking testB dependency is wrong
  • Adding dependsOnMethods to testA unnecessarily
  • Ignoring circular dependency errors
5. You want to run three tests: loginTest, searchTest, and logoutTest. searchTest should run only after loginTest passes, and logoutTest should run last. Which TestNG setup correctly structures this execution?
hard
A. @Test(priority = 1) public void loginTest() {} @Test(priority = 2, dependsOnMethods = {"loginTest"}) public void searchTest() {} @Test(priority = 3) public void logoutTest() {}
B. @Test(priority = 2) public void loginTest() {} @Test(priority = 4, dependsOnMethods = {"loginTest"}) public void searchTest() {} @Test(priority = 3) public void logoutTest() {}
C. @Test(priority = 3) public void loginTest() {} @Test(priority = 2, dependsOnMethods = {"logoutTest"}) public void searchTest() {} @Test(priority = 1) public void logoutTest() {}
D. @Test(priority = 1) public void loginTest() {} @Test(priority = 3, dependsOnMethods = {"logoutTest"}) public void searchTest() {} @Test(priority = 2) public void logoutTest() {}

Solution

  1. Step 1: Check loginTest priority and dependencies

    loginTest has priority 1 and no dependencies, so it runs first.
  2. Step 2: Verify searchTest depends on loginTest and runs second

    searchTest has priority 2 and depends on loginTest, so it runs after loginTest passes.
  3. Step 3: Confirm logoutTest runs last with priority 3

    logoutTest has priority 3 and no dependencies, so it runs last.
  4. Final Answer:

    loginTest (priority=1), searchTest (priority=2, dependsOnMethods={"loginTest"}), logoutTest (priority=3) -> Option A
  5. Quick Check:

    loginTest -> searchTest -> logoutTest with correct priority and dependsOnMethods [OK]
Hint: Use priority ascending and dependsOnMethods for order [OK]
Common Mistakes:
  • Setting wrong priority numbers
  • Wrong dependsOnMethods causing wrong order
  • Making logoutTest depend on searchTest incorrectly
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