如何使这个 TestNG 测试具有动态性但保持并行?

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

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>
</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 numbers for the row. So I was thinking maybe I could generate an XML file that would take care of this, but I'm still not sure if it would even be able to run in parallel that way.

英文:
public class FactoryTest {
    
    @Test  
    @Parameters(&quot;Row&quot;)
    public void run1(int row) throws 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 name=&quot;Suite&quot; parallel=&quot;methods&quot;&gt;
  &lt;test thread-count=&quot;2&quot; name=&quot;factory test&quot; parallel=&quot;methods&quot;&gt;
    &lt;classes&gt;
      &lt;class name=&quot;RealPackage.FactoryTest&quot;&gt;
             &lt;methods&gt;
                    &lt;include name=&quot;run1&quot;&gt;
                        &lt;parameter name=&quot;Row&quot;  value=&quot;1&quot;/&gt;
                    &lt;/include&gt;                
                &lt;/methods&gt;&lt;/class&gt;
    &lt;/classes&gt;
  &lt;/test&gt; &lt;!-- OfficialTestName --&gt;
&lt;/suite&gt; &lt;!-- Suite --&gt;

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

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.

答案1

得分: 0

以下是您提供的内容的翻译部分:

我能够通过以下方式修复它

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

I was able to fix it 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日 06:27:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63513946.html
匿名

发表评论

匿名网友

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

确定