簡體   English   中英

當使用工廠和數據提供者注釋方法時,如何限制特定方法僅執行一次?

[英]How to restrict the particular method to be executed once only, When factory and dataprovider annotation method is used?

  1. 我正在自動化數據庫自動化。 我在輸入中使用@factory和@Dataprovider批注。
  2. 我想限制此方法,只要getCountOfPt1(poiLocId)即可單獨運行
  3. 我也嘗試設置布爾值,但是失敗,因為我使用的是工廠以及dataprovider注釋。
  4. 我只想限制執行一次的代碼是
 String pt1 = null; if(!alreadyExecuted) { Map<String, Integer> records = DbMr.getCountOfPt1(poiLocId); pt1 = getMaxKey(records); LOG.debug("Max key value is...." + pt1); if (StringUtils.isBlank(pt11)) { records.remove(null); pt1 = getMaxKey(records); alreadyExecuted = true; } } 

注意:我在此方法中傳遞的poiLocId來自工廠方法

@Factory
public Object[] factoryMethod() {
    Object[] poiLocIdData = null;
    if (StringUtils.isNotBlank(cityName)) {
        List<String> poiLocId = DbMr.getPoiLocId(cityName);

        int size = poiLocId.size();
        poiLocIdData = new Object[size];
        for (int i = 0; i < size; i++) {
            poiLocIdData[i] = new CollectsTest(poiLocId.get(i));
        }
    } else {
        LOG.error("The parameter is required: Pass City Name");
        Assert.fail("Problems with parameters");
    }

    return poiLocIdData;
}

public CollectTest(String locationId) {
    poiLocId = locationId;
    this.reportsPath = "reports_" + cityName;
    this.extent = new ExtentReports();
}

@DataProvider(name = "pData")
public Object[][] getPData() {
    List<PData> pList = DbMr.getCollectionPs(poiLocId);

    Object[][] testData = new Object[pList.size()][];
    for (int i = 0; i < poiList.size(); i++) {
        testData[i] = new Object[] { pList.get(i) };
    }
    return testData;
}

@BeforeClass
private void setup() throws Exception {
    ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(reportsPath + "/" +
     cityName + "_extent.html");
     htmlReporter.loadXMLConfig("src/test/resources/extent-config.xml");
    extent.attachReporter(htmlReporter);
}

@Test(dataProvider = "pData")
public void verifyData(PData pData) throws Exception {
    extentTest = extent.createTest(pData.toString());
    String pt1 = null;
    if(!alreadyExecuted) {
    Map<String, Integer> records = DbMr.getCountOfPt1(poiLocId);
    pt1 = getMaxKey(records);
    LOG.debug("Max key value is...." + pt1);
    if (StringUtils.isBlank(pt11)) {
        records.remove(null);
        pt1 = getMaxKey(records);
        alreadyExecuted = true;
    }
    }

    if (pt1.equalsIgnoreCase("xxxx")) {
        Assert.assertEquals(pData.getpt1(), "xxxx");

        }

由於@factory和@DataProvider與測試類的實例一起使用,因此請嘗試使“ alreadyExecuted”變量成為靜態變量。(因為靜態變量處於類級別”)

以下代碼可以正常運行,並且只能運行一次,而我使用map只能執行一次。

// declare it as global variable
private static Map<String, String>LOC_ID_AND_PT1_COUNT_MAP = new HashMap();

//test method
@Test(dataProvider = "pData")
public void verifyData(PData pData) throws Exception {
    extentTest = extent.createTest(pData.toString());
    String pt1 = LOC_ID_AND_PT1_COUNT_MAP.get(LocId);
        if (pt1 == null) {
    Map<String, Integer> records = 
  DbMr.getCountOfPT1(LocId);
            pT1 = getMaxKey(records);
            LOG.debug("Max key value is...." + pt1);
            if (StringUtils.isBlank(pt1)) {
                records.remove(null);
                pt1 = getMaxKey(records);
                LOG.debug("Max key value is...." + pt1);
            }

            LOC_ID_AND_PT1_COUNT_MAP.put(locId, pt1);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM