【问题标题】:TestNG run set of test cases in loopTestNG 在循环中运行一组测试用例
【发布时间】:2016-08-04 18:38:46
【问题描述】:

到处搜索但找不到解决方案如何执行以下操作: 例如:

 for (int i = 0; i < 5; i++) {
@Test
public void test1(){
assertThat(assertvalue 1);
}

@Test
public void test2(){
assertThat(assertvalue 2);
}
@Test
public void test3(){
assertThat(assertvalue 3);
}
}

当然,我可以使用 dataProvider 或 invocationCount 并在一个测试用例中执行所有断言,但如果一个断言失败,则整个测试用例都会失败。我想要为 3 个小测试用例单独的一个大测试用例 任何帮助表示赞赏

【问题讨论】:

  • 你能用你的确切用例更新问题吗?为什么你要在一组@Test带注释的方法上选择循环?

标签: java selenium selenium-webdriver testng


【解决方案1】:

不鼓励测试用例之间存在依赖关系。您可以使用SoftAssert 来获得类似的结果——检查每个值 1-5 而不会短路并在 2 失败后停止。

SoftAssert soft = new SoftAssert();
soft.fail("Failure # 1\n");
soft.fail("Failure # 2");
soft.assertAll();

应该输出:

以下断言失败:
失败 #1
失败#2

【讨论】:

    【解决方案2】:

    我尝试了以下代码:

    public class SimpleTest {
        private String param = "";
        public SimpleTest(String param) {
            this.param = param;
        }
    
        @BeforeClass
        public void beforeClass() {
            System.out.println("Before SimpleTest class executed.");
        }
    
        @Test
        public void testMethod1() {
            System.out.println("testMethod parameter value is: " + param);
        }
    
        @Test(dependsOnMethods = "testMethod1")
        public void testMethod2() {
            System.out.println("testMethod parameter value is: " + param);
        }
    }
    
    public class SimpleTestFactory{
    @Factory
    public Object[] factoryMethod() {
        return new Object[] {
                new SimpleTest("one"),
                new SimpleTest("two")
        };
    }
    

    并尝试了以下代码,但它没有保持顺序

    【讨论】:

    • 这将不起作用,除非你已经将 group-by-instances 标记为 true..so 标记为 @julien 解释的第一个
    • 对不起,我的错。与 group-by-instances = true 一起使用。但只有当您在我的示例中将值“一”、“二”、“树”传递给new SimpleTest() 时,它才不会按顺序获取它们。它可以采用“一”、“三”、“二”或其他组合。这是预期的行为还是我错过了什么?提前谢谢你
    • @IlyaG 包含此代码尝试作为编辑和更新主要问题的观察。这个区域是为了完整的答案,你的更新可能很快就会被删除!!!
    【解决方案3】:

    添加调用计数

    @Test(priority = 350, groups = "success" ,invocationCount = 10)
        public void test1(){
            assertThat(assertvalue 1);
        }
    

    【讨论】:

      【解决方案4】:

      使用“property”,如果要执行test1(),添加“priority =1”

             @Test
              public void test1(priority =1){
                System.out.println("execute frist");;
              }
      
              @Test
              public void test2(priority =2){
                assertThat(assertvalue 2);
              }
              @Test
              public void test3(priority =3){
                assertThat(assertvalue 3);
              }
      

      【讨论】:

        【解决方案5】:
        @Test
          public void main() throws IOException,InterruptedException
        {
            FileInputStream fil = new FileInputStream(new File("C:\\Users\\V-DKumaravelu\\ECM Data\\ECM-Data-Sheet.xlsx"));
            XSSFWorkbook workbook=new XSSFWorkbook(fil);
            XSSFSheet sheet=workbook.getSheet("ECM");
            int count=sheet.getLastRowNum();
            System.out.println(count);
        
                for (int i=1;i<=5;i++){
        
                    Snapshot sc = new Snapshot();
        
                XSSFRow row = sheet.getRow(i);
                XSSFCell cell = row.getCell(0);
                String ServiceDate=cell.getStringCellValue();
                System.out.println("DOSate is "+ ServiceDate);
                XSSFCell cell1=row.getCell(1);
                String MemberID=cell1.getStringCellValue();
                System.out.println("Member "+MemberID);
                XSSFCell cell2 = row.getCell(2);
                String FacilityCode=cell2.getStringCellValue();
                System.out.println("Facility Code "+FacilityCode);
                XSSFCell cell3=row.getCell(3);
                String ReleaseOfInfo=cell3.getStringCellValue();
                System.out.println("ROI " + ReleaseOfInfo);
                XSSFCell cell4 = row.getCell(4);
                String ContactName=cell4.getStringCellValue();
                System.out.println("Contact Name " + ContactName);
                XSSFCell cell5=row.getCell(5);
                String ContactPhoneAreaCode=cell5.getStringCellValue();
                System.out.println("ContactPhoneAreaCode " + ContactPhoneAreaCode);
                XSSFCell cell6 = row.getCell(6);
                String ContactPhonePrefix=cell6.getStringCellValue();
                System.out.println("ContactPhoePrefix " + ContactPhonePrefix);
                XSSFCell cell7=row.getCell(7);
                String ContactPhoneLine=cell7.getStringCellValue();
                System.out.println("ContactPhoneLine " + ContactPhoneLine);
                XSSFCell cell8=row.getCell(8);
                String ContactPhoneExt=cell8.getStringCellValue();
                System.out.println("ContactPhoneExt " + ContactPhoneExt);
                XSSFCell cell9 = row.getCell(9);
                String ServiceType=cell9.getStringCellValue();
                System.out.println("ServiceType " + ServiceType);
                XSSFCell cell10=row.getCell(10);
                String ServiceLevel=cell10.getStringCellValue();
                System.out.println("ServiceLevel " + ServiceLevel);
                XSSFCell cell11=row.getCell(11);
                String PRIS=cell11.getStringCellValue();
                System.out.println("PRIS " + PRIS);
                XSSFCell cell12 = row.getCell(12);
                String DiagnosisCode=cell12.getStringCellValue();
                System.out.println("Diagosis " + DiagnosisCode);
                XSSFCell cell13=row.getCell(13);
                String ProcedureCode=cell13.getStringCellValue();
                System.out.println("Procedure Code " + ProcedureCode);
                XSSFCell cell14=row.getCell(14);
                String unitsProcedure=cell14.getStringCellValue();
                System.out.println("Procedure Units " + unitsProcedure);
        
        
                System.setProperty("webdriver.chrome.driver", "C:\\Users\\V-DKumaravelu\\eclipse-workspace\\ECM Portal\\driver\\chromedriver.exe");
                ChromeDriver driver = new ChromeDriver();
                driver.manage().window().maximize();
                driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                driver.get("http://10.32.52.79:8888/ECM/clientlogin.aspx");
                driver.findElement(By.id("txtUserId")).sendKeys("203861P");
                driver.findElement(By.id("txtPassword")).sendKeys("RETINA01");          
                driver.findElement(By.id("btnLogin")).click();
                driver.findElement(By.xpath("//*[@id=\'_ctl7_lbNewPA\']")).click();
                driver.findElement(By.id("outpatientServices2")).click();
                driver.findElement(By.id("serviceDate")).sendKeys(ServiceDate);
                driver.findElement(By.id("memberID")).sendKeys(MemberID);
                sc.main(driver);
                driver.findElement(By.id("nextLink1")).click();
                Select select = new Select(driver.findElement(By.id("facilityCode")));
                select .selectByVisibleText(FacilityCode);
                Select select1 = new Select(driver.findElement(By.id("releaseOfInfo")));
                select1 .selectByVisibleText(ReleaseOfInfo);
                driver.findElement(By.id("ContactName")).sendKeys(ContactName);
                driver.findElement(By.id("ContactPhoneAreaCode")).sendKeys(ContactPhoneAreaCode);
                driver.findElement(By.id("ContactPhonePrefix")).sendKeys(ContactPhonePrefix);
                driver.findElement(By.id("ContactPhoneLine")).sendKeys(ContactPhoneLine);
                driver.findElement(By.id("ContactPhoneExt")).sendKeys(ContactPhoneExt);
                sc.main(driver);
                driver.findElement(By.id("nextLink1")).click();
                Select select2 = new Select(driver.findElement(By.xpath("//*[@id='serviceType']")));
                select2 .selectByVisibleText(ServiceType);
                Select select3 = new Select(driver.findElement(By.xpath("//*[@id=\'levelOfService\']")));
                select3 .selectByVisibleText(ServiceLevel);
                driver.findElement(By.id("facilityPris")).sendKeys(PRIS);
                driver.findElement(By.id("diagnosisCode")).sendKeys(DiagnosisCode);
                driver.findElement(By.id("addDiagnosis")).click();
                driver.findElement(By.id("codeProcedure")).sendKeys(ProcedureCode);
                driver.findElement(By.id("unitsProcedure")).sendKeys(unitsProcedure);
                driver.findElement(By.id("addProcedure")).click();
                sc.main(driver);
                driver.findElement(By.id("nextLink2")).click();
                driver.findElement(By.id("btnSubmit")).click(); 
                sc.main(driver);
                Assert.assertNotNull(driver.findElement(By.xpath("//*[@id=\'Table1\']/tbody/tr[5]/td[6]")),"Failed");
        driver.close();
        }}
        
        }
        

        【讨论】:

        • 我尝试通过 TestNG annotation 执行脚本。我确实有一个循环,当我的变量 i='n' 时,我将执行 'n' 测试。但相反,只有我将整个脚本作为一个单独的测试。请帮助我执行“n”测试
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-09
        • 2018-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-14
        相关资源
        最近更新 更多