What does 'dependency between tests' mean in software testing?
It means one test relies on another test to run first or to pass before it can run correctly.
Click to reveal answer
beginner
Why should we avoid dependencies between tests?
Because if one test fails, dependent tests may also fail, making it hard to find the real problem.
Click to reveal answer
intermediate
How can you declare a test dependency in TestNG with Selenium Java?
Use the @Test annotation with 'dependsOnMethods' attribute, for example: @Test(dependsOnMethods = {"testLogin"})
Click to reveal answer
intermediate
What happens if a test that others depend on fails in TestNG?
The dependent tests are skipped automatically to avoid false failures.
Click to reveal answer
beginner
Give one real-life example of test dependency.
Testing 'Add to Cart' depends on 'Login' test passing first, because you must be logged in to add items.
Click to reveal answer
In TestNG, how do you specify that a test depends on another test?
A@Test(dependsOnMethods = {"methodName"})
B@DependsOnTest("methodName")
C@Test(priority = 1)
D@Test(enabled = false)
✗ Incorrect
The correct way is to use @Test with dependsOnMethods attribute listing the method names.
What is a risk of having dependencies between tests?
ATests run faster
BTests become independent
CTests always pass
DFailure in one test can cause others to skip or fail
✗ Incorrect
If one test fails, dependent tests may skip or fail, hiding the real issue.
If test A depends on test B, and test B fails, what happens to test A in TestNG?
ATest A runs anyway
BTest A passes automatically
CTest A is skipped
DTest A runs before test B
✗ Incorrect
TestNG skips dependent tests if the tests they depend on fail.
Which of the following is a good practice regarding test dependencies?
AAvoid dependencies to keep tests independent
BMake all tests dependent on each other
CWrite tests that never fail
DRun tests in random order
✗ Incorrect
Avoiding dependencies helps tests run independently and makes debugging easier.
What attribute in TestNG @Test annotation controls test dependencies?
Apriority
BdependsOnMethods
Cenabled
Dgroups
✗ Incorrect
dependsOnMethods specifies which tests must run and pass before the current test.
Explain what test dependency is and why it can be problematic.
Think about how one test relying on another can affect test results.
You got /3 concepts.
Describe how to declare a test dependency in Selenium Java using TestNG and what happens if the dependent test fails.
Recall the TestNG annotation and test execution flow.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using dependsOnMethods in Selenium Java tests?
easy
A. To skip tests that have no assertions
B. To run all tests in parallel regardless of order
C. To automatically retry failed tests
D. To make one test run only after another test has passed
Solution
Step 1: Understand test dependencies
The dependsOnMethods attribute is used to specify that a test should run only after another test method has successfully completed.
Step 2: Recognize the effect on test execution order
This ensures that dependent tests do not run if the test they depend on fails, maintaining logical test flow.
Final Answer:
To make one test run only after another test has passed -> Option D
Quick Check:
dependsOnMethods controls test order and dependency [OK]
Hint: dependsOnMethods links tests to run in sequence [OK]
Common Mistakes:
Thinking dependsOnMethods runs tests in parallel
Assuming it retries failed tests automatically
Believing it skips tests without assertions
2. Which of the following is the correct syntax to make testB depend on testA in TestNG?
easy
A. @Test(dependsOnMethods = testA) public void testB() {}
B. @Test(dependsOnMethods = {"testA"}) public void testB() {}
C. @Test(dependsOn = "testA") public void testB() {}
D. @Test(dependOn = "testA") public void testB() {}
Solution
Step 1: Recall TestNG annotation syntax
The correct attribute name is dependsOnMethods and it expects an array of method names as strings.
Step 2: Check the syntax correctness
@Test(dependsOnMethods = {"testA"}) public void testB() {} correctly uses @Test(dependsOnMethods = {"testA"}) with the method name in quotes inside braces.
Final Answer:
@Test(dependsOnMethods = {"testA"}) public void testB() {} -> Option B
Hint: Use dependsOnMethods with braces and quotes [OK]
Common Mistakes:
Using dependsOn instead of dependsOnMethods
Omitting quotes around method name
Not using braces for the array
3. Given the following TestNG code:
@Test
public void login() { Assert.assertTrue(false); }
@Test(dependsOnMethods = {"login"})
public void dashboard() { System.out.println("Dashboard loaded"); }
What will happen when these tests run?
medium
A. login fails, dashboard is skipped
B. login fails, dashboard runs and prints message
C. Both login and dashboard run and pass
D. login passes, dashboard is skipped
Solution
Step 1: Analyze login test result
The login test asserts false, so it will fail.
Step 2: Understand effect on dependent test
The dashboard test depends on login. Since login failed, dashboard will be skipped and not run.
Final Answer:
login fails, dashboard is skipped -> Option A
Quick Check:
Dependent tests skip if dependency fails [OK]
Hint: Failed dependency causes dependent test to skip [OK]
Common Mistakes:
Assuming dependent test runs despite failure
Thinking both tests pass
Confusing skip with fail
4. Identify the error in this TestNG test dependency code:
@Test(dependsOnMethods = "setup")
public void testFeature() {}
@Test
public void setup() {}
medium
A. setup method must be private
B. testFeature should not depend on setup
C. dependsOnMethods should be an array of strings, not a single string
D. setup method must have parameters
Solution
Step 1: Check dependsOnMethods syntax
The attribute dependsOnMethods requires an array of method names, even if only one method is listed.
Step 2: Identify the syntax mistake
Here, dependsOnMethods = "setup" is a single string, missing braces to make it an array.
Final Answer:
dependsOnMethods should be an array of strings, not a single string -> Option C
Quick Check:
dependsOnMethods needs braces for method names [OK]
Hint: Always use braces {} for dependsOnMethods even if one method [OK]
Common Mistakes:
Using single string instead of string array
Making dependent method private
Adding parameters to setup method unnecessarily
5. You have three tests: init(), login(), and purchase(). login() depends on init(), and purchase() depends on login(). If init() fails, what happens when you run all tests together?
hard
A. init fails, login and purchase are skipped
B. init fails, login runs but purchase is skipped
C. init fails, login and purchase run anyway
D. init fails, only purchase runs
Solution
Step 1: Understand the dependency chain
login depends on init, and purchase depends on login, forming a chain: init -> login -> purchase.
Step 2: Analyze failure impact
If init fails, login will be skipped because it depends on init. Since login is skipped, purchase will also be skipped because it depends on login.
Final Answer:
init fails, login and purchase are skipped -> Option A
Quick Check:
Failure cascades down dependent tests [OK]
Hint: Failure in first test skips all dependent tests down the chain [OK]
Common Mistakes:
Assuming dependent tests run despite failure
Thinking only immediate dependent test skips
Believing tests run independently
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.