TestNG Official Document Chinese version (7)-Method dependence and factory

Source: Internet
Author: User
Tags final regular expression testng

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.