javatestngtestng-eclipsetestng.xmltestng-annotation-test

How can I make this testNG test dynamic but remain parallel


public class FactoryTest {
    
    @Test  
    @Parameters("Row")
    public void run1(int row) throws MalformedURLException{           
        new Controller(row);
    }
    
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
  <test thread-count="2" name="factory test" parallel="methods">
    <classes>
      <class name="RealPackage.FactoryTest">
             <methods>
                    <include name="run1">
                        <parameter name="Row"  value="1"/>
                    </include>                
                </methods></class>
    </classes>
  </test> <!-- OfficialTestName -->
</suite> <!-- Suite -->

This is an example of one of the tests I need to run. I need it to run in parallel with other tests. So in the test run1() I create a Controller(row) which initiates the test and I pass a row number to it. I want to run new Controller(1) and new Controller(2) and new Controller(3), etc all at the same time. I am able to do this if I change the java file to this:

public class OfficialTest {
    
    @Test    
    public void run1() throws MalformedURLException{           
        new Controller(1);
    }
    
    @Test    
    public void run2() throws MalformedURLException{           
        new Controller(2);
    }
    
    @Test    
    public void run3() throws MalformedURLException{           
        new Controller(3);
    }
    
    @Test    
    public void run4() throws MalformedURLException{           
        new Controller(4);
    }
    
    @AfterMethod
    public void close() {
        System.out.println("closing");
    }
}

but this isn't dynamic. I need to be able to run this using any range of number for the row. So I was thinking maybe I could generate an XML file that would take care of this but I still am not sure if it would even be able to run in parallel that way.


Solution

  • I was able to fix it with this:

    public class ParallelTests 
    {
    
        int row;
        
        @Parameters({"Row"})
        @BeforeMethod()
        public void setUp(int rowParam) throws MalformedURLException
        {           
           row = rowParam;
        }
        
        @Test
        public void RunTest() throws InterruptedException, MalformedURLException
        {
            new Controller(row);
        }
    
    }
    
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
    <suite thread-count="5" name="BlogSuite" parallel="tests">
    <test name="Test 1">
    <parameter name="Row" value="1"/>
        <classes>
          <class name="RealPackage.ParallelTests"/>
        </classes>
      </test> 
      <test name="Test 2">
    <parameter name="Row" value="2"/>
        <classes>
          <class name="RealPackage.ParallelTests"/>
        </classes>
      </test> 
        <test name="Test 3">
    <parameter name="Row" value="3"/>
        <classes>
          <class name="RealPackage.ParallelTests"/>
        </classes>
      </test> 
        <test name="Test 4">
    <parameter name="Row" value="4"/>
        <classes>
          <class name="RealPackage.ParallelTests"/>
        </classes>
      </test> 
          <test name="Test 5">
    <parameter name="Row" value="5"/>
        <classes>
          <class name="RealPackage.ParallelTests"/>
        </classes>
      </test>
      </suite>