Idos
Idos

Reputation: 15310

Weird behaviour in TestNG with dependencies - no error raised

Update: I have also opened a bug in the GitHub project of TestNG here.


I have made a stupid typographical mistake while defining one of my tests. I have accidentally written the annotation like so:

@Test(dependsOnMethods = {"method1, method2"}, alwaysRun = true)

Do you see the error? I sure didn't (since the real method names are also much longer), and it made me waste almost 2 hours because TestNG offers no help regarding this.

Why it offers no help: As a result of this mistake, no error was raised, everything compiled fine, but when my suite was about the run it just skipped all the tests without any indication why:

__PLAN___
Total tests run: 0, Failures: 0, Skips: 0

There is also no place in the logs/xmls to show that something might be wrong.

Only after a long while I noticed that I meant to write:

@Test(dependsOnMethods = {"method1", "method2"}, alwaysRun = true) 

Meaning that the test actually depends on 2 methods except one long one which obviously didn't exist.

I am now trying to add to our framework the option to alert the user of such mistake so it won't happen to any more people (we are a big company).

Question: Where could I find, in the TestNG's code perhaps, the code/documentation that will allow me to grab the event where TestNG fails, and act upon it? There has to be some place in there where TestNG checks the suite and decides to not run the test because of this problem, where is it?

Upvotes: 3

Views: 656

Answers (1)

skirkpatrick
skirkpatrick

Reputation: 218

The reason the error is ignored is because you've specified alwaysRun=true in your @Test declaration. From Test.java:

/**
 * If set to true, this test method will always be run even if it depends
 * on a method that failed.  This attribute will be ignored if this test
 * doesn't depend on any method or group.
 */
public boolean alwaysRun() default false;

Setting alwaysRun=false or removing it from your @Test declaration completely should give you the error you're expecting.

To answer your question of where TestNG checks for missing dependencies, it's in MethodHelper.java#findDependedUponMethods(ITestNGMethod , ITestNGMethod[]). As you can see, having either alwaysRun or ignoreMissingDependencies set to true (they both default to false) will cause missing method dependencies to be silently ignored. I would avoid setting alwaysRun=true unless you have a specific reason to.

Upvotes: 2

Related Questions