Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestNG - How to force end the entire test suite from the BeforeSuite annotation if a condition is met

Is there a way to quit the entire test suite if a condition is met in the @BeforeSuite annotation? Maybe a way to call the @AfterSuite and bypass the entire test?

I make a database call in the @BeforeSuite. If the query returns any results, I send an email and now am wanting to kill the entire test suite.

I have tried System.exit(1); and org.testng.Assert.fail("There are unpaid invoices");, but neither of those terminate the entire suite. My scripts are setup to run classes in parallel and when I run the test from the test.xml file, each class tries to start and opens a window and then immediately closes it.

FYI, the drivers do not get created until the @BeforeClass or @BeforeMethod (depending on the switch I created for parallel methods or classes). So in all reality, there should never even be an attempt to open a browser window.

like image 411
Dustin N. Avatar asked Nov 03 '16 20:11

Dustin N.


2 Answers

try new SkipException("message"); this will skip the tests if the provided condition is not true.

like image 169
Shek Avatar answered Sep 20 '22 00:09

Shek


Try the below code in beforesuite annotation method , check if the run mode of your suite is Y/N. if that's N Then throw an Exception

throw new skipException("desired Message")

Please keep it in mind don't catch the skip exception in try and catch block. otherwise it will go for the execution of testcases in that suite after throwing skip Exception

package com.qtpselenium.suiteA;

import org.testng.SkipException;
import org.testng.annotations.BeforeSuite;

import com.qtpselenium.base.TestBase;
import com.qtpselenium.util.TestUtil;

public class TestSuiteBase extends TestBase{


    @BeforeSuite
    public void checksuiteskip(){ 


            //Initialize method of Test BASE Class to Initialize the logs and all the excel files
            try {
                Initialize();
            } catch (Exception e) {
                e.printStackTrace();
            }
             App_Logs.debug("checking run mode of SuiteA");
            if( !TestUtil.isSuiterunnable(suitexlsx, "suiteA")){

               App_Logs.debug("Run mode for SuiteA is N");
               throw new SkipException("Run mode for suiiteA is N");


           }else

               App_Logs.debug("Run mode for SuiteA is Y");


        } 


    }
like image 22
Arpan Saini Avatar answered Sep 19 '22 00:09

Arpan Saini