17

My question is basically the same as this SOF question, but deals with @BeforeMethod instead of @BeforeClass for TestNG.

Does test class inheritance play a factor when determining the order that @BeforeMethod annotated methods will execute? If I have class A and a class B extends A and both have one @BeforeMethod method, then will the parent's (A) run before the child's (B) or will the child's run before the parent, or does the order depend on some other factor such as alphabetical order of the method name. I'm trying to see if there is an inheritance order that I can rely on instead of having to use parameters of the annotation such as dependsOnMethods.

Community
  • 1
  • 1
ecbrodie
  • 11,246
  • 21
  • 71
  • 120

3 Answers3

11

If I have class A and a class B extends A and both have one @BeforeMethod method, then will the parent's (A) run before the child's (B) [...]

Yes, they will.

@BeforeMethod methods will run in inheritance order - the highest superclass first, then going down the inheritance chain. @AfterMethod methods run in reverse order (up the inheritance chain).

Note, however, that the ordering of multiple annotated methods within one class is not guaranteed (so it's best to avoid that).


Reading the code, this seems to have been the case in all versions of TestNG, however it was only documented in October 2016:

The annotations above will also be honored (inherited) when placed on a superclass of a TestNG class. This is useful for example to centralize test setup for multiple test classes in a common superclass.

In that case, TestNG guarantees that the "@Before" methods are executed in inheritance order (highest superclass first, then going down the inheritance chain), and the "@After" methods in reverse order (going up the inheritance chain).

See documentation-main.html on GitHub, or the online documentation.

Disclaimer: It was me who wrote and submitted this addition to the docs.

sleske
  • 81,358
  • 34
  • 189
  • 227
1

As explained in the other answers, TestNG will consider inheritance for execution order.

If you prefer to make the order of the "Before" methods explicit, you can also have the child simply call the parent setup.

public class ChildTest extends ParentTest {

    @BeforeMethod
    public void setup() {
       super.setup();
       // Child setup code follows here.
    }
}

public class ParentTest {
    @BeforeMethod // Only need if test will ever be invoked directly
    public void setup() {
      // Parent setup goes here.
    }
}
sleske
  • 81,358
  • 34
  • 189
  • 227
James Gawron
  • 871
  • 10
  • 27
  • 2
    "This happens in JUnit, but not TestNG" This is wrong, TestNG also does this (see my answer). Your solution also works, but I prefer not having to invoke the parent setup method manually in every test class (plus you may forget to invoke it). – sleske Oct 07 '16 at 13:44
  • Sorry, as I wrote in my answer, I checked the TestNG sources, and the current behaviour seems to have been present in earlier versions, too. At any rate, the answer is no longer correct as of 2019. I submitted an edit, and removed the downvote :-). – sleske Nov 17 '19 at 12:43
-2

Order has nothing to do with the inheritance of the class.

You can try to set the priority attribute on the @Test annotation. If no parameters are set in the annotation, they run in an alphabetical order.

You also set a prefix to the methods according to the order you want them to run (e.g. 01_method1(), 02_method2()).

ecbrodie
  • 11,246
  • 21
  • 71
  • 120
user3723314
  • 67
  • 1
  • 11
  • The question is clearly about config annotations(like BeforeMethod) order of execution which does depend on inheritance and it is not about @Test annotation – Rohit Mar 15 '21 at 10:14