如何在TestNG中创建动态并行测试。

huangapple go评论74阅读模式
英文:

How to make dynamic parallel tests in testNG

问题

以下是翻译后的代码部分:

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

如果您还有其他问题或需要进一步的帮助,请随时提问。

英文:

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(&quot;closing&quot;);
    }
}

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&lt;OfficialTest&gt; testThreads = new ArrayList&lt;&gt;();
    
    int row;
    
    ArrayList&lt;ThreadSafeMutableThreadParam&gt; threads = new ArrayList&lt;&gt;();

	
    @Test    
    public void run1() throws MalformedURLException{      
    	for (int i = 0; i &lt; 4; i++) {
    		threads.add(i, new ThreadSafeMutableThreadParam(i));
    	}
    	for (int i = 0; i &lt; 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

得分: 0

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>
英文:

I fixed my issue with this:

public class ParallelTests 
{

	int row;
    
    @Parameters({&quot;Row&quot;})
    @BeforeMethod()
    public void setUp(int rowParam) throws MalformedURLException
    {           
       row = rowParam;
    }
    
    @Test
    public void RunTest() throws InterruptedException, MalformedURLException
    {
    	new Controller(row);
    }

}
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE suite SYSTEM &quot;https://testng.org/testng-1.0.dtd&quot;&gt;
&lt;suite thread-count=&quot;5&quot; name=&quot;BlogSuite&quot; parallel=&quot;tests&quot;&gt;
&lt;test name=&quot;Test 1&quot;&gt;
&lt;parameter name=&quot;Row&quot; value=&quot;1&quot;/&gt;
    &lt;classes&gt;
      &lt;class name=&quot;RealPackage.ParallelTests&quot;/&gt;
    &lt;/classes&gt;
  &lt;/test&gt; 
  &lt;test name=&quot;Test 2&quot;&gt;
&lt;parameter name=&quot;Row&quot; value=&quot;2&quot;/&gt;
    &lt;classes&gt;
      &lt;class name=&quot;RealPackage.ParallelTests&quot;/&gt;
    &lt;/classes&gt;
  &lt;/test&gt; 
    &lt;test name=&quot;Test 3&quot;&gt;
&lt;parameter name=&quot;Row&quot; value=&quot;3&quot;/&gt;
    &lt;classes&gt;
      &lt;class name=&quot;RealPackage.ParallelTests&quot;/&gt;
    &lt;/classes&gt;
  &lt;/test&gt; 
    &lt;test name=&quot;Test 4&quot;&gt;
&lt;parameter name=&quot;Row&quot; value=&quot;4&quot;/&gt;
    &lt;classes&gt;
      &lt;class name=&quot;RealPackage.ParallelTests&quot;/&gt;
    &lt;/classes&gt;
  &lt;/test&gt; 
      &lt;test name=&quot;Test 5&quot;&gt;
&lt;parameter name=&quot;Row&quot; value=&quot;5&quot;/&gt;
    &lt;classes&gt;
      &lt;class name=&quot;RealPackage.ParallelTests&quot;/&gt;
    &lt;/classes&gt;
  &lt;/test&gt;
  &lt;/suite&gt;

huangapple
  • 本文由 发表于 2020年8月21日 04:17:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63512576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定