如何限制特定方法只执行一次,当使用factory和dataprovider注释方法时?

时间:2018-06-15 12:50:17

标签: java automation testng testng-dataprovider

  1. 我正在自动化数据库自动化。我正在使用@factory和 @Dataprovider注释输入输入。
  2. 我想限制此方法单独运行一次getCountOfPt1(poiLocId)
  3. 我也尝试设置布尔值,但它失败了,因为我使用的是工厂以及dataprovider注释。
  4. 我想要限制和执行一次的代码是
  5. 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");
    
            }
    

2 个答案:

答案 0 :(得分:0)

由于@factory和@DataProvider使用测试类的实例,因此尝试使&#34;已经执行过&#34;变量作为静态变量。(因为静态变量在类级别&#34;)

答案 1 :(得分:0)

以下代码可以正常工作,并且只能运行一次,我使用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);
        }