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

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

How to make dynamic parallel tests in testNG

问题

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

  1. public class OfficialTest {
  2. @Test
  3. public void run1() throws MalformedURLException{
  4. new Controller(1);
  5. }
  6. @Test
  7. public void run2() throws MalformedURLException{
  8. new Controller(2);
  9. }
  10. @Test
  11. public void run3() throws MalformedURLException{
  12. new Controller(3);
  13. }
  14. @Test
  15. public void run4() throws MalformedURLException{
  16. new Controller(4);
  17. }
  18. @AfterMethod
  19. public void close() {
  20. System.out.println("closing");
  21. }
  22. }
  1. public class OfficialTest extends Thread{
  2. ArrayList<OfficialTest> testThreads = new ArrayList<>();
  3. int row;
  4. ArrayList<ThreadSafeMutableThreadParam> threads = new ArrayList<>();
  5. @Test
  6. public void run1() throws MalformedURLException{
  7. for (int i = 0; i < 4; i++) {
  8. threads.add(i, new ThreadSafeMutableThreadParam(i));
  9. }
  10. for (int i = 0; i < 4; i++) {
  11. ThreadSafeMutableThreadParam t = threads.get(i);
  12. t.run();
  13. }
  14. }
  15. }
  16. class ThreadSafeMutableThreadParam implements Runnable {
  17. private int c;
  18. public ThreadSafeMutableThreadParam( int row ) {
  19. c = row;
  20. }
  21. public synchronized void setC( int c ) {
  22. this.c = c;
  23. }
  24. public synchronized int getC() {
  25. return c;
  26. }
  27. public void run() {
  28. try {
  29. new Controller( getC() );
  30. } catch (MalformedURLException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. }

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

英文:

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

  1. public class OfficialTest {
  2. @Test
  3. public void run1() throws MalformedURLException{
  4. new Controller(1);
  5. }
  6. @Test
  7. public void run2() throws MalformedURLException{
  8. new Controller(2);
  9. }
  10. @Test
  11. public void run3() throws MalformedURLException{
  12. new Controller(3);
  13. }
  14. @Test
  15. public void run4() throws MalformedURLException{
  16. new Controller(4);
  17. }
  18. @AfterMethod
  19. public void close() {
  20. System.out.println(&quot;closing&quot;);
  21. }
  22. }

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.

  1. public class OfficialTest extends Thread{
  2. ArrayList&lt;OfficialTest&gt; testThreads = new ArrayList&lt;&gt;();
  3. int row;
  4. ArrayList&lt;ThreadSafeMutableThreadParam&gt; threads = new ArrayList&lt;&gt;();
  5. @Test
  6. public void run1() throws MalformedURLException{
  7. for (int i = 0; i &lt; 4; i++) {
  8. threads.add(i, new ThreadSafeMutableThreadParam(i));
  9. }
  10. for (int i = 0; i &lt; 4; i++) {
  11. ThreadSafeMutableThreadParam t = threads.get(i);
  12. t.run();
  13. }
  14. }
  15. }
  16. class ThreadSafeMutableThreadParam implements Runnable {
  17. private int c;
  18. public ThreadSafeMutableThreadParam( int row ) {
  19. c = row;
  20. }
  21. public synchronized void setC( int c ) {
  22. this.c = c;
  23. }
  24. public synchronized int getC() {
  25. return c;
  26. }
  27. public void run() {
  28. try {
  29. new Controller( getC() );
  30. } catch (MalformedURLException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. }

答案1

得分: 0

  1. public class ParallelTests
  2. {
  3. int row;
  4. @Parameters({"Row"})
  5. @BeforeMethod()
  6. public void setUp(int rowParam) throws MalformedURLException
  7. {
  8. row = rowParam;
  9. }
  10. @Test
  11. public void RunTest() throws InterruptedException, MalformedURLException
  12. {
  13. new Controller(row);
  14. }
  15. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
  3. <suite thread-count="5" name="BlogSuite" parallel="tests">
  4. <test name="Test 1">
  5. <parameter name="Row" value="1"/>
  6. <classes>
  7. <class name="RealPackage.ParallelTests"/>
  8. </classes>
  9. </test>
  10. <test name="Test 2">
  11. <parameter name="Row" value="2"/>
  12. <classes>
  13. <class name="RealPackage.ParallelTests"/>
  14. </classes>
  15. </test>
  16. <test name="Test 3">
  17. <parameter name="Row" value="3"/>
  18. <classes>
  19. <class name="RealPackage.ParallelTests"/>
  20. </classes>
  21. </test>
  22. <test name="Test 4">
  23. <parameter name="Row" value="4"/>
  24. <classes>
  25. <class name="RealPackage.ParallelTests"/>
  26. </classes>
  27. </test>
  28. <test name="Test 5">
  29. <parameter name="Row" value="5"/>
  30. <classes>
  31. <class name="RealPackage.ParallelTests"/>
  32. </classes>
  33. </test>
  34. </suite>
英文:

I fixed my issue with this:

  1. public class ParallelTests
  2. {
  3. int row;
  4. @Parameters({&quot;Row&quot;})
  5. @BeforeMethod()
  6. public void setUp(int rowParam) throws MalformedURLException
  7. {
  8. row = rowParam;
  9. }
  10. @Test
  11. public void RunTest() throws InterruptedException, MalformedURLException
  12. {
  13. new Controller(row);
  14. }
  15. }
  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;!DOCTYPE suite SYSTEM &quot;https://testng.org/testng-1.0.dtd&quot;&gt;
  3. &lt;suite thread-count=&quot;5&quot; name=&quot;BlogSuite&quot; parallel=&quot;tests&quot;&gt;
  4. &lt;test name=&quot;Test 1&quot;&gt;
  5. &lt;parameter name=&quot;Row&quot; value=&quot;1&quot;/&gt;
  6. &lt;classes&gt;
  7. &lt;class name=&quot;RealPackage.ParallelTests&quot;/&gt;
  8. &lt;/classes&gt;
  9. &lt;/test&gt;
  10. &lt;test name=&quot;Test 2&quot;&gt;
  11. &lt;parameter name=&quot;Row&quot; value=&quot;2&quot;/&gt;
  12. &lt;classes&gt;
  13. &lt;class name=&quot;RealPackage.ParallelTests&quot;/&gt;
  14. &lt;/classes&gt;
  15. &lt;/test&gt;
  16. &lt;test name=&quot;Test 3&quot;&gt;
  17. &lt;parameter name=&quot;Row&quot; value=&quot;3&quot;/&gt;
  18. &lt;classes&gt;
  19. &lt;class name=&quot;RealPackage.ParallelTests&quot;/&gt;
  20. &lt;/classes&gt;
  21. &lt;/test&gt;
  22. &lt;test name=&quot;Test 4&quot;&gt;
  23. &lt;parameter name=&quot;Row&quot; value=&quot;4&quot;/&gt;
  24. &lt;classes&gt;
  25. &lt;class name=&quot;RealPackage.ParallelTests&quot;/&gt;
  26. &lt;/classes&gt;
  27. &lt;/test&gt;
  28. &lt;test name=&quot;Test 5&quot;&gt;
  29. &lt;parameter name=&quot;Row&quot; value=&quot;5&quot;/&gt;
  30. &lt;classes&gt;
  31. &lt;class name=&quot;RealPackage.ParallelTests&quot;/&gt;
  32. &lt;/classes&gt;
  33. &lt;/test&gt;
  34. &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:

确定