[英]Conditional skipping of TestNG tests
我對TestNG注釋沒有多少經驗,但我正在嘗試使用TestNG框架和零售網站的POM設計模式構建測試套件。 我打算使用數據驅動的方法。 我的計划是通過excel驅動我的測試場景,而不是使用testng.xml。
例如,我將擁有多個測試套件,它們只是包名稱TestSuite下的各種類文件。 TestSuite名稱將列在excel中,用戶可以通過將運行模式更改為TRUE / FALSE來設置測試套件的運行模式。 在這里,我計划實現一個條件檢查,看看Run模式是否為FALSE,並相應地跳過testsuite,即testsuite類。
我們是否有任何直接的方法來使用TestNG來實現相同的目的,或者請用一個小例子來建議任何解決方案。
請在測試開始時添加以下代碼行,這將跳過您的測試用例
throw new SkipException("Skipping the test case");
您可以使用TestNG的注釋轉換器將@Test
注釋的disabled
屬性設置為true或false。
例:
public class MyTransformer implements IAnnotationTransformer {
public void transform(ITest annotation, Class testClass,
Constructor testConstructor, Method testMethod){
if (isTestDisabled(testMethod.getName()))) {
annotation.setEnabled(false);
}
}
public boolean isTestDisabled(String testName){
// Do whatever you like here to determine if test is disabled or not
}
}
從6.13開始throw new SkipException("Skipping the test case");
不行。 我必須在拋出異常之前設置狀態,否則測試狀態將是失敗而不是跳過:
iTestResult.setStatus(TestResult.SKIP); throw new SkipException("Skipping the test case");
可能會在下一版本中修復/更改,有關詳細信息,請參閱https://github.com/cbeust/testng/issues/1632 。
throw new skipException("skipping the test case")
它將跳過測試用例而不是完整的套件。 不要檢查套件運行模式,而是在@beforeTest
方法@beforeTest
測試用例的運行模式檢查為Y / N. 然后,如果您發現運行模式為N,則拋出異常。
throw new skipException("skipping test case as run mode is y").
這將跳過您的測試用例。 這只是一個替代方案,即使我沒有找到任何其他方式來跳過整個套件。 如果您不想運行該套件,上面的案例將填充只需要將每個測試用例的運行模式保持為N的目的。 它將跳過該套件的所有測試用例,並且將成為您的報告中跳過這些測試用例的一部分。
實例如下
package com.qtpselenium.suiteC;
import org.testng.SkipException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.qtpselenium.util.TestUtil;
public class TestCaseC1 extends TestSuiteBase{
//Checking runMode of the Test case in Suite
@BeforeTest
public void checkTestcaseskip(){
//this.getclass().getSimpleName() method returns the name of the class
App_Logs.debug("checking run mode of " +this.getClass().getSimpleName() + " testcase");
if(!TestUtil.IsTestCaseRunnable(suiteCxlsx, this.getClass().getSimpleName())){
App_Logs.debug("Run mode of testcase " + this.getClass().getSimpleName() + " is N");
throw new SkipException("Run mode of testcase " + this.getClass().getSimpleName() + " is N");
}else
App_Logs.debug("Run mode of testcase " + this.getClass().getSimpleName() + " is Y");
}
@Test(dataProvider="getTestData")
public void TestcaseC1(
String col1,
String col2,
String col3,
String col4
){
App_Logs.debug("Test data of testcase : " + this.getClass().getSimpleName());
App_Logs.debug(col1+"--"+col2+"--"+col3+"--"+col4);
}
//Data provide to TestcaseC1
@DataProvider
public Object[][] getTestData(){
return TestUtil.getdata(suiteCxlsx, this.getClass().getSimpleName());
}
}
我發現的最佳解決方案,我們可以在@BeforeTest Annotation方法中檢查套件的運行模式,如果它找到N,那么拋出新的跳過異常,它會跳過該套件的所有測試用例,我抓錯了try catch塊中的異常,這就是為什么它會在throw skip異常后檢查所有測試用例。
如果在套件Excel中找到套件運行模式為N,請在右下方的示例中找到如何跳過套件中的所有測試用例。 通過testng.xml運行它
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");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.