How to make dynamic parallel tests in testNG

倾然丶 夕夏残阳落幕 提交于 2020-08-26 07:03:11

问题


I am running parallel tests using testNG and inside of my testNg java file I have this code:

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");
    }
}

so this will run 4 parallel tests, each with a different input. How can make this dynamic? I want to eventually test 100 tests in parallel but I don't want to have to write 100 methods. Does testNG have this capability?

I tried this as well, using threads and this made the tests not run at all in parallel. Any advice would be greatly appreciated.

public class OfficialTest extends Thread{
    
    ArrayList<OfficialTest> testThreads = new ArrayList<>();
    
    int row;
    
    ArrayList<ThreadSafeMutableThreadParam> threads = new ArrayList<>();

    
    @Test    
    public void run1() throws MalformedURLException{      
        for (int i = 0; i < 4; i++) {
            threads.add(i, new ThreadSafeMutableThreadParam(i));
        }
        for (int i = 0; i < 4; i++) {
            ThreadSafeMutableThreadParam t = threads.get(i);
            t.run();
        }
    }
    
}

class ThreadSafeMutableThreadParam implements Runnable {
    private int c;

    public ThreadSafeMutableThreadParam( int row ) {
        c = row;
    }

    public synchronized void setC( int c ) {
        this.c = c;
    }

    public synchronized int getC() {
        return c;
    }

    public void run() {
        try {
            new Controller( getC() );
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

回答1:


I fixed my issue 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>


来源:https://stackoverflow.com/questions/63512576/how-to-make-dynamic-parallel-tests-in-testng

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!