Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent TestNg sharing data between parallel tests
    primarykey
    data
    text
    <p><strong>Goal</strong>: To run 2 classes in independently in parallel where each test stores the Method name into a variable that could be accessed later in the test. </p> <p><strong>Issue</strong>: When the tests are run in parallel, they start sharing data between themselves thus corrupting the tests.</p> <p>If you see the console output this is wrong:</p> <pre><code>INFO: Name of Test from Before Method: classB_Method1 INFO: Name of Test from Before Method: classB_Method1 </code></pre> <p>Since these are two separate classes and methods are being run. And I set the correct name here:</p> <pre><code> !! Setting Method name to: classA_Method1 !! Setting Method name to: classB_Method1 </code></pre> <p>The output should look like this:</p> <pre><code>INFO: Name of Test from Before Method: classA_Method1 INFO: Name of Test from Before Method: classB_Method1 </code></pre> <p><strong>TestA</strong></p> <pre><code>import java.lang.reflect.Method; import org.testng.annotations.*; import com.xxxx.util.*; public class TestA { @Test(/*dataProvider = "DP_MVPLoan_Login",*/ groups = {"parallel_test" }, invocationCount = 1, priority = 2, enabled = true) public void classA_Method1(/*String... excelData*/) throws Exception { } ///////////////////////////////////////////////////////////////////////////// // ****SetUp and Tear Down @BeforeTest(alwaysRun=true) public void setupClass() throws Exception { } @BeforeMethod(alwaysRun=true) public void setupMethod(Method method) throws Exception { SeleniumHelperDebug.setCurrentMethodName(method.getName()); SeleniumHelperDebug.defaultBeforeMethod(); } </code></pre> <p>} </p> <p><strong>TestB</strong> </p> <pre><code>import java.lang.reflect.Method; import org.testng.annotations.*; import com.xxxx.util.*; public class TestB { @Test(/*dataProvider = "DP_MVPLoan_Login",*/ groups = { "parallel_test" }, invocationCount = 1, priority = 2, enabled = true) public void classB_Method1(/*String... excelData*/) throws Exception { } ///////////////////////////////////////////////////////////////////////////// // ****SetUp and Tear Down @BeforeTest(alwaysRun=true) public void setupClass() throws Exception { } @BeforeMethod(alwaysRun=true) public void setupMethod(Method method) throws Exception { SeleniumHelperDebug.setCurrentMethodName(method.getName()); SeleniumHelperDebug.defaultBeforeMethod(); } </code></pre> <p>} </p> <p><strong>Helper Method</strong> </p> <pre><code>public class SeleniumHelperDebug { //Name of the method/Test being run private static String currentMethodName; public static String getCurrentMethodName() { return currentMethodName; } public static void setCurrentMethodName(String currentMethodName) { System.out.println("!! Setting Method name to: "+ currentMethodName); SeleniumHelperDebug.currentMethodName = currentMethodName; } //Setup Method. BeforeTest public static void defaultBeforeMethod() throws Exception { Thread.sleep(500); /*setCurrentMethodName(method.getName());*/ System.out.println("INFO: Name of Test from Before Method: " +getCurrentMethodName() ); System.out.println("REMINDER: Keep Browser Window in Foreground to Help prevent F@ilures"); } } </code></pre> <p><strong>Testng.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"&gt; &lt;suite name="Suite" parallel="classes" verbose="2" thread-count="2"&gt; &lt;!-- &lt;listeners&gt; &lt;listener class-name="com.progressfin.util.WebDriverListener"&gt;&lt;/listener&gt; &lt;/listeners --&gt;&gt; &lt;tests&gt; &lt;test name="Test" preserve-order="true"&gt; &lt;!-- &lt;parameter name="browserName" value="firefox"&gt;&lt;/parameter&gt; --&gt; &lt;groups&gt; &lt;run&gt; &lt;include name="parallel_test"/&gt; &lt;/run&gt; &lt;/groups&gt; &lt;classes&gt; &lt;class name="com.xxxx.test.TestA" /&gt; &lt;class name="com.xxxx.test.TestB"/&gt; &lt;/classes&gt; &lt;/test&gt; &lt;!-- Test --&gt; &lt;/tests&gt; &lt;/suite&gt; &lt;!-- Suite --&gt; </code></pre> <p><strong>Console Output</strong></p> <pre><code>... ... TestNG 6.8.6 by Cédric Beust (cedric@beust.com) ... [TestNG] Running: C:\Users\samuel.safyan\workspace\JavaSelenium2\testNgParallelism2.xml [TestRunner] Starting executor for test Test with time out:2147483647 milliseconds. !! Setting Method name to: classA_Method1 !! Setting Method name to: classB_Method1 INFO: Name of Test from Before Method: classB_Method1 REMINDER: Keep Browser Window in Foreground to Help prevent F@ilures INFO: Name of Test from Before Method: classB_Method1 REMINDER: Keep Browser Window in Foreground to Help prevent F@ilures PASSED: classB_Method1 PASSED: classA_Method1 =============================================== Test Tests run: 2, Failures: 0, Skips: 0 =============================================== </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload