If I have class A and class B extends A, and both have the same @BeforeMethod method, then parent (A) is run before the child (B) [...]
Yes they will.
@BeforeMethod methods will be executed in the order of inheritance - first in the upper superclass, then in the inheritance chain. @AfterMethod works in reverse order (up the inheritance chain).
Note, however, that ordering multiple annotated methods within the same class is not guaranteed (therefore, it is best to avoid this).
Reading the code seems to have occurred in all versions of TestNG, however it was only registered in October 2016:
The annotations above will also be honored (inherited) when you place the superclass of the TestNG class. This is useful, for example, to centralize the installation of a test for several test classes in a common superclass.
In this case, TestNG guarantees that the @Before methods are executed in the order of inheritance (first the highest superclass, then down the inheritance chain) and the @After methods in the reverse order (up the inheritance chain).
See documentation-main.html on GitHub or online documentation .
Disclaimer: It was I who wrote and sent this supplement to the documents.
sleske
source share