TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    TestNG

    TestNG Dependencies

    Introduction

    TestNG dependencies allows us to specify dependency methods or dependency groups for test methods. In the test code, we can specify the dependencies using the @Test annotation attributes:

    • dependsOnMethods = The list of methods this method depends on
    • dependsOnGroups = The list of groups this method depends on

    TestNG guarantees that the methods depended upon will be run before the test method that contains this annotation is run. However, there is no guarantee on the order of the methods.

    We can also specify the dependencies using the <dependencies> tag in the testng.xml suite file.

    dependsOnMethods

    The methods should be in the same Test class or in the parent class. If the methods are overloaded, all the overloaded methods will run.

    If we specify a method of other class the test would org.testng.TestNGException depends on nonexistent method exception.

    Types

    There are two types of dependencies.

    • Hard Dependency.
    • Soft Dependency.

    To know more about the types:

    https://www.testingdocs.com/hard-and-soft-dependency-in-testng-framework/

     

    Dependency is a handy tool in TestNG framework to make some methods run in the order you specify. TestNG by default executes the tests in ascending order of their names. By default the dependent method is skipped when there is a failure in the method you specify as a dependency.

    Code Listing

     

    package com.testingdocs.testng.web;
    
    //www.TestingDocs.com - TestNG Tutorials
    import org.testng.annotations.Test;
    
    public class WebTests {
    
     /***************************************************************
     * Run only if all the methods from "deployApplicatio"
     * group are passed.
     ***************************************************************/
     @Test(dependsOnGroups="deployApplication")
     public void init() {
     System.out.println("Initializations..");
     }
    
     /**************************************************************
     * Run if "init" method is passed.
     **************************************************************/
     @Test(dependsOnMethods = {"init"})
     public void testAppMethod() {
     System.out.println("Web Application Test...");
     }
    }

     

    init() method depends on group of tests called “DeployApplication”. The testAppMethod() depends on the init method. The deploy tests are:

     

    package com.testingdocs.testng.deploy;
    
    //www.TestingDocs.com - TestNG Tutorials
    import org.testng.annotations.Test;
    
    public class DeployTest {
    
     @Test(groups="deployApplication")
     public void deployApp() {
     System.out.println("Application War deploy ...");
     }
    
     @Test(groups="deployApplication",dependsOnMethods="deployApp")
     public void basicTest() {
     System.out.println("Basic Application Test...");
     }
    }

     

    There are 2 tests in the deployApplication test group.  All the method in the deploy group are run before the init() method. This is because the init() method depends on the deploy group:

     

    @Test(dependsOnGroups="deployApplication") 
    public void init() {
    ...}

     

     

    In the same fashion, init() is run before the testAppMethod(). This is because the testAppMethod depends on the init() method.

     

    TestNG Tutorials on this website can be found at:

    https://www.testingdocs.com/testng-framework-tutorial/

    For more details on the TestNG Framework, visit the official website of TestNG at:

    https://testng.org

    Related Posts

    Tests TestNG Suite testng.xml

    TestNG /

    Run tests from TestNG Suite testng.xml file

    Add TestNG library

    TestNG /

    Add the TestNG library to the project

    new TestNG class

    TestNG /

    Create TestNG test class in Eclipse IDE.

    TestNG Plugin IntelliJ

    TestNG /

    Enable TestNG in IntelliJ IDE

    Installing Eclipse from Update Site

    TestNG /

    Install TestNG latest version from the update site

    ‹ TestNG Test Groups› Sample TestNG suite file with Custom Listener

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com