TestNG: How can I run same test case multiple times?

后端 未结 8 1212
你的背包
你的背包 2020-12-30 20:22

I want to run a test case multiple times. Is that configurable in the testng.xml? If I add a loop in the test method, then the results of each run will not be a

8条回答
  •  一向
    一向 (楼主)
    2020-12-30 20:51

    None of the answers so far really give the user the ability to up the invocation count from the testng file, which is what was asked for. This solution piggybacks off of gaurav25's DataProvider solution.

    class TestClass() {
        int invocationCount;
    
        @Parameters({ "invocationCount" })
        @BeginClass
        void BeginClass( @Optional("1") String invocationCount) {
            this.invocationCount = Ingeter.parse(invocationCount)
        }
    
        // It will return a 2D array of size 3x1
        @DataProvider(name="URLprovider")
        private Object[][] getURLs() {
            ArrayList obj = new ArrayList<>(3 * this.invocationCount);
    
            for(int iCount = 0; iCount < this.invocationCount; ++iCount) {
                list.add( new Object[] {"https://www.google.co.in/"} );
                list.add( new Object[] {"http://www.gmail.com/"} );
                list.add( new Object[] {"http://stackoverflow.com/"} );
            }
    
            return list.toArray();
        }
    
        /* Since Data provider for this test method returns 2D array of size
         (3*invocationCount)x1, this test method will run 3*invocationCount 
         times **automatically** with 1 parameter every time. */
        @Test(dataProvider="URLprovider")
        private void notePrice(String url) {
            driver.get(url);
            System.out.println(driver.getTitle());  
        }
    }
    
    
    

    Now you can alter how many test sets get run through the test function with this testng.xml file:

    
        
            
                
                    
                
            
        
    
    

    提交回复
    热议问题