如果发现Suite运行模式为N,则如何停止在套件中执行所有Testcase,即从Excel中读取

时间:2017-01-21 18:25:58

标签: testng

您好我正在运行我的代码时,我能够从excel读取运行模式为N,系统正在抛出新的SkippException。但仍然在为SuiteB发出skipp异常之后,它将继续执行SuiteB中的所有测试用例。 我想如果在@beforesuite方法中确认了skippException,系统会停止执行该套件的所有Testcase

我的SuiteB中有三节课

TestSuiteBase

TestCaseB1

TestCaseB2

    package com.qtpselenium.suiteB;

    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(){

            try {
                //Initialize method of Test BASE Class to Initialize the logs and all the excel files
                Initialize();
                 App_Logs.debug("checking run mode of SuiteB");
                if( !TestUtil.isSuiterunnable(suitexlsx, "suiteB")){

                   App_Logs.debug("Run mode for suiteB is N");
                   throw new SkipException("Run mode for suiiteB is N");

               }




            } catch (Exception e) {

                e.printStackTrace();
            }


        }

    }

******************TestcaseB1****
package com.qtpselenium.suiteB;

import org.testng.annotations.Test;

public class TestCaseB1 extends TestSuiteBase{

    @Test(dependsOnMethods="com.qtpselenium.suiteB.TestSuiteBase.checksuiteskip")
    public void TestcaseB1(){


        System.out.println("We are in Test case B1");

    }

}
************************TestcaseB2**********

package com.qtpselenium.suiteB;

import org.testng.annotations.Test;

public class TestCaseB2 extends TestSuiteBase{


    @Test(dependsOnMethods="com.qtpselenium.suiteB.TestSuiteBase.checksuiteskip")
    public void TestcaseB2(){


        System.out.println("We are in Test case B2");

    }

}

2 个答案:

答案 0 :(得分:2)

这是因为,在您抛出SkipException后,您也会捕获异常。 将您的beforeSuite更改为以下内容。这应该可以解决你的问题

@BeforeSuite
public void checksuiteskip(){
    //Initialize method of Test BASE Class to Initialize the logs and all the excel files
    Initialize();
     App_Logs.debug("checking run mode of SuiteB");
    if( !TestUtil.isSuiterunnable(suitexlsx, "suiteB")){

       App_Logs.debug("Run mode for suiteB is N");
       throw new SkipException("Run mode for suiiteB is N");
    }
}

答案 1 :(得分:0)

代码已更改,最后代码如下所示,如果套件的运行模式为N,则可以跳过整个套件。

package com.qtpselenium.suiteC;

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(){

        try {
            //Initialize method of Test BASE Class to Initialize the logs and all the excel files
            Initialize();

        } catch (Exception e) {

            e.printStackTrace();
        }

             App_Logs.debug("checking run mode of SuiteC");
            if( !TestUtil.isSuiterunnable(suitexlsx, "suiteC")){

               App_Logs.debug("Run mode for suiteC is N");
               throw new SkipException("Run mode for suiiteC is N");

              }else

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

    }
}