Search code examples
testngtestng-dataprovider

Executing Tests in sequencial in testng


I am executing a testng testsuite. I have one test in the testsuite. That test has a dataprovider which returns 2 records. so the same test has to run twice. I want these tests to run sequentially but what i see is it run parallely. I tried giving singleThread = true to the data provider but didnt work. I see following output

  • BeforeMethod-1
  • BeforeMothod-2
  • Test-1
  • Test-2
  • AfterMethod-1
  • AfterMethod-2

But what I want is

  • BeforeMethod-1
  • Test-1
  • AfterMethod-1
  • BeforeMothod-2
  • Test-2
  • AfterMethod-2

Solution

  • Remove parallel = true from DataProvider method (getData) If specified.

    @DataProvider()
    

    Or

    Make it false.

    @DataProvider(parallel = false)
    

    I tried the following example with TestNG 6.8.5 with default settings from Eclipse:

    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;
    import org.testng.asserts.Assertion;
    
    
    public class TestExample {
    
        @BeforeMethod
        public void beforeMethod(){
            System.out.println("before method ");
        }
    
        @Test(dataProvider="getData")
        public void test1(String username, String password) {
            System.out.println("test");
            System.out.println("you have provided username as::"+username);
            System.out.println("you have provided password as::"+password);
        }
    
    
        @AfterMethod
        public void afterMethod() {
            System.out.println("after method");
    
        }
    
        @DataProvider
        public Object[][] getData()
        {
        //Rows - Number of times your test has to be repeated.
        //Columns - Number of parameters in test data.
        Object[][] data = new Object[2][2];
    
        // 1st row
        data[0][0] ="sampleuser1";
        data[0][1] = "abcdef";
    
        // 2nd row
        data[1][0] ="testuser2";
        data[1][1] = "zxcvb";
    
        return data;
        }
    
    }
    

    which gives me following output:

    before method 
    test
    you have provided username as::sampleuser1
    you have provided password as::abcdef
    after method
    before method 
    test
    you have provided username as::testuser2
    you have provided password as::zxcvb
    after method
    PASSED: test1("sampleuser1", "abcdef")
    PASSED: test1("testuser2", "zxcvb")
    
    ===============================================
        Default test
        Tests run: 2, Failures: 0, Skips: 0
    ===============================================