5.6-dependent methods
Sometimes you need your test methods to be invoked in a particular order. This is very useful, for example:
* Verify that a specific number of test method calls are complete and successful before running more test methods
* Initialize the test and expect the initialization method to also be the test method (the method marked as @before/after will not be part of the final report)
To do this, you need to use the Dependsonmethods attribute of the @test annotation or the dependsongroups attribute.
There are two kinds of dependencies:
* Strong Reliance. All dependent methods must run and succeed before running your test methods. Even if a dependency method fails, the test method is not invoked and will be marked as skip in the report.
* Soft dependence. Test methods are always run after a dependent method is run, even if some dependent methods fail. This is useful for situations where you just want to confirm that the test method is running in a specific order and that the test method does not really depend on the success of other methods. Soft dependencies are implemented by adding "alwaysrun=true" to the @test annotation.
Here is an example of a strong reliance:
@Test
public void serverStartedOk() {}
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}
In this example, METHOD1 () is declared dependent on the method Serverstartedok (), which guarantees that the Serverstartedok () method will always be invoked first.
You can also make the method dependent on the complete test group:
@Test(groups = { "init" })
public void serverStartedOk() {}
@Test(groups = { "init" })
public void initEnvironment() {}
@Test(dependsOnGroups = { "init.* })
public void method1() {}
In this example, METHOD1 () is declared dependent on any group that matches the regular expression "init.*", which guarantees that the method Serverstartedok () and initenvironment () are always invoked before method1 ().
Note: The order of the methods that belong to the same group is not guaranteed to be the same during the test run, as explained earlier. If a method's dependencies fail and are strongly dependent (default Alwaysrun=false), this method will not be marked as fail but skip. It is important that the skipped method report in the final report (in HTML other than red and green), because the skipped method does not necessarily fail.
Both Dependsongroups and dependsonmethods accept regular expressions as arguments. For Dependsonmethods, if the method you rely on is coincidental with multiple overloaded versions, all loaded methods will be invoked. If you only want to invoke one of the overloaded methods, use Dependsongroups.
For a more advanced example of method dependency, refer to this document and use inheritance to provide an elegant solution to deal with multiple dependencies.