2017-07-19 23 views
1

請求您的親切時間和解決方案,以解決我的課程設計中可能存在的缺陷。 下面是一個解釋加上一個工作示例。 感謝您寶貴的時間和關注。重新設計類別 - 將對象傳遞給ITestResult,而無需使用Reporter.getCurrentTestResult()

關於:

  • 我用TestNG 6.11設置測試腳本。
  • 每個測試類別Run1, Run2 etc...源自基礎測試BaseRun
  • 每個測試類包含多個@Test方法。
  • 在運行測試步驟之前,每個@Test方法必須首先獲取unique test session
  • unique test session什麼的ExtentTest一個新的實例爲 當前ExtentReport
  • 一旦測試完成後的結果保存到的程度報告的測試。
  • 重要的是,當從NG xml運行並行套件/測試時,測試必須準確運行。

的問題是在我目前一流的設計:

  • 基類BaseRun不知道唯一的測試會議已催生。
  • 參見getSession()BaseRun

,我目前使用是解決方案:

  • @Test,我手動注入TestSession入ITestResult。
  • 我必須這樣做,以便在@AfterMethod我能夠正確地執行正確的測試會話的報告。

    這些是Run1.java即爲此噴射下的線...
    ITestResult結果= Reporter.getCurrentTestResult();
    result.setAttribute(「session」,testSession);

的問題是:

  • 我怎樣才能避免內部@Test注入TestSession?
  • 有沒有更具活力的方式,可能是抽象的東西?
  • 我目前的課程設計需要做些什麼改變?

Run1.java

public class Run1 extends BaseRun { 

    @Test 
    void runner1(){ 
     ExtentTest testSession = getSession("Testing Runner 1"); 
     ITestResult result = Reporter.getCurrentTestResult(); 
     result.setAttribute("session", testSession); 
     testSession.log(Status.INFO, "performing Runner1 Step1"); 
     testSession.log(Status.INFO, "performing Runner1 Step2"); 
    } 

    @Test 
    void runner2(){ 
     ExtentTest testSession = getSession("Testing Runner 2"); 
     ITestResult result = Reporter.getCurrentTestResult(); 
     result.setAttribute("session", testSession); 
     testSession.log(Status.INFO, "performing Runner2 Step1"); 
     testSession.log(Status.INFO, "performing Runner2 Step2");  
     assertTrue(false); 
    }   
} 

BaseRun.java

public class BaseRun { 

    Reports MyExtentReport; 

    @BeforeSuite void setup(){ MyExtentReport = new Reports(); } 

    @AfterSuite void teardown(){ MyExtentReport.save();} 

    public ExtentTest getSession(String testName){ 
     return MyExtentReport.createTest(testName); 
    } 

    @AfterMethod 
    void doSomeReporting(ITestResult result){  
     ExtentTest extentTest = (ExtentTest) result.getAttribute("session"); 
     if(result.getStatus() == ITestResult.SUCCESS){ 
      extentTest.pass(MarkupHelper.createLabel(result.getMethod().getMethodName() + " passed.", ExtentColor.GREEN)); 
     } 
     else if(result.getStatus() == ITestResult.SKIP){ 
      extentTest.skip(MarkupHelper.createLabel(result.getMethod().getMethodName() + " skipped.", ExtentColor.YELLOW)); 
      extentTest.fail(result.getThrowable()); 
     } 
     else{ 
      extentTest.fail(MarkupHelper.createLabel(result.getMethod().getMethodName() + " failed.", ExtentColor.RED)); 
      extentTest.fail(result.getThrowable()); 
     } 
    } 
} 

Reports.java

public class Reports { 

    public ExtentReports extentReports; 

    public Reports(){ 
     File file = new File(System.getProperty("user.dir") + "\\test-output\\extent.html"); 
     ExtentHtmlReporter reporter = new ExtentHtmlReporter(file); 
     reporter.setAppendExisting(false); 
     extentReports = new ExtentReports(); 
     extentReports.attachReporter(reporter); 
    } 

    public ExtentTest createTest(String testName){ 
     return extentReports.createTest(testName); 
    } 

    public void save(){ 
     extentReports.flush(); 
    } 
} 

回答

1

你可以做類似如下:

  1. 創建使用,你可以表明你想測試方法與相關聯的唯一的會話標識符的註釋。
  2. 現在,您可以增強您的基類以包含@BeforeMethod帶註釋的方法,其中您將內省執行的方法,檢查註釋[在(1)中創建],如果存在,從中提取會話標識並將其注入爲將要執行的屬性@Test方法的ITestResult對象。
  3. 現在在您的@AfterMethod中,您應該能夠輕鬆提取出屬性。

使用這種方法,你不必與提取會話ID,並明確其注入@Test方法的ITestResult對象的所有邏輯來污染你的@Test註解的測試方法。

下面是一個顯示所有這些行爲的示例。

您的註釋可以像下面

import java.lang.annotation.Retention; 
import java.lang.annotation.Target; 

import static java.lang.annotation.ElementType.METHOD; 

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME) 
@Target(METHOD) 
public @interface SessionId { 
    String id(); 
} 

修改後的基類可以像下面

import org.testng.ITestResult; 
import org.testng.annotations.AfterMethod; 
import org.testng.annotations.BeforeMethod; 

import java.lang.reflect.Method; 

public class BaseRun { 
    private String getSession(Method method) { 
     SessionId id = method.getAnnotation(SessionId.class); 
     if (id == null) { 
      return ""; 
     } 
     return id.id(); 
    } 

    @BeforeMethod 
    public void beforeMethod(Method method, ITestResult result) { 
     String id = getSession(method); 
     result.setAttribute("session", id); 
    } 

    @AfterMethod 
    public void afterMethod(ITestResult result) { 
     System.out.println("Session Id = " + result.getAttribute("session")); 
    } 

} 

現在修改後的測試類可以類似於下面

import org.testng.annotations.Test; 

public class Run1 extends BaseRun { 
    @Test 
    @SessionId(id = "Testing Runner 1") 
    public void testMethod() { 
     System.out.println("This is a test case"); 
    } 
} 

希望有所幫助。

注: 我特意跳過指ExtentReports類,因爲包括我將需要加起來程度報告相關的罐子我的類路徑。