如何生成失败的junit报告

时间:2015-04-22 16:44:45

标签: ant junit selenium-webdriver junit3 junit-runner

我有junit testscript,它创建了不同的唯一ID。因此,当它找到现有ID或错误的Id时,我希望通过ANT的测试脚本报告显示它对于后续记录失败,但是传递给其他正确的记录。

                @Test
                public void testCreateTrade() throws Exception
                driver.findElement(By.id("VIN")).clear();
                driver.findElement(By.id("VIN")).sendKeys(vVin);
                String str = driver.getCurrentUrl();
                if(str.contains("step1")) // for existing ID
                {
                    driver.findElement(By.cssSelector("body > div.bootbox.modal.in > div.modal-footer > a.btn.null")).click();
                    break;
                }
                driver.findElement(By.id("mileage")).sendKeys(vMileage);
                driver.findElement(By.id("odometerType")).sendKeys(vKm);
                driver.findElement(By.id("passengers")).sendKeys(vPassengers);
                driver.findElement(By.id("exteriorColor")).sendKeys(vExterior);
                driver.findElement(By.id("interiorColor")).sendKeys(vInterior);
                driver.findElement(By.id("hasAccident")).sendKeys(vAccident);
                driver.findElement(By.id("dealerSalesPerson")).sendKeys(vSalesPerson);
                driver.findElement(By.id("step3btn")).click();
                Thread.sleep(1000);
                String str3 = driver.getCurrentUrl();
                if(str3.contains("step2")) // Loop for wrong ID
                {
                    driver.findElement(By.linkText("Create")).click();
                    driver.findElement(By.xpath("html/body/div[7]/div[2]/a[1]")).click();
                    //System.out.println("Is a wrong Vin"+vVin);
                    break;
                }
                driver.findElement(By.id("step4btn")).click();
                driver.findElement(By.id("windshieldCondition")).sendKeys(vWindshield);
                driver.findElement(By.id("tireCondition")).sendKeys(vTire);
                driver.findElement(By.id("accidentBrand3")).sendKeys(vAcBrand);
                driver.findElement(By.id("confirmedParked")).click();

1 个答案:

答案 0 :(得分:0)

如果你想让一个测试用例继续运行,那么"失败"然后在最后报告其例外情况,使用ErrorCollector

@RunWith(JUnit4.class) public class YourTestClass {
  @Rule public ErrorCollector errorCollector = new ErrorCollector();

  @Test public void yourTest() {
    // ... (your setup)
    for (Record record : expectedRecords) {
      if (dataSource.hasRecord(record.getId())) {
        Record fetchedRecord = dataSource.getRecord(record.getId());
        errorCollector.checkThat(record, matchesRecordValuesOf(record));
      } else {
        errorCollector.addError(new IllegalStateException(""));
      }
    }
  }
}

但请注意,每单位测试只测试一件事总是比较好。这可能有意义,但不要过度使用ErrorCollector,重构和拆分测试更有意义。